forked from microsoft/devicescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogging.ts
More file actions
32 lines (30 loc) · 987 Bytes
/
logging.ts
File metadata and controls
32 lines (30 loc) · 987 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import {
JDBus,
Packet,
PACKET_REPORT,
SRV_DEVICE_SCRIPT_MANAGER,
DeviceScriptManagerCmd,
LoggerPriority,
SRV_LOGGER,
LoggerCmd,
} from "jacdac-ts"
import { logToConsole } from "./command"
function prefixMsg(shortId: string, content: string) {
const prefix = content.startsWith(shortId) ? "" : `${shortId}> `
return `${prefix}${content.trimEnd()}`
}
export function enableLogging(bus: JDBus, logFn = logToConsole) {
bus.minLoggerPriority = LoggerPriority.Debug
bus.subscribe(PACKET_REPORT, (pkt: Packet) => {
if (
pkt.serviceClass === SRV_LOGGER &&
LoggerCmd.Debug <= pkt.serviceCommand &&
pkt.serviceCommand <= LoggerCmd.Error
) {
const priority =
LoggerPriority.Debug + (pkt.serviceCommand - LoggerCmd.Debug)
const content = pkt.jdunpack<[string]>("s")[0]
logFn(priority, prefixMsg(pkt.device.shortId, content))
}
})
}