Skip to content

Commit 92d5f41

Browse files
committed
chore: convert src/logging.{js,ts}
1 parent a9b7eb6 commit 92d5f41

File tree

3 files changed

+46
-32
lines changed

3 files changed

+46
-32
lines changed

src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ import { pipedProviders } from './pipedproviders'
6767
import { EventsActorId, WithWrappedEmitter, wrapEmitter } from './events'
6868
import { Zones } from './zones'
6969
const debug = createDebug('signalk-server')
70+
import logging from './logging'
7071

7172
import { StreamBundle } from './streambundle'
7273

@@ -95,7 +96,7 @@ class Server {
9596
_.merge(app, opts)
9697

9798
load(app)
98-
app.logging = require('./logging')(app)
99+
app.logging = logging(app)
99100
app.version = '0.0.1'
100101

101102
setupCors(app, getSecurityConfig(app))

src/logging.js renamed to src/logging.ts

+42-27
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,35 @@
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 ?? ''
929
let rememberDebug = false
1030
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')
1632

17-
debugPath = path.join(app.config.configPath, 'debug')
1833
if (fs.existsSync(debugPath)) {
1934
const enabled = fs.readFileSync(debugPath, 'utf8')
2035
if (enabled.length > 0) {
@@ -24,8 +39,8 @@ module.exports = function (app) {
2439
}
2540
}
2641

27-
function storeOutput(output, isError) {
28-
const data = {
42+
function storeOutput(output: string, isError: boolean) {
43+
const data: LogMessage = {
2944
ts: moment().format('MMM DD HH:mm:ss'),
3045
row: output
3146
}
@@ -47,22 +62,22 @@ module.exports = function (app) {
4762
const outWrite = process.stdout.write
4863
const errWrite = process.stderr.write
4964

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>)
5368
}
5469

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>)
5873
}
5974

6075
// send debug to stdout so it does not look like an error
6176
debugCore.log = console.info.bind(console)
6277

63-
function enableDebug(enabled) {
78+
function enableDebug(enabled: string) {
6479
if (enabled.length > 0) {
65-
let all = enabled.split(',')
80+
const all = enabled.split(',')
6681

6782
if (all.indexOf('*') !== -1) {
6883
return false
@@ -97,7 +112,7 @@ module.exports = function (app) {
97112
getDebugSettings: () => {
98113
return { debugEnabled, rememberDebug }
99114
},
100-
rememberDebug: (enabled) => {
115+
rememberDebug: (enabled: boolean) => {
101116
if (debugPath) {
102117
if (enabled) {
103118
fs.writeFileSync(debugPath, debugEnabled)
@@ -115,7 +130,7 @@ module.exports = function (app) {
115130
}
116131
})
117132
},
118-
addDebug: (name) => {
133+
addDebug: (name: string) => {
119134
if (debugEnabled.length > 0) {
120135
const all = debugEnabled.split(',')
121136
if (all.indexOf(name) === -1) {
@@ -125,7 +140,7 @@ module.exports = function (app) {
125140
enableDebug(name)
126141
}
127142
},
128-
removeDebug: (name) => {
143+
removeDebug: (name: string) => {
129144
if (debugEnabled.length > 0) {
130145
const all = debugEnabled.split(',')
131146
const idx = all.indexOf(name)

src/serverroutes.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import {
5151
import { listAllSerialPorts } from './serialports'
5252
import { StreamBundle } from './streambundle'
5353
import { WithWrappedEmitter } from './events'
54+
import { Logging } from './logging'
5455
const readdir = util.promisify(fs.readdir)
5556
const debug = createDebug('signalk-server:serverroutes')
5657
import { getAISShipTypeName } from '@signalk/signalk-schema'
@@ -75,10 +76,7 @@ interface App
7576
PluginManager,
7677
WithWrappedEmitter {
7778
webapps: Package[]
78-
logging: {
79-
rememberDebug: (r: boolean) => void
80-
enableDebug: (r: string) => boolean
81-
}
79+
logging: Logging
8280
activateSourcePriorities: () => void
8381
streambundle: StreamBundle
8482
}

0 commit comments

Comments
 (0)