@ts-for-gir/lib
Version:
Typescript .d.ts generator from GIR for gjs
134 lines • 3.71 kB
JavaScript
/**
* A logger that displays information in different colors on the console.
* In addition, the environment or the module currently being processed is also included as prepended to the logging string
*/
import { blue, yellow, yellowBright, green, red, white, gray } from 'colorette';
export class Logger {
verbose;
moduleName;
constructor(verbose, moduleName) {
this.verbose = verbose;
this.moduleName = moduleName;
}
static prepend(txt, prepend) {
if (typeof txt === 'string') {
txt = `${prepend}${txt}`;
}
return txt;
}
/**
* Returns something like '[node][Gda-5.0] Could not find type 'Gda.SqlExpr' for 'expr''
* @param txt
* @param logLevel
*/
prependInfo(txt, logLevel) {
if (logLevel || this.moduleName.length > 0) {
txt = Logger.prepend(txt, ' ');
}
if (logLevel) {
if (this.moduleName.length > 0) {
txt = Logger.prepend(txt, ' ' + logLevel);
}
else {
txt = Logger.prepend(txt, logLevel);
}
}
if (this.moduleName.length > 0) {
txt = Logger.prepend(txt, `[${this.moduleName}]`);
}
return txt;
}
log(...args) {
if (!this.verbose) {
return;
}
return console.log(...args);
}
dir(...args) {
if (!this.verbose) {
return;
}
args.forEach((arg) => {
console.dir(arg);
});
return;
}
info(txt, ...args) {
if (!this.verbose) {
return;
}
return console.info(blue(txt), ...args);
}
warn(txt, ...args) {
if (!this.verbose) {
return;
}
txt = this.prependInfo(txt, 'WARN:');
return console.warn(yellow(txt), ...args);
}
debug(txt, ...args) {
if (!this.verbose) {
return;
}
txt = this.prependInfo(txt, 'DEBUG:');
return console.debug(yellowBright(txt), ...args);
}
error(txt, ...args) {
txt = this.prependInfo(txt, 'ERROR:');
return this.danger(txt, ...args);
}
success(txt, ...args) {
if (!this.verbose) {
return;
}
this.log(green(txt), ...args);
}
danger(txt, ...args) {
console.error(red(txt), ...args);
}
muted(txt, ...args) {
this.log(gray(txt), ...args);
}
// Static versions (Here it must be ensured that Verbose is activated)
static log(...args) {
return console.log(...args);
}
static dir(...args) {
args.forEach((arg) => {
console.dir(arg);
});
return;
}
static info(txt, ...args) {
txt = this.prepend(txt, 'INFO: ');
return console.info(blue(txt), ...args);
}
static warn(txt, ...args) {
txt = this.prepend(txt, 'WARN: ');
return console.warn(yellow(txt), ...args);
}
static debug(txt, ...args) {
txt = this.prepend(txt, 'DEBUG: ');
return console.debug(yellowBright(txt), ...args);
}
static error(txt, ...args) {
txt = this.prepend(txt, 'ERROR: ');
return this.danger(txt, ...args);
}
static success(txt, ...args) {
this.log(green(txt), ...args);
}
static danger(txt, ...args) {
this.log(red(txt), ...args);
}
static white(txt, ...args) {
this.log(white(txt), ...args);
}
static yellow(txt, ...args) {
this.log(yellow(txt), ...args);
}
static gray(txt, ...args) {
this.log(gray(txt), ...args);
}
}
//# sourceMappingURL=logger.js.map