-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdebug.js
51 lines (42 loc) · 1.35 KB
/
debug.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
var stacktrace = require('stacktrace-js');
var createWriteStream = require('fs').createWriteStream;
var format = require('util').format;
var basename = require('path').basename;
var config = require('./config.js');
var enabled = config.LOG;
var logfile = config.LOGFILE;
var debug_console;
function create_logfile_console(logfile) {
var write_stream = createWriteStream(logfile);
return new console.Console(write_stream, write_stream);
}
function dummy() {
}
debug_console = logfile ? create_logfile_console(logfile) : console;
if(enabled) {
module.exports = function gen_logger(namespace) {
function logger() {
var now = Date();
var args = Array.prototype.slice.call(arguments);
var stack = stacktrace.getSync();
var log_format = "[%s/%s/%s %s:%s] %s";
var caller = stack[1]; /* stack[0] es logger */
var f = caller.functionName;
var file = basename(caller.fileName); /* Muestro únicamente el nombre del archivo */
var line = caller.lineNumber;
var args_format = args[0];
var args_args = args.slice(1);
var string = format(log_format, now, namespace, f, file, line, args_format);
if(args_args.length > 0) {
debug_console.log.apply(debug_console, [string].concat(args_args));
} else {
debug_console.log(string);
}
}
return logger;
};
} else {
module.exports = function dummy_logger() {
return dummy;
};
}