This repository has been archived by the owner on Feb 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add custom log levels to match buttplug, fix logging events
Make winston logging levels match buttplugs, and make transports emit log messages when received (as the winston "logged" event doesn't work right). When error message is emitted, show in UI.
- Loading branch information
Showing
4 changed files
with
124 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,26 @@ | ||
import * as winston from "winston"; | ||
import * as TransportStream from "winston-transport"; | ||
|
||
export class IntifaceFrontendLogStorageTransport extends TransportStream { | ||
|
||
private _logStore: any[]; | ||
// Maximum number of messages to store. All messages are written to the log | ||
// file. | ||
private _storeLimit: number; | ||
|
||
public constructor(aLogStore: any[], aLogLimit: number, aOptions: TransportStream.TransportStreamOptions) { | ||
super(aOptions); | ||
this._logStore = aLogStore; | ||
this._storeLimit = aLogLimit; | ||
} | ||
|
||
public log(info: any, callback: () => void) { | ||
this._logStore.push(info); | ||
while (this._logStore.length > this._storeLimit) { | ||
this._logStore.shift(); | ||
} | ||
callback(); | ||
} | ||
} | ||
import * as winston from "winston"; | ||
import * as TransportStream from "winston-transport"; | ||
|
||
export class IntifaceFrontendLogStorageTransport extends TransportStream { | ||
|
||
private _logStore: any[]; | ||
// Maximum number of messages to store. All messages are written to the log | ||
// file. | ||
private _storeLimit: number; | ||
|
||
public constructor(aLogStore: any[], aLogLimit: number, aOptions: TransportStream.TransportStreamOptions) { | ||
super(aOptions); | ||
this._logStore = aLogStore; | ||
this._storeLimit = aLogLimit; | ||
} | ||
|
||
public log(info: any, callback: () => void) { | ||
this._logStore.push(info); | ||
while (this._logStore.length > this._storeLimit) { | ||
this._logStore.shift(); | ||
} | ||
// Transports should emit messages | ||
this.emit("logged", info); | ||
callback(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,58 @@ | ||
import * as winston from "winston"; | ||
import * as Transport from "winston-transport"; | ||
import { IntifaceLogger } from "./IntifaceLogger"; | ||
import { FrontendConnector } from "./FrontendConnector"; | ||
import { IntifaceFrontendLoggerTransport } from "./IntifaceFrontendLoggerTransport"; | ||
import { IntifaceFrontendLogStorageTransport } from "./IntifaceFrontendLogStorageTransport"; | ||
|
||
// The Frontend Logger is not actually a logger. It's basically just a shell to | ||
// shuttle messages to the BackendLogger, which actually stores the log messages. | ||
export class IntifaceFrontendLogger extends IntifaceLogger { | ||
|
||
public static AddConnectorTransport(aConnector: FrontendConnector) { | ||
IntifaceFrontendLogger.Logger.add(new IntifaceFrontendLoggerTransport(aConnector, {})); | ||
} | ||
|
||
public static get Logger(): winston.Logger { | ||
if (IntifaceFrontendLogger._logInstance === undefined) { | ||
IntifaceFrontendLogger._logInstance = new IntifaceFrontendLogger(); | ||
} | ||
return IntifaceFrontendLogger._logInstance._logger; | ||
} | ||
|
||
public static GetChildLogger(aLocation: string): winston.Logger { | ||
return IntifaceFrontendLogger.Logger.child({ location: aLocation }); | ||
} | ||
|
||
public static Logs() { | ||
if (IntifaceFrontendLogger._logInstance === undefined) { | ||
throw new Error("Must call create before acessing Frontend Log Store"); | ||
} | ||
return IntifaceFrontendLogger._logInstance._logStore; | ||
} | ||
|
||
protected static _logInstance: IntifaceFrontendLogger; | ||
protected _logStore: any[] = []; | ||
protected _logger: winston.Logger; | ||
|
||
protected constructor() { | ||
super("frontend", "General"); | ||
// DO NOT MAKE ANY WINSTON TRANSPORTS OWNED BY THE LOGGER FOR NOW. | ||
// | ||
// There's something weird about how the TransportStream types are defined | ||
// in winston-transport that causes typescript to freak out when trying to | ||
// compile modules that depend on this one (like intiface), and it is | ||
// extremely hard to debug. All TransportStream instances should be added to | ||
// the logger but never owned by one of our loggers (i.e. don't keep an | ||
// IntifaceFrontendLogStorageTransport as a member of this class.) | ||
this._logger.add(new IntifaceFrontendLogStorageTransport(this._logStore, 10000, {})); | ||
} | ||
} | ||
import * as winston from "winston"; | ||
import * as Transport from "winston-transport"; | ||
import { IntifaceLogger } from "./IntifaceLogger"; | ||
import { FrontendConnector } from "./FrontendConnector"; | ||
import { IntifaceFrontendLoggerTransport } from "./IntifaceFrontendLoggerTransport"; | ||
import { IntifaceFrontendLogStorageTransport } from "./IntifaceFrontendLogStorageTransport"; | ||
|
||
// The Frontend Logger is not actually a logger. It's basically just a shell to | ||
// shuttle messages to the BackendLogger, which actually stores the log messages. | ||
export class IntifaceFrontendLogger extends IntifaceLogger { | ||
|
||
public static AddConnectorTransport(aConnector: FrontendConnector) { | ||
IntifaceFrontendLogger.Logger.add(new IntifaceFrontendLoggerTransport(aConnector, {})); | ||
} | ||
|
||
public static get Instance(): IntifaceFrontendLogger { | ||
return IntifaceFrontendLogger._logInstance; | ||
} | ||
|
||
public static get Logger(): winston.Logger { | ||
if (IntifaceFrontendLogger._logInstance === undefined) { | ||
IntifaceFrontendLogger._logInstance = new IntifaceFrontendLogger(); | ||
} | ||
return IntifaceFrontendLogger._logInstance._logger; | ||
} | ||
|
||
public static GetChildLogger(aLocation: string): winston.Logger { | ||
return IntifaceFrontendLogger.Logger.child({ location: aLocation }); | ||
} | ||
|
||
public static Logs() { | ||
if (IntifaceFrontendLogger._logInstance === undefined) { | ||
throw new Error("Must call create before acessing Frontend Log Store"); | ||
} | ||
return IntifaceFrontendLogger._logInstance._logStore; | ||
} | ||
|
||
protected static _logInstance: IntifaceFrontendLogger; | ||
protected _logStore: any[] = []; | ||
protected _logger: winston.Logger; | ||
|
||
protected constructor() { | ||
super("frontend", "General"); | ||
// DO NOT MAKE ANY WINSTON TRANSPORTS OWNED BY THE LOGGER FOR NOW. | ||
// | ||
// There's something weird about how the TransportStream types are defined | ||
// in winston-transport that causes typescript to freak out when trying to | ||
// compile modules that depend on this one (like intiface), and it is | ||
// extremely hard to debug. All TransportStream instances should be added to | ||
// the logger but never owned by one of our loggers (i.e. don't keep an | ||
// IntifaceFrontendLogStorageTransport as a member of this class.) | ||
const frontendTransport = new IntifaceFrontendLogStorageTransport(this._logStore, 10000, {}); | ||
this._logger.add(frontendTransport); | ||
frontendTransport.on("logged", (transport, level, msg, meta) => { | ||
this.emit("logged", transport, level, msg, meta); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,35 @@ | ||
import * as winston from "winston"; | ||
|
||
export class IntifaceLogger { | ||
protected _logger: winston.Logger; | ||
|
||
protected constructor(aLogType: "frontend" | "backend" | "process", aDefaultLocation: string) { | ||
this._logger = winston.createLogger({ | ||
level: "silly", | ||
format: winston.format.combine( | ||
winston.format.timestamp(), | ||
winston.format.json(), | ||
), | ||
}).child({logType: aLogType, location: aDefaultLocation }); | ||
} | ||
} | ||
import * as winston from "winston"; | ||
import { EventEmitter } from "events"; | ||
|
||
export class IntifaceLogger extends EventEmitter { | ||
protected _logger: winston.Logger; | ||
|
||
protected constructor(aLogType: "frontend" | "backend" | "process", aDefaultLocation: string) { | ||
super(); | ||
const ButtplugLevels = { | ||
levels: { | ||
fatal: 1, | ||
error: 2, | ||
warn: 3, | ||
info: 4, | ||
debug: 5, | ||
trace: 6, | ||
}, | ||
colors: { | ||
fatal: "gray", | ||
error: "red", | ||
warn: "yellow", | ||
info: "green", | ||
debug: "blue", | ||
trace: "purple", | ||
}, | ||
}; | ||
this._logger = winston.createLogger({ | ||
levels: ButtplugLevels.levels, | ||
format: winston.format.combine( | ||
winston.format.timestamp(), | ||
winston.format.json(), | ||
), | ||
}).child({logType: aLogType, location: aDefaultLocation }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters