This repository has been archived by the owner on Jul 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
63 lines (58 loc) · 1.81 KB
/
utils.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
52
53
54
55
56
57
58
59
60
61
62
63
var utils = exports, crypto = require('crypto'), config = require('./config.js');
// ansi terminal colors
utils.colors = {
ok : '\033[32m', // green
info : '\033[36m', // blue
warn : '\033[33m', // yellow
error : '\033[31m', // red
low : '\033[2m', // grey, not often supported
reset : '\033[0m' // reset
}
// shortcut to terminal colors based on nap type
utils.napcolors = {
INF : utils.colors.info,
TAP : utils.colors.warn,
CAP : utils.colors.warn,
ERR : utils.colors.error
}
// log string to console with datetime stamp. if in development environment, log
// any included data object too
utils.log = function(str, data) {
var fs = require('fs');
fs.open("error.log", 'a+');
if (config.dev && data)
console.log();
console.log(utils.colors.low + (new Date()).toISOString() + ' - '
+ utils.colors.reset + str);
fs.appendFile("error.log", utils.colors.low + (new Date()).toISOString()
+ ' - ' + utils.colors.reset + str + '\n', function(err) {
if (err)
console.log("ERROR");
});
if (config.dev && data) {
console.log(data);
fs.appendFile("error.log", data.toString() + '\n', function(err) {
if (err)
console.log("ERROR");
});
console.log();
}
}
// enhanced text logging with message type coloring
utils.logText = function(text, TYPE, color) {
if (!TYPE)
TYPE = 'INF';
if (!color)
color = utils.napcolors[TYPE] || utils.colors.info;
utils.log(color + TYPE + utils.colors.reset + ' ' + text);
}
// enhanced nap packet logging with message type coloring and package
// identification based on hash
utils.logPacket = function(packet, TYPE, text, hash) {
if (!hash)
hash = crypto.createHash('sha1').update(JSON.stringify(packet)).digest(
'hex');
utils.log((utils.napcolors[TYPE] ? utils.napcolors[TYPE] : '') + TYPE
+ utils.colors.reset + ' ' + hash.substring(0, 6) + ' ' + text,
packet);
}