Skip to content

Commit

Permalink
added assert logging
Browse files Browse the repository at this point in the history
  • Loading branch information
vladmandic committed Sep 19, 2021
1 parent 4c2363a commit 299f61f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions dist/pilogger.js
Original file line number Diff line number Diff line change
Expand Up @@ -1776,6 +1776,7 @@ var tags = {
data: ctx.green("DATA: "),
error: ctx.red("ERROR:"),
fatal: ctx.bold.red("FATAL:"),
assert: ctx.italic.bold.red("ASSERT:"),
timed: ctx.magentaBright("TIMED:"),
state: ctx.magenta("STATE:"),
verbose: ctx.bgGray.yellowBright("VERB: "),
Expand Down Expand Up @@ -1854,6 +1855,10 @@ function setClientFile(file) {
streams.clientFile = false;
});
}
async function assert(res, exp, ...messages) {
if (res !== exp)
log("assert", ...messages, { res, exp });
}
async function timed(t0, ...messages) {
if (arguments.length < 2) {
messages = [t0];
Expand Down Expand Up @@ -1958,6 +1963,7 @@ exports.configure = configure;
exports.options = options;
exports.console = print;
exports.timed = timed;
exports.assert = assert;
exports.blank = (...message) => log(...message);
exports.info = (...message) => log("info", ...message);
exports.state = (...message) => log("state", ...message);
Expand Down
6 changes: 6 additions & 0 deletions src/pilogger.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const tags = {
data: ctx.green('DATA: '),
error: ctx.red('ERROR:'),
fatal: ctx.bold.red('FATAL:'),
assert: ctx.italic.bold.red('ASSERT:'),
timed: ctx.magentaBright('TIMED:'),
state: ctx.magenta('STATE:'),
verbose: ctx.bgGray.yellowBright('VERB: '),
Expand Down Expand Up @@ -116,6 +117,10 @@ function setClientFile(file) {
});
}

async function assert(res, exp, ...messages) {
if (res !== exp) log('assert', ...messages, { res, exp });
}

async function timed(t0, ...messages) {
if (arguments.length < 2) {
messages = [t0];
Expand Down Expand Up @@ -223,6 +228,7 @@ exports.options = options;
// actual log methods
exports.console = print; // simple replacement for logger.log
exports.timed = timed; // log with timing
exports.assert = assert; // log if assertion failed
exports.blank = (...message) => log(...message);
exports.info = (...message) => log('info', ...message);
exports.state = (...message) => log('state', ...message);
Expand Down
4 changes: 4 additions & 0 deletions src/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ log.client('client');

log.verbose('verbose');
log.debug('debug');

log.assert(1, 1, 'assert pass');
// @ts-ignore
log.assert(1, 0, 'assert failed');

0 comments on commit 299f61f

Please sign in to comment.