Skip to content

Instantly share code, notes, and snippets.

@Ore4444
Created April 3, 2017 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ore4444/42b1af88c741b9bc6a242ee034e50a98 to your computer and use it in GitHub Desktop.
Save Ore4444/42b1af88c741b9bc6a242ee034e50a98 to your computer and use it in GitHub Desktop.
muliti-console
class Context {
constructor(...args) {
this[Symbol.for('isContext')] = true;
Object.assign(this, ...args);
}
}
class Reporter {
constructor() {
this.LOG = 'log';
this.INFO = 'info';
this.WARN = 'warn';
this.ERROR = 'error';
}
getContext(args) {
const length = args.length;
if (length && args[length - 1] && args[length - 1][Symbol.for('isContext')]) {
return args.pop();
}
return new Context();
}
log(...args) {
return this._log(this.getContext(args), ...args);
}
_log() {}
error(...args) {
return this._error(this.getContext(args), ...args);
}
_error() {}
info(...args) {
return this._info(this.getContext(args), ...args);
}
_info() {}
warn(...args) {
return this._warn(this.getContext(args), ...args);
}
_warn() {}
toString() {
return '[object Reporter]';
}
}
class Console {
constructor(console) {
Object.defineProperties(this, {
reporters: {
enumerable: false,
configurable: false,
value: [],
},
console: {
enumerable: false,
configurable: false,
value: console,
},
});
}
addReporter(...args) {
args.forEach(reporter => {
if (this.reporters.includes(reporter)) {
return;
}
if (typeof reporter.toString === 'function' && reporter.toString() === '[object Reporter]') {
this.reporters.push(reporter);
}
else {
this.console.error('Added fucked up reporter.', reporter);
}
});
return this;
}
removeReporter(...args) {
args.forEach(reporter => {
const idx = this.reporters.indexOf(reporter);
if (idx > -1) {
this.reporters.splice(idx, 1);
}
});
return this;
}
report(method,...args) {
this.reporters.forEach(reporter => {
try {
reporter[method](...args);
}
catch (e) {
this.console.error('Console reporter raised error. Moving on to next reporter.', e);
}
});
}
log(...args) {
return this.report('log', ...args);
}
info(...args) {
return this.report('info', ...args);
}
warn(...args) {
return this.report('warn', ...args);
}
error(...args) {
return this.report('error', ...args);
}
}
class NodeConsole extends Console {
trace() {
const trace = {};
Error.captureStackTrace(trace, this.trace);
return trace.stack;
}
time(...args) {
return this.console.time(...args);
}
timeEnd(...args) {
return this.console.timeEnd(...args);
}
assert(...args) {
return this.console.assert(...args);
}
dir(...args) {
return this.console.dir(...args);
}
}
class ChromeConsole extends NodeConsole {
count(...args) {
return this.console.count(...args);
}
clear(...args) {
return this.console.clear(...args);
}
dirxml(...args) {
return this.console.dirxml(...args);
}
group(...args) {
return this.console.group(...args);
}
groupCollapsed(...args) {
return this.console.groupCollapsed(...args);
}
groupEnd(...args) {
return this.console.groupEnd(...args);
}
profile(...args) {
return this.console.profile(...args);
}
profileEnd(...args) {
return this.console.profileEnd(...args);
}
timeStamp(...args) {
return this.console.timeStamp(...args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment