Skip to content

Commit

Permalink
Merge pull request #333 from SassDoc/simplify-logger
Browse files Browse the repository at this point in the history
Streamline and simplify logger messages
  • Loading branch information
pascalduez committed Jan 24, 2015
2 parents b223b3a + bc8957b commit 293902b
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/annotation/annotations/alias.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default function alias(env) {
let aliasedItem = data.find(i => i.context.name === alias);

if (aliasedItem === undefined) {
env.logger.log(`Item "${name}" is an alias of "${alias}" but this item doesn't exist.`);
env.logger.warn(`Item "${name}" is an alias of "${alias}" but this item doesn't exist.`);
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/annotation/annotations/require.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export default function (env) {

if (reqItem === undefined) {
if (!req.autofill) {
env.logger.log(
env.logger.warn(
`Item "${item.context.name}" requires "${req.name}" from type "${req.type}" but this item doesn't exist.`
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/annotation/annotations/see.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default function see(env) {
return seeItem;
}

env.logger.log(`Item "${item.context.name}" refers to "${see.name}" from type "${see.type}" but this item doesn't exist.`);
env.logger.warn(`Item "${item.context.name}" refers to "${see.name}" from type "${see.type}" but this item doesn't exist.`);
})
.filter(x => x !== undefined);

Expand Down
61 changes: 46 additions & 15 deletions src/logger.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,89 @@
const chalk = require('chalk');
const { is, getDateTime } = require('./utils');
const { is } = require('./utils');
const errors = require('./errors');
const chalk = require('chalk');

// Special chars.
let chevron = '\xBB';
let checkmark = '\u2713';
let octopus = '\uD83D\uDC19'; // jshint ignore:line
// Helpers.
let br = str => `[${str}]`; // Wrap in brackets.
let prepend = (arg, arr) => [arg].concat(arr); // Prepend.
let date = arr => prepend(br(getDateTime()), arr); // Prepend date.
let flag = (name, arr) => prepend(br(name), arr); // Prepend flag.
let log = arr => date(arr).join(' '); // Log.
let flog = (name, arr) => log(flag(name, arr)); // Log with flag.
let br = str => `[${str}]`; // Wrap in brackets.

export default class Logger {
constructor(verbose = false, debug = false) {
this.verbose = verbose;
this.debug_ = debug;
this._debug = debug;
this._times = [];
}

/**
* Log arguments into stderr if the verbose mode is enabled.
*/
log(...args) {
if (this.verbose) {
console.error(log(args));
console.error(chalk.green(chevron), ...args);
}
}

info(...args) {
return this.log(...args);
}

/**
* Always log arguments as warning into stderr.
*/
warn(...args) {
chalkHack(() => console.error(chalk.yellow(flog('WARNING', args))));
chalkHack(() =>
console.error(chalk.yellow(chevron), br('WARNING'), ...args)
);
}

/**
* Always log arguments as error into stderr.
*/
error(...args) {
chalkHack(() => console.error(chalk.red(flog('ERROR', args))));
chalkHack(() =>
console.error(chalk.red(chevron), br('ERROR'), ...args)
);
}

/**
* Init a new timer.
* @param {String} label
*/
time(label) {
this._times[label] = Date.now();
}

/**
* End timer and log result.
* @param {String} label
* @param {String} format
*/
timeEnd(label, format) {
let time = this._times[label];
if (!time) {
throw new Error(`No such label: ${label}`);
}

let duration = Date.now() - time;
console.error(`${chalk.green(checkmark)} ${format}`, label, duration);
}

/**
* Log arguments into stderr if debug mode is enabled (will call all
* argument functions to allow "lazy" arguments).
*/
debug(...args) {
if (this.debug_) {
if (this._debug) {
chalkHack(() => {
console.error(chalk.grey(flog('DEBUG', args.map(f => {
console.error(chalk.grey(br('DEBUG'), ...args.map(f => {
if (f instanceof Function) {
return f();
}

return f;
}))));
})));
});
}
}
Expand Down
12 changes: 11 additions & 1 deletion src/sassdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ export default function sassdoc(...args) {
async function documentize(env) { // jshint ignore:line
/* jshint ignore:start */

init(env);

let data = await baseDocumentize(env);

try {
Expand Down Expand Up @@ -156,6 +158,7 @@ export default function sassdoc(...args) {

async function documentize() {
try {
init(env);
await refresh(env);
await theme(env);
okay(env);
Expand Down Expand Up @@ -197,7 +200,6 @@ export function parse(...args) { // jshint ignore:line
*/
async function documentize(env) {
let data = await baseDocumentize(env);
okay(env);

return data;
}
Expand Down Expand Up @@ -346,9 +348,17 @@ function srcEnv(documentize, stream) {
};
}

/**
* Init timer.
*/
function init(env) { // jshint ignore:line
env.logger.time('SassDoc');
}

/**
* Log final success message.
*/
function okay(env) { // jshint ignore:line
env.logger.log('Process over. Everything okay!');
env.logger.timeEnd('SassDoc', '%s completed after %dms');
}
26 changes: 0 additions & 26 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const path = require('path');
const each = require('lodash.foreach');
const glob2base = require('glob2base');
const Glob = require('glob').Glob;
Expand All @@ -11,31 +10,6 @@ export function eachItem(byTypeAndName, cb) {
});
}

// Get file extension.
export var ext = file => path.extname(file).substr(1);

/**
* Get current date/time.
*
* @param {Date} date
* @return {String} Stringified date time.
*/
export function getDateTime(date = new Date()) {
let y, m, d, h, i, s;

y = date.getFullYear();
m = exports.pad(date.getMonth() + 1);
d = exports.pad(date.getDate());
h = exports.pad(date.getHours());
i = exports.pad(date.getMinutes());
s = exports.pad(date.getSeconds());

return `${y}-${m}-${d} ${h}:${i}:${s}`;
}

// Pad a number with a leading 0 if inferior to 10.
export var pad = value => (value < 10 ? '0' : '') + value;

// Namespace delimiters.
let nsDelimiters = ['::', ':', '\\.', '/'];
let ns = new RegExp(nsDelimiters.join('|'), 'g');
Expand Down

0 comments on commit 293902b

Please sign in to comment.