Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove date from logger #333

Merged
merged 9 commits into from
Jan 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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