Updated log files with -client/-worker/-headless suffixes

This commit is contained in:
Lucas Dower 2022-09-16 22:30:43 +01:00
parent f837b56898
commit 97f98f33d0
9 changed files with 138 additions and 13 deletions

View File

@ -22,6 +22,8 @@ export class AppContext {
private _ui: UI; private _ui: UI;
private _workerController: WorkerController; private _workerController: WorkerController;
public constructor() { public constructor() {
Logger.Get.enableLogToFile();
Logger.Get.initLogFile('client');
Logger.Get.enableLOG(); Logger.Get.enableLOG();
Logger.Get.enableLOGMAJOR(); Logger.Get.enableLOGMAJOR();
Logger.Get.enableLOGWARN(); Logger.Get.enableLOGWARN();
@ -71,7 +73,7 @@ export class AppContext {
break; break;
} }
default: { default: {
this._ui.enableTo(action + 1); this._ui.enable(action + 1);
const { builder, style } = this._getActionMessageBuilder(action, payload.statusMessages); const { builder, style } = this._getActionMessageBuilder(action, payload.statusMessages);
uiOutput.setMessage(builder, style as OutputStyle); uiOutput.setMessage(builder, style as OutputStyle);

View File

@ -1,43 +1,59 @@
import fs from 'fs'; import fs from 'fs';
import { AppConfig } from '../config';
import util from 'util'; import util from 'util';
import { AppConfig } from '../config';
import { FileUtil } from './file_util'; import { FileUtil } from './file_util';
import { AppPaths, PathUtil } from './path_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[]) => { export const LOG = (...data: any[]) => {
if (Logger.Get.isLOGEnabled()) { if (Logger.Get.isLOGEnabled()) {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.log(...data); 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[]) => { export const LOG_MAJOR = (...data: any[]) => {
if (Logger.Get.isLOGMAJOREnabled()) { if (Logger.Get.isLOGMAJOREnabled()) {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.log(...data); 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[]) => { export const LOG_WARN = (...data: any[]) => {
if (Logger.Get.isLOGWARNEnabled()) { if (Logger.Get.isLOGWARNEnabled()) {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.warn(...data); 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) => { export const TIME_START = (label: string) => {
if (Logger.Get.isLOGTIMEEnabled()) { if (Logger.Get.isLOGTIMEEnabled()) {
// eslint-disable-next-line no-console // 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) => { export const TIME_END = (label: string) => {
if (Logger.Get.isLOGTIMEEnabled()) { if (Logger.Get.isLOGTIMEEnabled()) {
// eslint-disable-next-line no-console // 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 */ /* eslint-disable-next-line no-console */
export const LOG_ERROR = console.error; export const LOG_ERROR = console.error;
/**
* Logger controls enable/disabling the logging functions above.
*/
export class Logger { export class Logger {
/* Singleton */ /* Singleton */
private static _instance: Logger; private static _instance: Logger;
@ -67,6 +94,8 @@ export class Logger {
private _enabledLOGWARN: boolean; private _enabledLOGWARN: boolean;
private _enabledLOGTIME: boolean; private _enabledLOGTIME: boolean;
private _enabledLogToFile?: boolean;
private _logStream?: fs.WriteStream; private _logStream?: fs.WriteStream;
private constructor() { private constructor() {
@ -74,62 +103,134 @@ export class Logger {
this._enabledLOGMAJOR = false; this._enabledLOGMAJOR = false;
this._enabledLOGWARN = false; this._enabledLOGWARN = false;
this._enabledLOGTIME = false; this._enabledLOGTIME = false;
}
FileUtil.mkdirSyncIfNotExist(AppPaths.Get.logs); /**
if (AppConfig.LOG_TO_FILE) { * Setup the log file.
this._logStream = fs.createWriteStream(PathUtil.join(AppPaths.Get.logs, `./${Date.now()}.log`)); * @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[]) { 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() { public enableLOG() {
this._enabledLOG = true; this._enabledLOG = true;
} }
/**
* Prevent `LOG` calls to be printed to the console and to the log file if setup.
*/
public disableLOG() { public disableLOG() {
this._enabledLOG = false; this._enabledLOG = false;
} }
/**
* Allow `LOG_MAJOR` calls to be printed to the console and to the log file if setup.
*/
public enableLOGMAJOR() { public enableLOGMAJOR() {
this._enabledLOGMAJOR = true; this._enabledLOGMAJOR = true;
} }
/**
* Prevent `LOG_MAJOR` calls to be printed to the console and to the log file if setup.
*/
public disableLOGMAJOR() { public disableLOGMAJOR() {
this._enabledLOGMAJOR = false; this._enabledLOGMAJOR = false;
} }
/**
* Allow `LOG_WARN` calls to be printed to the console and to the log file if setup.
*/
public enableLOGWARN() { public enableLOGWARN() {
this._enabledLOGWARN = true; this._enabledLOGWARN = true;
} }
/**
* Prevent `LOG_WARN` calls to be printed to the console and to the log file if setup.
*/
public disableLOGWARN() { public disableLOGWARN() {
this._enabledLOGWARN = false; this._enabledLOGWARN = false;
} }
/**
* Allow `TIME_START`/`TIME_END` calls to be printed to the console and to the log file if setup.
*/
public enableLOGTIME() { public enableLOGTIME() {
this._enabledLOGTIME = true; this._enabledLOGTIME = true;
} }
/**
* Prevent `TIME_START`/`TIME_END` calls to be printed to the console and to the log file if setup.
*/
public disableLOGTIME() { public disableLOGTIME() {
this._enabledLOGTIME = false; 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() { public isLOGEnabled() {
return this._enabledLOG; return this._enabledLOG;
} }
/**
* Whether or not `LOG_MAJOR` calls should be printed to the console and log file.
*/
public isLOGMAJOREnabled() { public isLOGMAJOREnabled() {
return this._enabledLOGMAJOR; return this._enabledLOGMAJOR;
} }
/**
* Whether or not `LOG_WARN` calls should be printed to the console and log file.
*/
public isLOGWARNEnabled() { public isLOGWARNEnabled() {
return this._enabledLOGWARN; return this._enabledLOGWARN;
} }
/**
* Whether or not `TIME_START`/`TIME_END` calls should be printed to the console and log file.
*/
public isLOGTIMEEnabled() { public isLOGTIMEEnabled() {
return this._enabledLOGTIME; return this._enabledLOGTIME;
} }
/**
* Whether or not console log calls should be logged to the log file if setup.
*/
public isLogToFileEnabled() {
return this.logToFile;
}
} }

View File

@ -10,6 +10,7 @@ import { VoxelMesh } from './voxel_mesh';
import { IVoxeliser } from './voxelisers/base-voxeliser'; import { IVoxeliser } from './voxelisers/base-voxeliser';
import { VoxeliserFactory } from './voxelisers/voxelisers'; import { VoxeliserFactory } from './voxelisers/voxelisers';
import { AssignParams, ExportParams, ImportParams, RenderBlockMeshParams, RenderMeshParams, RenderVoxelMeshParams, VoxeliseParams } from './worker_types'; import { AssignParams, ExportParams, ImportParams, RenderBlockMeshParams, RenderMeshParams, RenderVoxelMeshParams, VoxeliseParams } from './worker_types';
import { Logger } from './util/log_util';
export class WorkerClient { export class WorkerClient {
private static _instance: WorkerClient; private static _instance: WorkerClient;
@ -18,6 +19,8 @@ export class WorkerClient {
} }
private constructor() { private constructor() {
Logger.Get.enableLogToFile();
Logger.Get.initLogFile('worker');
} }
private _loadedMesh?: Mesh; private _loadedMesh?: Mesh;

View File

@ -3,6 +3,7 @@ import { ColourSpace } from '../../src/util';
import { AppPaths, PathUtil } from '../../src/util/path_util'; import { AppPaths, PathUtil } from '../../src/util/path_util';
import { runHeadless, THeadlessConfig } from '../../tools/headless'; import { runHeadless, THeadlessConfig } from '../../tools/headless';
import { FileUtil } from '../../src/util/file_util'; import { FileUtil } from '../../src/util/file_util';
import { TEST_PREAMBLE } from '../preamble';
const baseConfig: THeadlessConfig = { const baseConfig: THeadlessConfig = {
import: { import: {
@ -35,6 +36,8 @@ const baseConfig: THeadlessConfig = {
}; };
test('FULL Obj->Obj', () => { test('FULL Obj->Obj', () => {
TEST_PREAMBLE();
AppPaths.Get.setBaseDir(PathUtil.join(__dirname, '../..')); AppPaths.Get.setBaseDir(PathUtil.join(__dirname, '../..'));
FileUtil.mkdirSyncIfNotExist(PathUtil.join(AppPaths.Get.testData, '../out/')); FileUtil.mkdirSyncIfNotExist(PathUtil.join(AppPaths.Get.testData, '../out/'));

View File

@ -3,6 +3,7 @@ import { ColourSpace } from '../../src/util';
import { AppPaths, PathUtil } from '../../src/util/path_util'; import { AppPaths, PathUtil } from '../../src/util/path_util';
import { runHeadless, THeadlessConfig } from '../../tools/headless'; import { runHeadless, THeadlessConfig } from '../../tools/headless';
import { FileUtil } from '../../src/util/file_util'; import { FileUtil } from '../../src/util/file_util';
import { TEST_PREAMBLE } from '../preamble';
const baseConfig: THeadlessConfig = { const baseConfig: THeadlessConfig = {
import: { import: {
@ -35,6 +36,8 @@ const baseConfig: THeadlessConfig = {
}; };
test('FULL Obj->Obj', () => { test('FULL Obj->Obj', () => {
TEST_PREAMBLE();
AppPaths.Get.setBaseDir(PathUtil.join(__dirname, '../..')); AppPaths.Get.setBaseDir(PathUtil.join(__dirname, '../..'));
FileUtil.mkdirSyncIfNotExist(PathUtil.join(AppPaths.Get.testData, '../out/')); FileUtil.mkdirSyncIfNotExist(PathUtil.join(AppPaths.Get.testData, '../out/'));

View File

@ -3,6 +3,7 @@ import { ColourSpace } from '../../src/util';
import { AppPaths, PathUtil } from '../../src/util/path_util'; import { AppPaths, PathUtil } from '../../src/util/path_util';
import { runHeadless, THeadlessConfig } from '../../tools/headless'; import { runHeadless, THeadlessConfig } from '../../tools/headless';
import { FileUtil } from '../../src/util/file_util'; import { FileUtil } from '../../src/util/file_util';
import { TEST_PREAMBLE } from '../preamble';
const baseConfig: THeadlessConfig = { const baseConfig: THeadlessConfig = {
import: { import: {
@ -35,6 +36,8 @@ const baseConfig: THeadlessConfig = {
}; };
test('FULL Obj->Obj', () => { test('FULL Obj->Obj', () => {
TEST_PREAMBLE();
AppPaths.Get.setBaseDir(PathUtil.join(__dirname, '../..')); AppPaths.Get.setBaseDir(PathUtil.join(__dirname, '../..'));
FileUtil.mkdirSyncIfNotExist(PathUtil.join(AppPaths.Get.testData, '../out/')); FileUtil.mkdirSyncIfNotExist(PathUtil.join(AppPaths.Get.testData, '../out/'));

View File

@ -3,6 +3,7 @@ import { ColourSpace } from '../../src/util';
import { AppPaths, PathUtil } from '../../src/util/path_util'; import { AppPaths, PathUtil } from '../../src/util/path_util';
import { runHeadless, THeadlessConfig } from '../../tools/headless'; import { runHeadless, THeadlessConfig } from '../../tools/headless';
import { FileUtil } from '../../src/util/file_util'; import { FileUtil } from '../../src/util/file_util';
import { TEST_PREAMBLE } from '../preamble';
const baseConfig: THeadlessConfig = { const baseConfig: THeadlessConfig = {
import: { import: {
@ -35,6 +36,8 @@ const baseConfig: THeadlessConfig = {
}; };
test('FULL Obj->Schematic', () => { test('FULL Obj->Schematic', () => {
TEST_PREAMBLE();
AppPaths.Get.setBaseDir(PathUtil.join(__dirname, '../..')); AppPaths.Get.setBaseDir(PathUtil.join(__dirname, '../..'));
FileUtil.mkdirSyncIfNotExist(PathUtil.join(AppPaths.Get.testData, '../out/')); FileUtil.mkdirSyncIfNotExist(PathUtil.join(AppPaths.Get.testData, '../out/'));

5
tests/preamble.ts Normal file
View File

@ -0,0 +1,5 @@
import { Logger } from "../src/util/log_util"
export const TEST_PREAMBLE = () => {
Logger.Get.disableLogToFile();
}

View File

@ -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 { AppPaths, PathUtil } from '../src/util/path_util';
import { runHeadless } from './headless'; import { runHeadless } from './headless';
import { headlessConfig } from './headless-config'; import { headlessConfig } from './headless-config';
void async function main() { void async function main() {
AppPaths.Get.setBaseDir(PathUtil.join(__dirname, '../..')); AppPaths.Get.setBaseDir(PathUtil.join(__dirname, '../..'));
Logger.Get.enableLogToFile();
Logger.Get.initLogFile('headless');
runHeadless(headlessConfig); runHeadless(headlessConfig);