import * as Sentry from "@sentry/node" ; import { DataDirection , getDirectionPrefix , LogContext , ZWaveLogContainer , ZWaveLoggerBase , } from "@zwave-js/core" ; import { buffer2hex , getEnumMemberName , num2hex } from "@zwave-js/shared" ; import { MessageHeaders } from "./MessageHeaders" ; export const SERIAL_LABEL = "SERIAL" ; const SERIAL_LOGLEVEL = "debug" ; export interface SerialLogContext extends LogContext < "serial" > { direction : DataDirection ; header ? : string ; } export class SerialLogger extends ZWaveLoggerBase < SerialLogContext > { constructor ( loggers ) { super ( loggers , SERIAL_LABEL ) ; } private isVisible ( ) { return this . container . isLoglevelVisible ( SERIAL_LOGLEVEL ) ; } param direction public ACK ( direction ) { if ( this . isVisible ( ) ) this . logMessageHeader ( direction , MessageHeaders . ACK ) ; } param direction public NAK ( direction ) { if ( this . isVisible ( ) ) this . logMessageHeader ( direction , MessageHeaders . NAK ) ; } param direction public CAN ( direction ) { if ( this . isVisible ( ) ) this . logMessageHeader ( direction , MessageHeaders . CAN ) ; } private logMessageHeader ( direction , header ) { this . logger . log ( { level : SERIAL_LOGLEVEL , primaryTags : `[${ MessageHeaders [ header ] }]` , message : "" , secondaryTags : `(${ num2hex ( header ) })` , direction : getDirectionPrefix ( direction ) , context : { source : "serial" , header : getEnumMemberName ( MessageHeaders , header ) , direction } } ) ; } param direction param data public data ( direction , data ) { if ( this . isVisible ( ) ) { this . logger . log ( { level : SERIAL_LOGLEVEL , message : `0x${ data . toString ( "hex" ) }` , secondaryTags : `(${ data . length } bytes)` , direction : getDirectionPrefix ( direction ) , context : { source : "serial" , direction } } ) ; } if ( process . env . NODE_ENV !== "test" ) { Sentry . addBreadcrumb ( { category : "serial" , timestamp : Date . now ( ) / 1000 , type : "debug" , message : `${ getDirectionPrefix ( direction ) }${ buffer2hex ( data ) }` } ) ; } } param msg public message ( message ) { if ( this . isVisible ( ) ) { this . logger . log ( { level : SERIAL_LOGLEVEL , message , direction : getDirectionPrefix ( "none" ) , context : { source : "serial" , direction : "none" } } ) ; } } }