diff --git a/geo/MeshContainer.js b/geo/MeshContainer.js index c889cc5..29f1afa 100644 --- a/geo/MeshContainer.js +++ b/geo/MeshContainer.js @@ -1,3 +1,5 @@ +var Log = require('../lib/NodeLog.js'); + /** * Represents a mesh. All meshes are loaded once in geo.availableMesh to avoid * 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 = { diff --git a/lib/NodeLog.js b/lib/NodeLog.js new file mode 100644 index 0000000..8b588ab --- /dev/null +++ b/lib/NodeLog.js @@ -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; diff --git a/posts/arrow-clicked/index.js b/posts/arrow-clicked/index.js index 9fb61cc..84f86ed 100644 --- a/posts/arrow-clicked/index.js +++ b/posts/arrow-clicked/index.js @@ -1,5 +1,6 @@ var pg = require('pg'); var secret = require('../../private'); +var Log = require('../../lib/NodeLog.js'); 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], function(err, result) { if (err !== null) - console.log("[DBERROR] " + err); + Log.dberror(err); release(); } ); diff --git a/posts/coin-clicked/index.js b/posts/coin-clicked/index.js index 0ae7c37..ba9e7eb 100644 --- a/posts/coin-clicked/index.js +++ b/posts/coin-clicked/index.js @@ -1,5 +1,6 @@ var pg = require('pg'); var secret = require('../../private'); +var Log = require('../../lib/NodeLog.js'); 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], function(err, result) { if (err !== null) - console.log("[DBERROR] " + err); + Log.dberror(err); release(); } ); diff --git a/posts/fps/index.js b/posts/fps/index.js index a0344ae..c885754 100644 --- a/posts/fps/index.js +++ b/posts/fps/index.js @@ -1,5 +1,6 @@ var pg = require('pg'); var secret = require('../../private'); +var Log = require('../../lib/NodeLog.js'); module.exports.index = function(req, res) { @@ -9,7 +10,7 @@ module.exports.index = function(req, res) { [req.session.exp_id, req.body.fps], function(err, result) { if (err !== null) - console.log("[DBERROR] " + err); + Log.dberror(err); release(); } ); diff --git a/posts/hovered/index.js b/posts/hovered/index.js index 2ea2735..dbf6bd0 100644 --- a/posts/hovered/index.js +++ b/posts/hovered/index.js @@ -1,5 +1,6 @@ var pg = require('pg'); var secret = require('../../private'); +var Log = require('../../lib/NodeLog.js'); module.exports.index = function(req, res) { @@ -15,7 +16,7 @@ module.exports.index = function(req, res) { ], function(err, result) { if (err !== null) - console.log("[DBERROR] " + err); + Log.dberror(err); release(); } ); diff --git a/posts/keyboard-event/index.js b/posts/keyboard-event/index.js index 122eaae..1dad574 100644 --- a/posts/keyboard-event/index.js +++ b/posts/keyboard-event/index.js @@ -1,5 +1,6 @@ var pg = require('pg'); var secret = require('../../private'); +var Log = require('../../lib/NodeLog.js'); module.exports.index = function(req, res) { @@ -21,7 +22,7 @@ module.exports.index = function(req, res) { ], function(err, result) { if (err !== null) - console.log("[DBERROR] " + err); + Log.dberror(err); release(); } ); diff --git a/posts/previous-next-clicked/index.js b/posts/previous-next-clicked/index.js index 59c8fae..2637366 100644 --- a/posts/previous-next-clicked/index.js +++ b/posts/previous-next-clicked/index.js @@ -1,5 +1,6 @@ var pg = require('pg'); var secret = require('../../private'); +var Log = require('../../lib/NodeLog.js'); module.exports.index = function(req, res) { @@ -20,7 +21,7 @@ module.exports.index = function(req, res) { ], function(err, result) { if (err !== null) - console.log("[DBERROR] " + err); + Log.dberror(err); release(); } ); diff --git a/posts/reset-clicked/index.js b/posts/reset-clicked/index.js index 7845735..955daf9 100644 --- a/posts/reset-clicked/index.js +++ b/posts/reset-clicked/index.js @@ -1,5 +1,6 @@ var pg = require('pg'); var secret = require('../../private'); +var Log = require('../../lib/NodeLog.js'); module.exports.index = function(req, res) { @@ -13,7 +14,7 @@ module.exports.index = function(req, res) { ], function(err, result) { if (err !== null) - console.log("[DBERROR] " + err); + Log.dberror(err); release(); } ); diff --git a/server.js b/server.js index 553f049..0b2b514 100644 --- a/server.js +++ b/server.js @@ -19,6 +19,7 @@ var bodyParser = require('body-parser'); var session = require('cookie-session'); var cookieParser = require('cookie-parser'); var urls = require('./urls'); +var Log = require('./lib/NodeLog.js'); var isDev = app.get('env') === 'development'; @@ -37,13 +38,7 @@ app.use(bodyParser.json()); app.use(function(req, res, next) { // Log connection - console.log( - (isDev ? '\033[32m' : '') + - '[REQ] ' + new Date() + ' ' + - (req.headers['x-forwarded-for'] || req.connection.remoteAddress) + - ' : ' + req.url + - (isDev ? '\033[0m' : '') - ); + Log.request(req, res); res.locals.title = "3DUI"; res.locals.urls = urls; @@ -113,5 +108,5 @@ if ( isDev ) { // Start server 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); }); diff --git a/socket.js b/socket.js index a408fb6..8a246a3 100644 --- a/socket.js +++ b/socket.js @@ -1,23 +1,15 @@ var fs = require('fs'); var geo = require('./lib/geo.min.js'); -var isDev = require('express')().get('env') === 'development'; +var Log = require('./lib/NodeLog.js'); module.exports = function(io) { io.on('connection', function(socket) { - console.log( - (isDev ? '\033[33m' : '') + - '[SOK] ' + new Date() + ' ' + socket.handshake.address + ' connection' + - (isDev ? '\033[0m' : '') - ); + Log.socket.connection(socket); socket.on('disconnect', function() { - console.log( - (isDev ? '\033[34m' : '') + - '[SOK] ' + new Date() + ' ' + socket.handshake.address + ' disconnect' + - (isDev ? '\033[0m' : '') - ); + Log.socket.disconnect(socket); }); var streamer = new geo.MeshStreamer();