Skip to content
This repository has been archived by the owner on Feb 4, 2023. It is now read-only.

Commit

Permalink
chore: Add custom log levels to match buttplug, fix logging events
Browse files Browse the repository at this point in the history
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
qdot committed May 10, 2019
1 parent 89ab052 commit 71422ad
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 89 deletions.
50 changes: 26 additions & 24 deletions packages/core/src/IntifaceFrontendLogStorageTransport.ts
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();
}
}
108 changes: 58 additions & 50 deletions packages/core/src/IntifaceFrontendLogger.ts
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);
});
}
}
50 changes: 35 additions & 15 deletions packages/core/src/IntifaceLogger.ts
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 });
}
}
5 changes: 5 additions & 0 deletions packages/intiface/src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ export default class App extends Vue {
}
this.config = this.connector!.Config!;
this.logger = IntifaceFrontendLogger.GetChildLogger(this.constructor.name);
IntifaceFrontendLogger.Instance.on("logged", (transport: any, level: string, msg: string, meta: object) => {
if (level === "Error") {
this.onError(msg);
}
});
this.logger.info("Intiface desktop application frontend mounted");
this.checkSetup();
}
Expand Down

0 comments on commit 71422ad

Please sign in to comment.