UNPKG

reactronic

Version:

Reactronic - Transactional Reactive State Management

90 lines (89 loc) 4.05 kB
export function error(message, dump) { if (Log.isOn && Log.opt.error) Log.write("█", " ███", message, undefined, " *** ERROR ***", dump); return new Error(message); } export function misuse(message, dump) { const error = new Error(message); Log.write(" ", " ███", message, undefined, " *** ERROR / MISUSE ***", dump !== null && dump !== void 0 ? dump : error); return error; } export function fatal(error) { Log.write(" ", " ███", error.message, undefined, " *** FATAL ***", error); return error; } export class Log { static get opt() { return this.getMergedLoggingOptions(undefined); } static setMode(isOn, options) { Log.global = options || Log.DefaultLevel; if (isOn) { const t = Log.global; const o = Object.keys(Log.global).filter(x => t[x] === true).join(", "); Log.write("", "", `Reactronic logging is turned on: ${o}`); Log.write("", "", "Member-level logging can be configured with @options({ logging: ... }) decorator"); } else if (Log.isOn) Log.write("", "", "Reactronic logging is turned off"); Log.isOn = isOn; } static write(bar, tran, message, ms = 0, highlight = undefined, dump) { Log.writeAs(undefined, bar, tran, message, ms, highlight, dump); } static writeAs(options, bar, tran, message, ms = 0, highlight = undefined, dump) { const t = Log.getMergedLoggingOptions(options); const margin1 = " ".repeat(t.margin1 >= 0 ? t.margin1 : 0); const margin2 = " ".repeat(t.margin2); const enabled = (options && options.enabled !== undefined) ? options.enabled : t.enabled; if (enabled) { console.log("\x1b[37m%s\x1b[0m \x1b[" + t.color + "m%s %s%s\x1b[0m \x1b[" + t.color + "m%s%s\x1b[0m \x1b[" + t.color + "m%s\x1b[0m%s", "", t.prefix, t.transaction ? margin1 : "", t.transaction ? bar : bar.replace(/./g, " "), margin2, tran, message, (highlight !== undefined ? `${highlight}` : "") + (ms > 2 ? ` [ ${ms}ms ]` : "")); if (dump) console.log(dump); } } static merge(t, color, prefix, existing) { const result = !t ? Object.assign({}, existing) : { enabled: t.enabled !== undefined ? t.enabled : existing.enabled, transaction: t.transaction !== undefined ? t.transaction : existing.transaction, operation: t.operation !== undefined ? t.operation : existing.operation, step: t.step !== undefined ? t.step : existing.step, indicator: t.indicator !== undefined ? t.indicator : existing.indicator, read: t.read !== undefined ? t.read : existing.read, write: t.write !== undefined ? t.write : existing.write, change: t.change !== undefined ? t.change : existing.change, obsolete: t.obsolete !== undefined ? t.obsolete : existing.obsolete, error: t.error !== undefined ? t.error : existing.error, warning: t.warning !== undefined ? t.warning : existing.warning, gc: t.gc !== undefined ? t.gc : existing.gc, color: t.color !== undefined ? t.color : existing.color, prefix: t.prefix !== undefined ? t.prefix : existing.prefix, margin1: t.margin1 !== undefined ? t.margin1 : existing.margin1, margin2: t.margin2 !== undefined ? t.margin2 : existing.margin2, }; if (color !== undefined) result.color = color; if (prefix !== undefined) result.prefix = prefix; return result; } } Log.DefaultLevel = { enabled: true, error: false, warning: false, transaction: false, operation: false, step: false, indicator: false, read: false, write: false, change: false, obsolete: false, gc: false, color: 37, prefix: "", margin1: 0, margin2: 0, }; Log.isOn = false; Log.global = Log.DefaultLevel; Log.getMergedLoggingOptions = (local) => Log.global;