Skip to content
Snippets Groups Projects
Commit 7fef8e12 authored by Valentin Perrelle's avatar Valentin Perrelle
Browse files

[Dome] Use Dome.Debug

parent e162e1cf
No related branches found
No related tags found
No related merge requests found
...@@ -26,7 +26,6 @@ ...@@ -26,7 +26,6 @@
*/ */
/* eslint-disable max-len */ /* eslint-disable max-len */
/* eslint-disable no-console */
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// --- Evolved Spawn Process // --- Evolved Spawn Process
...@@ -38,6 +37,38 @@ import Exec from 'child_process'; ...@@ -38,6 +37,38 @@ import Exec from 'child_process';
import fspath from 'path'; import fspath from 'path';
import fs from 'fs'; import fs from 'fs';
// --------------------------------------------------------------------------
// --- Logging
// --------------------------------------------------------------------------
/** Development mode flag */
export const DEVEL = process.env.NODE_ENV !== 'production';
export class Debug {
moduleName: string;
constructor(moduleName: string) {
this.moduleName = moduleName;
}
/* eslint-disable no-console */
log(...args: unknown[]): void {
if (DEVEL) console.log(`[${this.moduleName}]`, ...args);
}
warn(...args: unknown[]): void {
if (DEVEL) console.warn(`[${this.moduleName}]`, ...args);
}
error(...args: unknown[]): void {
if (DEVEL) console.error(`[${this.moduleName}]`, ...args);
}
/* eslint-enable no-console */
}
const D = new Debug('dome');
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// --- Platform Specificities // --- Platform Specificities
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
...@@ -55,7 +86,7 @@ switch (process.platform) { ...@@ -55,7 +86,7 @@ switch (process.platform) {
case 'sunos': case 'sunos':
thePlatform = 'linux'; break; thePlatform = 'linux'; break;
default: default:
console.warn(`Unkwnon '${process.platform}' (fallback to 'linux')`); D.warn(`Unknown '${process.platform}' (fallback to 'linux')`);
thePlatform = 'linux'; break; thePlatform = 'linux'; break;
} }
...@@ -72,13 +103,6 @@ switch (process.platform) { ...@@ -72,13 +103,6 @@ switch (process.platform) {
*/ */
export const platform = thePlatform; export const platform = thePlatform;
// --------------------------------------------------------------------------
// --- Logging
// --------------------------------------------------------------------------
/** Development mode flag */
export const DEVEL = process.env.NODE_ENV !== 'production';
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// --- System Emitter // --- System Emitter
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
...@@ -107,7 +131,7 @@ export function atExit(callback: Callback): void { ...@@ -107,7 +131,7 @@ export function atExit(callback: Callback): void {
export function doExit(): void { export function doExit(): void {
exitJobs.forEach((fn) => { exitJobs.forEach((fn) => {
try { fn(); } try { fn(); }
catch (err) { console.error('[Dome] atExit:', err); } catch (err) { D.error('atExit:', err); }
}); });
exitJobs.length = 0; exitJobs.length = 0;
} }
...@@ -399,7 +423,7 @@ async function rmDirRec(path: string): Promise<void> { ...@@ -399,7 +423,7 @@ async function rmDirRec(path: string): Promise<void> {
return; return;
} }
} catch (err) { } catch (err) {
if (DEVEL) console.warn('[Dome.rmDirRec]', err); D.warn(err);
} }
} }
...@@ -444,7 +468,7 @@ atExit(() => { ...@@ -444,7 +468,7 @@ atExit(() => {
childprocess.forEach((process, pid) => { childprocess.forEach((process, pid) => {
try { process.kill(); } try { process.kill(); }
catch (err) { catch (err) {
if (DEVEL) console.warn('[Dome.atExit] killing process', pid, err); D.warn('killing process', pid, err);
} }
}); });
}); });
...@@ -481,7 +505,7 @@ function pipeTee(std: Readable, fd: number): void { ...@@ -481,7 +505,7 @@ function pipeTee(std: Readable, fd: number): void {
if (!fd) return; if (!fd) return;
const out = fs.createWriteStream('<ignored>', { fd, encoding: 'utf-8' }); const out = fs.createWriteStream('<ignored>', { fd, encoding: 'utf-8' });
out.on('error', (err) => { out.on('error', (err) => {
console.warn('[Dome] can not pipe:', err); D.warn('can not pipe:', err);
std.unpipe(out); std.unpipe(out);
}); });
std.pipe(out); std.pipe(out);
......
...@@ -32,7 +32,9 @@ ...@@ -32,7 +32,9 @@
@module dome/data/json @module dome/data/json
*/ */
import { DEVEL } from 'dome/system'; import { Debug } from 'dome/system';
const D = new Debug('Dome.json');
export type json = export type json =
undefined | null | boolean | number | string | undefined | null | boolean | number | string |
...@@ -52,7 +54,7 @@ export function parse(text: string, noError = false): json { ...@@ -52,7 +54,7 @@ export function parse(text: string, noError = false): json {
try { try {
return JSON.parse(text); return JSON.parse(text);
} catch (err) { } catch (err) {
if (DEVEL) console.error('[Dome.json] Invalid format:', err); D.error('Invalid format:', err);
return undefined; return undefined;
} }
} else } else
...@@ -225,7 +227,7 @@ export function jCatch<A>(fn: Loose<A>, fallBack: A): Safe<A> { ...@@ -225,7 +227,7 @@ export function jCatch<A>(fn: Loose<A>, fallBack: A): Safe<A> {
try { try {
return fn(js) ?? fallBack; return fn(js) ?? fallBack;
} catch (err) { } catch (err) {
if (DEVEL) console.warn('[Dome.json]', err); D.warn(err);
return fallBack; return fallBack;
} }
}; };
...@@ -240,7 +242,7 @@ export function jTry<A>(fn: Loose<A>, defaultValue?: A): Loose<A> { ...@@ -240,7 +242,7 @@ export function jTry<A>(fn: Loose<A>, defaultValue?: A): Loose<A> {
try { try {
return fn(js) ?? defaultValue; return fn(js) ?? defaultValue;
} catch (err) { } catch (err) {
if (DEVEL) console.warn('[Dome.json]', err); D.warn(err);
return defaultValue; return defaultValue;
} }
}; };
......
...@@ -75,8 +75,8 @@ function setContextAppNode(): HTMLElement | null { ...@@ -75,8 +75,8 @@ function setContextAppNode(): HTMLElement | null {
// --- Helpers // --- Helpers
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** Configured to be `'true'` when in development mode. */ /** DEVEL is configured to be `'true'` when in development mode. */
export const { DEVEL } = System; export const { DEVEL, Debug } = System;
export type PlatformKind = 'linux' | 'macos' | 'windows'; export type PlatformKind = 'linux' | 'macos' | 'windows';
...@@ -815,30 +815,3 @@ export function useGlobalSettings<A extends Json.json>( ...@@ -815,30 +815,3 @@ export function useGlobalSettings<A extends Json.json>(
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// --- Pretty Printing (Browser Console)
// --------------------------------------------------------------------------
export class Debug {
moduleName: string;
constructor(moduleName: string) {
this.moduleName = moduleName;
}
/* eslint-disable no-console */
log(...args: any): void {
if (DEVEL) console.log(`[${this.moduleName}]`, ...args);
}
warn(...args: any): void {
if (DEVEL) console.warn(`[${this.moduleName}]`, ...args);
}
error(...args: any): void {
if (DEVEL) console.error(`[${this.moduleName}]`, ...args);
}
/* eslint-enable no-console */
}
// --------------------------------------------------------------------------
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment