1
- const debugCore = require ( 'debug' )
2
- const moment = require ( 'moment' )
3
- const path = require ( 'path' )
4
- const fs = require ( 'fs' )
5
-
6
- module . exports = function ( app ) {
7
- const log = [ ]
8
- let debugEnabled = ''
1
+ import debugCore from 'debug'
2
+ import moment from 'moment'
3
+ import path from 'path'
4
+ import fs from 'fs'
5
+ import type { SignalKServer } from './types'
6
+ import type { WithConfig } from './app'
7
+ import type { WrappedEmitter } from './events'
8
+
9
+ export interface Logging {
10
+ getLog : ( ) => LogMessage [ ]
11
+ enableDebug : ( enabled : string ) => boolean
12
+ getDebugSettings : ( ) => { debugEnabled : string ; rememberDebug : boolean }
13
+ rememberDebug : ( enabled : boolean ) => void
14
+ addDebug : ( name : string ) => void
15
+ removeDebug : ( name : string ) => void
16
+ }
17
+
18
+ type LogMessage = {
19
+ ts : string
20
+ row : string
21
+ isError ?: boolean
22
+ }
23
+
24
+ export default function (
25
+ app : SignalKServer & WithConfig & WrappedEmitter
26
+ ) : Logging {
27
+ const log : LogMessage [ ] = [ ]
28
+ let debugEnabled = process . env . DEBUG ?? ''
9
29
let rememberDebug = false
10
30
const size = 100
11
- let debugPath
12
-
13
- if ( process . env . DEBUG ) {
14
- debugEnabled = process . env . DEBUG
15
- }
31
+ const debugPath = path . join ( app . config . configPath , 'debug' )
16
32
17
- debugPath = path . join ( app . config . configPath , 'debug' )
18
33
if ( fs . existsSync ( debugPath ) ) {
19
34
const enabled = fs . readFileSync ( debugPath , 'utf8' )
20
35
if ( enabled . length > 0 ) {
@@ -24,8 +39,8 @@ module.exports = function (app) {
24
39
}
25
40
}
26
41
27
- function storeOutput ( output , isError ) {
28
- const data = {
42
+ function storeOutput ( output : string , isError : boolean ) {
43
+ const data : LogMessage = {
29
44
ts : moment ( ) . format ( 'MMM DD HH:mm:ss' ) ,
30
45
row : output
31
46
}
@@ -47,22 +62,22 @@ module.exports = function (app) {
47
62
const outWrite = process . stdout . write
48
63
const errWrite = process . stderr . write
49
64
50
- process . stdout . write = function ( string ) {
51
- outWrite . apply ( process . stdout , arguments )
52
- storeOutput ( string , false )
65
+ process . stdout . write = function ( ... args ) {
66
+ storeOutput ( args [ 0 ] . toString ( ) , false )
67
+ return outWrite . apply ( process . stdout , args as Parameters < typeof outWrite > )
53
68
}
54
69
55
- process . stderr . write = function ( string ) {
56
- errWrite . apply ( process . stderr , arguments )
57
- storeOutput ( string , true )
70
+ process . stderr . write = function ( ... args ) {
71
+ storeOutput ( args [ 0 ] . toString ( ) , true )
72
+ return errWrite . apply ( process . stderr , args as Parameters < typeof errWrite > )
58
73
}
59
74
60
75
// send debug to stdout so it does not look like an error
61
76
debugCore . log = console . info . bind ( console )
62
77
63
- function enableDebug ( enabled ) {
78
+ function enableDebug ( enabled : string ) {
64
79
if ( enabled . length > 0 ) {
65
- let all = enabled . split ( ',' )
80
+ const all = enabled . split ( ',' )
66
81
67
82
if ( all . indexOf ( '*' ) !== - 1 ) {
68
83
return false
@@ -97,7 +112,7 @@ module.exports = function (app) {
97
112
getDebugSettings : ( ) => {
98
113
return { debugEnabled, rememberDebug }
99
114
} ,
100
- rememberDebug : ( enabled ) => {
115
+ rememberDebug : ( enabled : boolean ) => {
101
116
if ( debugPath ) {
102
117
if ( enabled ) {
103
118
fs . writeFileSync ( debugPath , debugEnabled )
@@ -115,7 +130,7 @@ module.exports = function (app) {
115
130
}
116
131
} )
117
132
} ,
118
- addDebug : ( name ) => {
133
+ addDebug : ( name : string ) => {
119
134
if ( debugEnabled . length > 0 ) {
120
135
const all = debugEnabled . split ( ',' )
121
136
if ( all . indexOf ( name ) === - 1 ) {
@@ -125,7 +140,7 @@ module.exports = function (app) {
125
140
enableDebug ( name )
126
141
}
127
142
} ,
128
- removeDebug : ( name ) => {
143
+ removeDebug : ( name : string ) => {
129
144
if ( debugEnabled . length > 0 ) {
130
145
const all = debugEnabled . split ( ',' )
131
146
const idx = all . indexOf ( name )
0 commit comments