Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 22 additions & 17 deletions src/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,29 @@ import type { ISOFile } from './isofile';
*/
const start = new Date();

const LOG_LEVEL_ERROR = 4;
const LOG_LEVEL_WARNING = 3;
const LOG_LEVEL_INFO = 2;
const LOG_LEVEL_DEBUG = 1;

let log_level = LOG_LEVEL_ERROR;

export const Log = {
setLogLevel(level: (module: string, msg?: string) => void) {
if (level === this.debug) log_level = LOG_LEVEL_DEBUG;
else if (level === this.info) log_level = LOG_LEVEL_INFO;
else if (level === this.warn) log_level = LOG_LEVEL_WARNING;
else if (level === this.error) log_level = LOG_LEVEL_ERROR;
else log_level = LOG_LEVEL_ERROR;
LOG_LEVEL_NONE: 9,
LOG_LEVEL_ERROR: 4,
LOG_LEVEL_WARNING: 3,
LOG_LEVEL_INFO: 2,
LOG_LEVEL_DEBUG: 1,

setLogLevel(level: number | ((module: string, msg?: string) => void)) {
if (typeof level === 'number') {
log_level = level;
return;
}
if (level === this.debug) log_level = Log.LOG_LEVEL_DEBUG;
else if (level === this.info) log_level = Log.LOG_LEVEL_INFO;
else if (level === this.warn) log_level = Log.LOG_LEVEL_WARNING;
else if (level === this.error) log_level = Log.LOG_LEVEL_ERROR;
else log_level = Log.LOG_LEVEL_ERROR;
},
debug(module: string, msg?: string) {
if (console.debug === undefined) {
console.debug = console.log;
}
if (LOG_LEVEL_DEBUG >= log_level) {
if (Log.LOG_LEVEL_DEBUG >= log_level) {
console.debug(
'[' + Log.getDurationString(new Date().getTime() - start.getTime(), 1000) + ']',
'[' + module + ']',
Expand All @@ -37,7 +40,7 @@ export const Log = {
this.debug(module.msg);
},
info(module: string, msg?: string) {
if (LOG_LEVEL_INFO >= log_level) {
if (Log.LOG_LEVEL_INFO >= log_level) {
console.info(
'[' + Log.getDurationString(new Date().getTime() - start.getTime(), 1000) + ']',
'[' + module + ']',
Expand All @@ -46,7 +49,7 @@ export const Log = {
}
},
warn(module: string, msg?: string) {
if (LOG_LEVEL_WARNING >= log_level) {
if (Log.LOG_LEVEL_WARNING >= log_level) {
console.warn(
'[' + Log.getDurationString(new Date().getTime() - start.getTime(), 1000) + ']',
'[' + module + ']',
Expand All @@ -57,7 +60,7 @@ export const Log = {
error(module: string, msg?: string, isofile?: ISOFile) {
if (isofile?.onError) {
isofile.onError(module, msg);
} else if (LOG_LEVEL_ERROR >= log_level) {
} else if (Log.LOG_LEVEL_ERROR >= log_level) {
console.error(
'[' + Log.getDurationString(new Date().getTime() - start.getTime(), 1000) + ']',
'[' + module + ']',
Expand Down Expand Up @@ -128,3 +131,5 @@ export const Log = {
}
},
};

let log_level = Log.LOG_LEVEL_ERROR;