Skip to content

Commit

Permalink
Add logger
Browse files Browse the repository at this point in the history
  • Loading branch information
eGavr committed Sep 3, 2014
1 parent 870eb79 commit 3258cf5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 29 deletions.
34 changes: 34 additions & 0 deletions lib/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require('colors');

/**
* Returns current time in an appropriate form
* @example 00:00:00.000
*/
function getCurrTime() {
var now = new Date(),
_hours = now.getHours() + '',
_minutes = now.getMinutes() + '',
_seconds = now.getSeconds() + '',
_milliseconds = now.getMilliseconds() + '',
hours = (_hours.length < 2 ? '0' : '') + _hours,
minutes = (_minutes.length < 2 ? '0' : '') + _minutes,
seconds = (_seconds.length < 2 ? '0' : '') + _seconds,
milliSecLen = _milliseconds.length,
milliseconds = (milliSecLen < 3 ? (milliSecLen === 2 ? '0' : '00') : '') + _milliseconds;

return (hours + ':' + minutes + ':' + seconds + '.' + milliseconds).grey;
}

/**
* Logs the text
* @param {String} - type
* @param {String} - text
* @param {String} - description
*/
module.exports = function (type, text, description) {
if (type === 'o') {
console.log(text);
} else if (type === 'i' || type === 'e') {
console.log(getCurrTime() + ' - ' + (type === 'i' ? 'info'.green : 'error'.red) + ': ' + text + description.grey);
}
}
34 changes: 5 additions & 29 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,7 @@
var fs = require('fs'),
path = require('path'),
shell = require('shelljs');

require('colors');

/**
* Returns current time in an appropriate form
* @example 00:00:00.000
*/
function getCurrTime() {
var now = new Date(),
_hours = now.getHours() + '',
_minutes = now.getMinutes() + '',
_seconds = now.getSeconds() + '',
_milliseconds = now.getMilliseconds() + '',
hours = (_hours.length < 2 ? '0' : '') + _hours,
minutes = (_minutes.length < 2 ? '0' : '') + _minutes,
seconds = (_seconds.length < 2 ? '0' : '') + _seconds,
milliSecLen = _milliseconds.length,
milliseconds = (milliSecLen < 3 ? (milliSecLen === 2 ? '0' : '00') : '') + _milliseconds;

return (hours + ':' + minutes + ':' + seconds + '.' + milliseconds).grey;
}
shell = require('shelljs'),
benchLogger = require('./logger');

/**
* Makes directories
Expand All @@ -37,7 +17,7 @@ function mkdir(_path) {
fs.mkdirSync(part);
} catch(err) {
if (err && err.code !== 'EEXIST') {
console.log(getCurrTime() + ' - ' + 'error'.red + ': [e] Can not create the folder --> ' + part);
benchLogger('e', '[e] Can not create the folder --> ', part);
throw err;
}
}
Expand Down Expand Up @@ -68,10 +48,7 @@ function minimize(minimizer, minMethod) {
* @returns {Object}
*/
return function (minimizerName, inputFile, outputFile) {
console.log(
getCurrTime() + ' - ' + 'info'.green + ': [m] Minimization by ' + minimizerName + ' --> ' +
(inputFile + ' -> ' + outputFile).grey
);
benchLogger('i', '[m] Minimization by ' + minimizerName + ' --> ', inputFile + ' -> ' + outputFile);

var css = fs.readFileSync(inputFile, 'utf-8'),
res = {};
Expand Down Expand Up @@ -107,7 +84,7 @@ function archive(command, fileSuffix) {
.replace(/\[FILE_PATH\]/g, filePath)
.replace(/\[TO_ARCH_CSS\]/g, toArchCSS);

console.log(getCurrTime() + ' - ' + 'info'.green + ': [a] ' + 'Exec bash script --> ' + _command.grey);
benchLogger('i', '[a] Exec bash script --> ', _command);

shell.exec(_command);

Expand All @@ -116,7 +93,6 @@ function archive(command, fileSuffix) {
}

module.exports = {
getCurrTime: getCurrTime,
mkdir: mkdir,
addSymbols: addSymbols,
minimize: minimize,
Expand Down

0 comments on commit 3258cf5

Please sign in to comment.