Added log module

This commit is contained in:
Thomas FORGIONE 2015-07-03 13:27:05 +02:00
parent e8af26913e
commit c44c13e34a
11 changed files with 88 additions and 27 deletions

View File

@ -1,3 +1,5 @@
var Log = require('../lib/NodeLog.js');
/** /**
* Represents a mesh. All meshes are loaded once in geo.availableMesh to avoid * Represents a mesh. All meshes are loaded once in geo.availableMesh to avoid
* loading at each mesh request * loading at each mesh request
@ -157,7 +159,7 @@ function trySetLoaded() {
} }
console.log("[OBJ] All meshes are ready"); Log.ready("All meshes are ready");
} }
var availableMeshNames = { var availableMeshNames = {

65
lib/NodeLog.js Normal file
View File

@ -0,0 +1,65 @@
var Log = {};
var Colors = Object.freeze({
DEFAULT: '\033[0m',
BLACK: '\033[30m',
RED: '\033[31m',
GREEN: '\033[32m',
YELLOW: '\033[33m',
BLUE: '\033[34m',
MAGENTA: '\033[35m',
CYAN: '\033[36m',
});
var isDev = require('express')().get('env') === 'development';
var log;
if (isDev) {
log = function(elt, color) {
console.log(color + elt + Colors.DEFAULT);
}
} else {
log = function(elt, color) {
console.log(elt);
}
}
Log.ready = function(msg) {
log('[RDY] ' + new Date() + ' ' + msg, Colors.GREEN);
}
Log.request = function(req, res) {
log(
'[REQ] ' + new Date() + ' ' +
(req.headers['x-forwarded-for'] || req.connection.remoteAddress) +
' : ' + req.url,
Colors.CYAN
);
}
Log.socket = {};
Log.socket.connection = function(socket) {
log(
'[SOK] ' + new Date() + ' ' +
socket.handshake.address + ' connection',
Colors.YELLOW
);
}
Log.socket.disconnect = function(socket) {
log(
'[SOK] ' + new Date() + ' ' +
socket.handshake.address + ' disconnect',
Colors.YELLOW
);
}
Log.dberror = function(error) {
log(
'[DBE] ' + new Date() + ' ' + error,
Colors.RED
);
}
module.exports = Log;

View File

@ -1,5 +1,6 @@
var pg = require('pg'); var pg = require('pg');
var secret = require('../../private'); var secret = require('../../private');
var Log = require('../../lib/NodeLog.js');
module.exports.index = function(req, res) { module.exports.index = function(req, res) {
@ -9,7 +10,7 @@ module.exports.index = function(req, res) {
[req.session.exp_id, req.body.arrow_id, req.body.time], [req.session.exp_id, req.body.arrow_id, req.body.time],
function(err, result) { function(err, result) {
if (err !== null) if (err !== null)
console.log("[DBERROR] " + err); Log.dberror(err);
release(); release();
} }
); );

View File

@ -1,5 +1,6 @@
var pg = require('pg'); var pg = require('pg');
var secret = require('../../private'); var secret = require('../../private');
var Log = require('../../lib/NodeLog.js');
module.exports.index = function(req, res) { module.exports.index = function(req, res) {
@ -9,7 +10,7 @@ module.exports.index = function(req, res) {
[req.session.exp_id, req.body.coin_id, req.body.time], [req.session.exp_id, req.body.coin_id, req.body.time],
function(err, result) { function(err, result) {
if (err !== null) if (err !== null)
console.log("[DBERROR] " + err); Log.dberror(err);
release(); release();
} }
); );

View File

@ -1,5 +1,6 @@
var pg = require('pg'); var pg = require('pg');
var secret = require('../../private'); var secret = require('../../private');
var Log = require('../../lib/NodeLog.js');
module.exports.index = function(req, res) { module.exports.index = function(req, res) {
@ -9,7 +10,7 @@ module.exports.index = function(req, res) {
[req.session.exp_id, req.body.fps], [req.session.exp_id, req.body.fps],
function(err, result) { function(err, result) {
if (err !== null) if (err !== null)
console.log("[DBERROR] " + err); Log.dberror(err);
release(); release();
} }
); );

View File

@ -1,5 +1,6 @@
var pg = require('pg'); var pg = require('pg');
var secret = require('../../private'); var secret = require('../../private');
var Log = require('../../lib/NodeLog.js');
module.exports.index = function(req, res) { module.exports.index = function(req, res) {
@ -15,7 +16,7 @@ module.exports.index = function(req, res) {
], ],
function(err, result) { function(err, result) {
if (err !== null) if (err !== null)
console.log("[DBERROR] " + err); Log.dberror(err);
release(); release();
} }
); );

View File

@ -1,5 +1,6 @@
var pg = require('pg'); var pg = require('pg');
var secret = require('../../private'); var secret = require('../../private');
var Log = require('../../lib/NodeLog.js');
module.exports.index = function(req, res) { module.exports.index = function(req, res) {
@ -21,7 +22,7 @@ module.exports.index = function(req, res) {
], ],
function(err, result) { function(err, result) {
if (err !== null) if (err !== null)
console.log("[DBERROR] " + err); Log.dberror(err);
release(); release();
} }
); );

View File

@ -1,5 +1,6 @@
var pg = require('pg'); var pg = require('pg');
var secret = require('../../private'); var secret = require('../../private');
var Log = require('../../lib/NodeLog.js');
module.exports.index = function(req, res) { module.exports.index = function(req, res) {
@ -20,7 +21,7 @@ module.exports.index = function(req, res) {
], ],
function(err, result) { function(err, result) {
if (err !== null) if (err !== null)
console.log("[DBERROR] " + err); Log.dberror(err);
release(); release();
} }
); );

View File

@ -1,5 +1,6 @@
var pg = require('pg'); var pg = require('pg');
var secret = require('../../private'); var secret = require('../../private');
var Log = require('../../lib/NodeLog.js');
module.exports.index = function(req, res) { module.exports.index = function(req, res) {
@ -13,7 +14,7 @@ module.exports.index = function(req, res) {
], ],
function(err, result) { function(err, result) {
if (err !== null) if (err !== null)
console.log("[DBERROR] " + err); Log.dberror(err);
release(); release();
} }
); );

View File

@ -19,6 +19,7 @@ var bodyParser = require('body-parser');
var session = require('cookie-session'); var session = require('cookie-session');
var cookieParser = require('cookie-parser'); var cookieParser = require('cookie-parser');
var urls = require('./urls'); var urls = require('./urls');
var Log = require('./lib/NodeLog.js');
var isDev = app.get('env') === 'development'; var isDev = app.get('env') === 'development';
@ -37,13 +38,7 @@ app.use(bodyParser.json());
app.use(function(req, res, next) { app.use(function(req, res, next) {
// Log connection // Log connection
console.log( Log.request(req, res);
(isDev ? '\033[32m' : '') +
'[REQ] ' + new Date() + ' ' +
(req.headers['x-forwarded-for'] || req.connection.remoteAddress) +
' : ' + req.url +
(isDev ? '\033[0m' : '')
);
res.locals.title = "3DUI"; res.locals.title = "3DUI";
res.locals.urls = urls; res.locals.urls = urls;
@ -113,5 +108,5 @@ if ( isDev ) {
// Start server // Start server
http.listen(server_port, server_ip_address, function() { http.listen(server_port, server_ip_address, function() {
console.log("[READY] Now listening " + server_ip_address + ":" + server_port); Log.ready("Now listening " + server_ip_address + ":" + server_port);
}); });

View File

@ -1,23 +1,15 @@
var fs = require('fs'); var fs = require('fs');
var geo = require('./lib/geo.min.js'); var geo = require('./lib/geo.min.js');
var isDev = require('express')().get('env') === 'development'; var Log = require('./lib/NodeLog.js');
module.exports = function(io) { module.exports = function(io) {
io.on('connection', function(socket) { io.on('connection', function(socket) {
console.log( Log.socket.connection(socket);
(isDev ? '\033[33m' : '') +
'[SOK] ' + new Date() + ' ' + socket.handshake.address + ' connection' +
(isDev ? '\033[0m' : '')
);
socket.on('disconnect', function() { socket.on('disconnect', function() {
console.log( Log.socket.disconnect(socket);
(isDev ? '\033[34m' : '') +
'[SOK] ' + new Date() + ' ' + socket.handshake.address + ' disconnect' +
(isDev ? '\033[0m' : '')
);
}); });
var streamer = new geo.MeshStreamer(); var streamer = new geo.MeshStreamer();