From 97f98f33d031972ee27e73a80632d98eaab2bc76 Mon Sep 17 00:00:00 2001 From: Lucas Dower Date: Fri, 16 Sep 2022 22:30:43 +0100 Subject: [PATCH] Updated log files with -client/-worker/-headless suffixes --- src/app_context.ts | 4 +- src/util/log_util.ts | 123 +++++++++++++++++++++++++++++--- src/worker_client.ts | 3 + tests/full/objlitematic.test.ts | 3 + tests/full/objobj.test.ts | 3 + tests/full/objschem.test.ts | 3 + tests/full/objschematic.test.ts | 3 + tests/preamble.ts | 5 ++ tools/run-headless.ts | 4 +- 9 files changed, 138 insertions(+), 13 deletions(-) create mode 100644 tests/preamble.ts diff --git a/src/app_context.ts b/src/app_context.ts index f823fe4..effdd22 100644 --- a/src/app_context.ts +++ b/src/app_context.ts @@ -22,6 +22,8 @@ export class AppContext { private _ui: UI; private _workerController: WorkerController; public constructor() { + Logger.Get.enableLogToFile(); + Logger.Get.initLogFile('client'); Logger.Get.enableLOG(); Logger.Get.enableLOGMAJOR(); Logger.Get.enableLOGWARN(); @@ -71,7 +73,7 @@ export class AppContext { break; } default: { - this._ui.enableTo(action + 1); + this._ui.enable(action + 1); const { builder, style } = this._getActionMessageBuilder(action, payload.statusMessages); uiOutput.setMessage(builder, style as OutputStyle); diff --git a/src/util/log_util.ts b/src/util/log_util.ts index 3fa27f3..ed91418 100644 --- a/src/util/log_util.ts +++ b/src/util/log_util.ts @@ -1,43 +1,59 @@ import fs from 'fs'; +import { AppConfig } from '../config'; import util from 'util'; -import { AppConfig } from '../config'; import { FileUtil } from './file_util'; import { AppPaths, PathUtil } from './path_util'; /** - * Performs console.log if logging LOG is enabled + * Logs to console and file if logging `LOG` is enabled. + * This should be used for verbose logs. + * @see LOG_MAJOR */ export const LOG = (...data: any[]) => { if (Logger.Get.isLOGEnabled()) { // eslint-disable-next-line no-console console.log(...data); } - Logger.Get.logToFile(...data); + if (Logger.Get.logToFile) { + Logger.Get.logToFile(...data); + } }; /** - * Performs console.log if logging LOG_MAJOR is enabled + * Logs to console and file if logging `LOG_MAJOR` is enabled. + * This is identical to `LOG` but can be enabled/disabled separately. + * This should be used for important logs. + * @see LOG */ export const LOG_MAJOR = (...data: any[]) => { if (Logger.Get.isLOGMAJOREnabled()) { // eslint-disable-next-line no-console console.log(...data); } - Logger.Get.logToFile(...data); + if (Logger.Get.logToFile) { + Logger.Get.logToFile(...data); + } }; /** - * Performs console.warn if logging LOG_WARN is enabled + * Logs a warning to the console and file if logging `LOG_WARN` is enabled. */ export const LOG_WARN = (...data: any[]) => { if (Logger.Get.isLOGWARNEnabled()) { // eslint-disable-next-line no-console console.warn(...data); } - Logger.Get.logToFile(...data); + if (Logger.Get.logToFile) { + Logger.Get.logToFile(...data); + } }; +/** + * Starts a timer. + * @see `TIME_END` To stop the timer. + * @param label The ID of this timer. + */ export const TIME_START = (label: string) => { if (Logger.Get.isLOGTIMEEnabled()) { // eslint-disable-next-line no-console @@ -45,6 +61,11 @@ export const TIME_START = (label: string) => { } }; +/** + * Stops a timer and prints the time elapsed. Not logged to file. + * @see `TIME_START` To start the timer. + * @param label The ID of this timer. + */ export const TIME_END = (label: string) => { if (Logger.Get.isLOGTIMEEnabled()) { // eslint-disable-next-line no-console @@ -52,9 +73,15 @@ export const TIME_END = (label: string) => { } }; +/** + * Logs an error to the console and file, always. + */ /* eslint-disable-next-line no-console */ export const LOG_ERROR = console.error; +/** + * Logger controls enable/disabling the logging functions above. + */ export class Logger { /* Singleton */ private static _instance: Logger; @@ -67,6 +94,8 @@ export class Logger { private _enabledLOGWARN: boolean; private _enabledLOGTIME: boolean; + private _enabledLogToFile?: boolean; + private _logStream?: fs.WriteStream; private constructor() { @@ -74,62 +103,134 @@ export class Logger { this._enabledLOGMAJOR = false; this._enabledLOGWARN = false; this._enabledLOGTIME = false; + } - FileUtil.mkdirSyncIfNotExist(AppPaths.Get.logs); - if (AppConfig.LOG_TO_FILE) { - this._logStream = fs.createWriteStream(PathUtil.join(AppPaths.Get.logs, `./${Date.now()}.log`)); + /** + * Setup the log file. + * @param suffix The suffix to append to the end of the log file name. + */ + public initLogFile(suffix: string) { + if (this._logStream === undefined && this._enabledLogToFile === true) { + FileUtil.mkdirSyncIfNotExist(AppPaths.Get.logs); + this._logStream = fs.createWriteStream(PathUtil.join(AppPaths.Get.logs, `./${Date.now()}-${suffix}.log`)); } } + /** + * Logs to the log file if setup. + * @param data The data to print. + */ public logToFile(...data: any[]) { - this._logStream?.write(util.format(...data) + '\n'); + if (this._logStream && this._enabledLogToFile) { + this._logStream.write(`${util.format(...data)}\n`); + } } + /** + * Allow `LOG` calls to be printed to the console and to the log file if setup. + */ public enableLOG() { this._enabledLOG = true; } + /** + * Prevent `LOG` calls to be printed to the console and to the log file if setup. + */ public disableLOG() { this._enabledLOG = false; } + /** + * Allow `LOG_MAJOR` calls to be printed to the console and to the log file if setup. + */ public enableLOGMAJOR() { this._enabledLOGMAJOR = true; } + /** + * Prevent `LOG_MAJOR` calls to be printed to the console and to the log file if setup. + */ public disableLOGMAJOR() { this._enabledLOGMAJOR = false; } + /** + * Allow `LOG_WARN` calls to be printed to the console and to the log file if setup. + */ public enableLOGWARN() { this._enabledLOGWARN = true; } + /** + * Prevent `LOG_WARN` calls to be printed to the console and to the log file if setup. + */ public disableLOGWARN() { this._enabledLOGWARN = false; } + /** + * Allow `TIME_START`/`TIME_END` calls to be printed to the console and to the log file if setup. + */ public enableLOGTIME() { this._enabledLOGTIME = true; } + /** + * Prevent `TIME_START`/`TIME_END` calls to be printed to the console and to the log file if setup. + */ public disableLOGTIME() { this._enabledLOGTIME = false; } + /** + * Allow console log calls to logged to the log file if setup. + * Should be called before `initLogFile` + */ + public enableLogToFile() { + if (AppConfig.LOG_TO_FILE && this._enabledLogToFile === undefined) { + this._enabledLogToFile = true; + } + } + + /** + * Prevent console log calls to logged to the log file if setup. + */ + public disableLogToFile() { + this._enabledLogToFile = false; + } + + /** + * Whether or not `LOG` calls should be printed to the console and log file. + */ public isLOGEnabled() { return this._enabledLOG; } + /** + * Whether or not `LOG_MAJOR` calls should be printed to the console and log file. + */ public isLOGMAJOREnabled() { return this._enabledLOGMAJOR; } + /** + * Whether or not `LOG_WARN` calls should be printed to the console and log file. + */ public isLOGWARNEnabled() { return this._enabledLOGWARN; } + /** + * Whether or not `TIME_START`/`TIME_END` calls should be printed to the console and log file. + */ public isLOGTIMEEnabled() { return this._enabledLOGTIME; } + + /** + * Whether or not console log calls should be logged to the log file if setup. + */ + public isLogToFileEnabled() { + return this.logToFile; + } } diff --git a/src/worker_client.ts b/src/worker_client.ts index f1b41af..273d496 100644 --- a/src/worker_client.ts +++ b/src/worker_client.ts @@ -10,6 +10,7 @@ import { VoxelMesh } from './voxel_mesh'; import { IVoxeliser } from './voxelisers/base-voxeliser'; import { VoxeliserFactory } from './voxelisers/voxelisers'; import { AssignParams, ExportParams, ImportParams, RenderBlockMeshParams, RenderMeshParams, RenderVoxelMeshParams, VoxeliseParams } from './worker_types'; +import { Logger } from './util/log_util'; export class WorkerClient { private static _instance: WorkerClient; @@ -18,6 +19,8 @@ export class WorkerClient { } private constructor() { + Logger.Get.enableLogToFile(); + Logger.Get.initLogFile('worker'); } private _loadedMesh?: Mesh; diff --git a/tests/full/objlitematic.test.ts b/tests/full/objlitematic.test.ts index cad2238..59d4692 100644 --- a/tests/full/objlitematic.test.ts +++ b/tests/full/objlitematic.test.ts @@ -3,6 +3,7 @@ import { ColourSpace } from '../../src/util'; import { AppPaths, PathUtil } from '../../src/util/path_util'; import { runHeadless, THeadlessConfig } from '../../tools/headless'; import { FileUtil } from '../../src/util/file_util'; +import { TEST_PREAMBLE } from '../preamble'; const baseConfig: THeadlessConfig = { import: { @@ -35,6 +36,8 @@ const baseConfig: THeadlessConfig = { }; test('FULL Obj->Obj', () => { + TEST_PREAMBLE(); + AppPaths.Get.setBaseDir(PathUtil.join(__dirname, '../..')); FileUtil.mkdirSyncIfNotExist(PathUtil.join(AppPaths.Get.testData, '../out/')); diff --git a/tests/full/objobj.test.ts b/tests/full/objobj.test.ts index ecf248b..d3f5f35 100644 --- a/tests/full/objobj.test.ts +++ b/tests/full/objobj.test.ts @@ -3,6 +3,7 @@ import { ColourSpace } from '../../src/util'; import { AppPaths, PathUtil } from '../../src/util/path_util'; import { runHeadless, THeadlessConfig } from '../../tools/headless'; import { FileUtil } from '../../src/util/file_util'; +import { TEST_PREAMBLE } from '../preamble'; const baseConfig: THeadlessConfig = { import: { @@ -35,6 +36,8 @@ const baseConfig: THeadlessConfig = { }; test('FULL Obj->Obj', () => { + TEST_PREAMBLE(); + AppPaths.Get.setBaseDir(PathUtil.join(__dirname, '../..')); FileUtil.mkdirSyncIfNotExist(PathUtil.join(AppPaths.Get.testData, '../out/')); diff --git a/tests/full/objschem.test.ts b/tests/full/objschem.test.ts index 62c547a..ec5b1c7 100644 --- a/tests/full/objschem.test.ts +++ b/tests/full/objschem.test.ts @@ -3,6 +3,7 @@ import { ColourSpace } from '../../src/util'; import { AppPaths, PathUtil } from '../../src/util/path_util'; import { runHeadless, THeadlessConfig } from '../../tools/headless'; import { FileUtil } from '../../src/util/file_util'; +import { TEST_PREAMBLE } from '../preamble'; const baseConfig: THeadlessConfig = { import: { @@ -35,6 +36,8 @@ const baseConfig: THeadlessConfig = { }; test('FULL Obj->Obj', () => { + TEST_PREAMBLE(); + AppPaths.Get.setBaseDir(PathUtil.join(__dirname, '../..')); FileUtil.mkdirSyncIfNotExist(PathUtil.join(AppPaths.Get.testData, '../out/')); diff --git a/tests/full/objschematic.test.ts b/tests/full/objschematic.test.ts index 2d72f87..75074ac 100644 --- a/tests/full/objschematic.test.ts +++ b/tests/full/objschematic.test.ts @@ -3,6 +3,7 @@ import { ColourSpace } from '../../src/util'; import { AppPaths, PathUtil } from '../../src/util/path_util'; import { runHeadless, THeadlessConfig } from '../../tools/headless'; import { FileUtil } from '../../src/util/file_util'; +import { TEST_PREAMBLE } from '../preamble'; const baseConfig: THeadlessConfig = { import: { @@ -35,6 +36,8 @@ const baseConfig: THeadlessConfig = { }; test('FULL Obj->Schematic', () => { + TEST_PREAMBLE(); + AppPaths.Get.setBaseDir(PathUtil.join(__dirname, '../..')); FileUtil.mkdirSyncIfNotExist(PathUtil.join(AppPaths.Get.testData, '../out/')); diff --git a/tests/preamble.ts b/tests/preamble.ts new file mode 100644 index 0000000..6bc2157 --- /dev/null +++ b/tests/preamble.ts @@ -0,0 +1,5 @@ +import { Logger } from "../src/util/log_util" + +export const TEST_PREAMBLE = () => { + Logger.Get.disableLogToFile(); +} \ No newline at end of file diff --git a/tools/run-headless.ts b/tools/run-headless.ts index 55522c5..02e043d 100644 --- a/tools/run-headless.ts +++ b/tools/run-headless.ts @@ -1,10 +1,12 @@ -import { LOG_MAJOR } from '../src/util/log_util'; +import { Logger, LOG_MAJOR } from '../src/util/log_util'; import { AppPaths, PathUtil } from '../src/util/path_util'; import { runHeadless } from './headless'; import { headlessConfig } from './headless-config'; void async function main() { AppPaths.Get.setBaseDir(PathUtil.join(__dirname, '../..')); + Logger.Get.enableLogToFile(); + Logger.Get.initLogFile('headless'); runHeadless(headlessConfig);