Skip to content

Commit

Permalink
#57 all tests pass.
Browse files Browse the repository at this point in the history
  • Loading branch information
itayw committed Apr 6, 2014
1 parent 2ef0b25 commit 80f713b
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 173 deletions.
93 changes: 0 additions & 93 deletions lib/common/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,99 +8,6 @@
* Some rights reserved. See LICENSE, AUTHORS.
**/


var
os = require('os'),
cluster = require('cluster');

global.stopped = false;
global.shutdown = function (code, callback) {
if (stopped)
return;

stopped = true;
joola.logger.info('Gracefully shutting down, code: ' + code);
joola.state.set('core', 'stop', 'received code [' + code + ']+');

joola.dispatch.emit('nodes:state:change', [joola.UID, joola.state.get()]);

var node = nodeState();
if (node.http) {
//We're running an http node, let's try to start another one.
joola.logger.info('Trying to start Web Services on another node');
joola.dispatch.request(0, 'startWebServer', [node], function () {
//do we care?
});
}
joola.redis.del('nodes:' + joola.UID, function () {
//do we care?
});

setTimeout(function () {
process.exit(code || 0);
}, 10);

if (typeof callback === 'function')
return callback(null);
};

global.emptyfunc = function () {

};

global.rt = function () {
joola.dispatch.roundtrip(function (err, delta) {
if (err)
return console.log('Roundtrip: ' + err);
return console.log('Roundtrip: ' + delta + 'ms');
});
};

global.nodeState = function () {

var hostname = joola.hostname = os.hostname();
var isCluster = false; //Object.keys(cluster.workers).length > 0;
var clusterID = null;// = cluster.worker ? cluster.worker.id : null;
var isMaster = null;// = Object.keys(cluster.workers).length > 0 ? cluster.isMaster : null;
var isWorker = null;// = Object.keys(cluster.workers).length > 0 ? cluster.isWorker : null;

try {
if (typeof cluster.workers === 'object') {
if (cluster.workers === null) {
isCluster = true;
clusterID = cluster.worker ? cluster.worker.id : null;
isMaster = cluster.isMaster;
isWorker = cluster.isWorker;
}
}
}
catch (ex) {
//ignore errors
}

var state = joola.state.get();
var status = state.status;

var result = {
uid: joola.UID,
state: status,
uptime: process.uptime(),
'last-seen': new Date().getTime(),
'last-seen-nice': new Date(),
hostname: hostname,
'usage-cpu': null,
'usage-mem': null,
http: joola.webserver.http._handle ? true : false,
https: joola.webserver.https._handle ? true : false,
cluster: isCluster,
'cluster-id': clusterID,
'cluster-master': isMaster,
'cluster-slave': isWorker
};

return result;
};

joolaio.timezone = function (tz) {
if (tz)
joolaio.options.timezoneOffset = tz;
Expand Down
134 changes: 54 additions & 80 deletions lib/common/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,83 +12,57 @@
var logger = exports;
logger._id = 'logger';

if (process.env.NODE_ENV == 'test') {
logger = module.exports = {
log: function () {
},
silly: function () {

},
debug: function () {

},
info: function () {

},
error: function () {

},
notice: function () {

},
setLevel: function () {

}
};
}
else {
logger._log = function (level, message, callback) {
switch (level) {
case 'debug':
case 'info':
case 'warn':
case 'error':
break;
case 'silly':
level = 'debug';
break;
default:
break;
}

if (!joolaio.options.debug.enabled)
return;

if (typeof message === 'object')
message = '[' + new Date().format('hh:nn:ss.fff') + '] ' + JSON.stringify(message);
else
message = '[' + new Date().format('hh:nn:ss.fff') + '] ' + message;

if (joolaio.options.isBrowser && console.debug) {
if (['silly', 'debug'].indexOf(level) == -1)
console[level](message);
else if (joolaio.options.debug.enabled && ['silly', 'debug'].indexOf(level) > -1)
console[level](message);
}
else
console.log(message);

if (callback)
return callback(null);
};

logger.silly = function (message, callback) {
return this._log('silly', message, callback);
};

logger.info = function (message, callback) {
return this._log('info', message, callback);
};

logger.debug = function (message, callback) {
return this._log('debug', message, callback);
};

logger.warn = function (message, callback) {
return this._log('warn', message, callback);
};

logger.error = function (message, callback) {
return this._log('error', message, callback);
};
}
logger._log = function (level, message, callback) {
switch (level) {
case 'debug':
case 'info':
case 'warn':
case 'error':
break;
case 'silly':
level = 'debug';
break;
default:
break;
}

if (!joolaio.options.debug.enabled)
return;

if (typeof message === 'object')
message = '[' + new Date().format('hh:nn:ss.fff') + '] ' + JSON.stringify(message);
else
message = '[' + new Date().format('hh:nn:ss.fff') + '] ' + message;

if (joolaio.options.isBrowser && console.debug) {
if (['silly', 'debug'].indexOf(level) == -1)
console[level](message);
else if (joolaio.options.debug.enabled && ['silly', 'debug'].indexOf(level) > -1)
console[level](message);
}
else
console.log(message);

if (callback)
return callback(null);
};

logger.silly = function (message, callback) {
return this._log('silly', message, callback);
};

logger.info = function (message, callback) {
return this._log('info', message, callback);
};

logger.debug = function (message, callback) {
return this._log('debug', message, callback);
};

logger.warn = function (message, callback) {
return this._log('warn', message, callback);
};

logger.error = function (message, callback) {
return this._log('error', message, callback);
};
36 changes: 36 additions & 0 deletions test/unit/2_common/logger.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
describe("logger", function () {
before(function () {
this._enabled = joolaio.options.debug.enabled;
joolaio.options.debug.enabled = true;
});

it("should output log message [silly]", function () {
joolaio.logger.silly('test log message');
});

it("should output log message [debug]", function () {
joolaio.logger.debug('test log message');
});

it("should output log message [info]", function () {
joolaio.logger.info('test log message');
});

it("should output log message [warn]", function () {
joolaio.logger.warn('test log message');
});

it("should output log message [error]", function () {
joolaio.options.isBrowser = true; //check also other routes for coverage.
console.debug = console.log;
joolaio.logger.error('test log message');
});

it("should output log message [json]", function () {
joolaio.logger.silly({test: 'log message'});
});

after(function () {
joolaio.options.debug.enabled = this._enabled;
});
});

0 comments on commit 80f713b

Please sign in to comment.