forked from starak/node-console-stamp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
97 lines (73 loc) · 2.58 KB
/
Copy pathmain.js
File metadata and controls
97 lines (73 loc) · 2.58 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*jshint node:true, bitwise:false */
/**
*
* Node Console stamp by Ståle Raknes
*
*/
"use strict";
var dateFormat = require( "dateformat" );
var merge = require( "merge" );
var colors = require( "colors" );
var defaults = require( "./defaults.json" );
var util = require( 'util' );
module.exports = function ( con, options, prefix_metadata ) {
// If the console is patched already, restore it
if ( con.__ts__ && "restoreConsole" in con ) {
con.restoreConsole();
}
var pattern;
if ( typeof options === "string" ) {
// Fallback to version 0.1.x
pattern = options;
options = merge( {}, defaults );
} else {
options = merge( {}, defaults, (options || {}) );
pattern = options.pattern;
prefix_metadata = prefix_metadata || options.metadata;
}
options.include = options.include.filter( function filter( m ) {
return !~options.exclude.indexOf( m );
} );
// Set the color theme
colors.setTheme( options.colors );
var original_functions = [];
var slice = Array.prototype.slice;
options.include.forEach( function ( f ) {
original_functions.push( [f, con[f]] );
var org = con[f];
con[f] = function () {
var prefix = ("[" + dateFormat( pattern ) + "]").stamp + " ";
var args = slice.call( arguments );
// Add label if flag is set
if ( options.label ) {
prefix += ("[" + f.toUpperCase() + "]").label + " ".substr( f.length );
}
// Add metadata if any
var metadata = "";
if ( typeof prefix_metadata === 'function' ) {
metadata = prefix_metadata( f, args );
} else if ( typeof prefix_metadata === 'object' ) {
metadata = util.inspect( prefix_metadata );
} else if ( typeof prefix_metadata !== 'undefined' ) {
metadata = prefix_metadata;
}
if ( metadata ) {
prefix += metadata.metadata + " ";
}
if ( f === "error" || f === "warn" || ( f === "assert" && !args[0] ) ) {
process.stderr.write( prefix );
} else if ( f !== "assert" ) {
process.stdout.write( prefix );
}
return org.apply( con, args );
};
} );
con.restoreConsole = function () {
original_functions.forEach( function ( pair ) {
con[pair[0]] = pair[1];
delete con.__ts__;
} );
delete con.restoreConsole;
};
con.__ts__ = true;
};