From 6089e5fe20adfb8d7eca0458d4b891328c7ab034 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20M=C3=A9tais?= Date: Fri, 28 Nov 2014 17:53:39 +0100 Subject: [PATCH] Introduce debug instead of console.log --- bin/cli.js | 8 ++++---- lib/rulesChecker.js | 10 ++++++++++ lib/runner.js | 5 +++-- lib/tools/phantomasWrapper.js | 16 +++++++++------- package.json | 9 +++++---- 5 files changed, 31 insertions(+), 17 deletions(-) diff --git a/bin/cli.js b/bin/cli.js index 3937df3..88b615a 100644 --- a/bin/cli.js +++ b/bin/cli.js @@ -1,6 +1,6 @@ #!/usr/bin/env node -//var color = require('colors'); +var debug = require('debug')('ylt:cli'); var YellowLabTools = require('../lib/yellowlabtools'); @@ -17,17 +17,17 @@ var url = process.argv[2]; 'use strict'; var ylt = new YellowLabTools(url); - console.log('Test launched...'); + debug('Test launched...'); ylt. then(function(data) { - console.log('Success'); + debug('Success'); console.log(JSON.stringify(data, null, 2)); }).fail(function(err) { - console.error('Test failed for %s', url); + debug('Test failed for %s', url); console.error(err); }); diff --git a/lib/rulesChecker.js b/lib/rulesChecker.js index 042d73e..c576c72 100644 --- a/lib/rulesChecker.js +++ b/lib/rulesChecker.js @@ -1,4 +1,5 @@ var Q = require('q'); +var debug = require('debug')('ylt:ruleschecker'); var RulesChecker = function() { 'use strict'; @@ -8,6 +9,8 @@ var RulesChecker = function() { var results = {}; var err = null; + debug('Starting checking rules'); + for (var metricName in policies) { var policy = policies[metricName]; @@ -38,9 +41,16 @@ var RulesChecker = function() { rule.abnormalityScore = Math.min(Math.round(abnormalityScore), 0); results[metricName] = rule; + + } else { + + debug('Metric %s not found for tool %s', metricName, policy.tool); + } } + debug('Rules checking finished'); + return results; }; }; diff --git a/lib/runner.js b/lib/runner.js index 67e641c..c9083c1 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -1,4 +1,5 @@ var Q = require('q'); +var debug = require('debug')('ylt:yellowlabtools'); var phantomasWrapper = require('./tools/phantomasWrapper'); var rulesChecker = require('./rulesChecker'); @@ -29,8 +30,8 @@ var Runner = function(params) { deferred.resolve(data); }).fail(function(err) { - console.log('Run failed'); - console.log(err); + debug('Run failed'); + debug(err); deferred.reject(err); }); diff --git a/lib/tools/phantomasWrapper.js b/lib/tools/phantomasWrapper.js index 7186941..e1df01d 100644 --- a/lib/tools/phantomasWrapper.js +++ b/lib/tools/phantomasWrapper.js @@ -1,7 +1,9 @@ var async = require('async'); var Q = require('q'); +var debug = require('debug')('ylt:phantomaswrapper'); var phantomas = require('phantomas'); + var PhantomasWrapper = function() { 'use strict'; @@ -47,16 +49,16 @@ var PhantomasWrapper = function() { }; // Output the command line for debugging purpose - console.log('If you want to reproduce the phantomas task only, copy the following command line:'); + debug('If you want to reproduce the phantomas task only, copy the following command line:'); var optionsString = ''; for (var opt in options) { optionsString += ' ' + '--' + opt + '=' + options[opt]; } - console.log('node node_modules/phantomas/bin/phantomas.js --url=' + task.url + optionsString + ' --verbose'); + debug('node node_modules/phantomas/bin/phantomas.js --url=' + task.url + optionsString + ' --verbose'); // Kill the application if nothing happens for 10 minutes var killer = setTimeout(function() { - console.log('Killing the app because the test on ' + task.url + ' was launched 10 minutes ago'); + debug('Killing the app because the test on ' + task.url + ' was launched 10 minutes ago'); // If in server mode, forever will restart the server process.exit(1); }, 600000); @@ -66,7 +68,7 @@ var PhantomasWrapper = function() { async.retry(triesNumber, function(cb) { phantomas(task.url, options, function(err, json, results) { - console.log('Returning from Phantomas'); + debug('Returning from Phantomas'); // Adding some YellowLabTools errors here if (json && json.metrics && !json.metrics.javascriptExecutionTree) { @@ -79,12 +81,12 @@ var PhantomasWrapper = function() { // Don't cancel test if it is a timeout and we've got some results if (err === 252 && json) { - console.log('Timeout after ' + options.timeout + ' seconds. But it\'s not a problem, the test is valid.'); + debug('Timeout after ' + options.timeout + ' seconds. But it\'s not a problem, the test is valid.'); err = null; } if (err) { - console.log('Attempt failed. Error code ' + err); + debug('Attempt failed. Error code ' + err); } cb(err, json); @@ -94,7 +96,7 @@ var PhantomasWrapper = function() { clearTimeout(killer); if (err) { - console.log('All ' + triesNumber + ' attemps failed for the test'); + debug('All ' + triesNumber + ' attemps failed for the test'); deferred.reject(err); } else { diff --git a/package.json b/package.json index 896a474..eed79c0 100644 --- a/package.json +++ b/package.json @@ -10,12 +10,13 @@ }, "main": "./lib/yellowlabtools.js", "dependencies": { - "phantomas": "1.7.0", - "express": "~4.10.1", "async": "~0.9.0", - "socket.io": "~1.2.0", "body-parser": "~1.9.2", - "compression": "~1.2.0" + "compression": "~1.2.0", + "debug": "^2.1.0", + "express": "~4.10.1", + "phantomas": "1.7.0", + "socket.io": "~1.2.0" }, "devDependencies": { "chai": "^1.9.2",