-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.js
128 lines (81 loc) · 2.35 KB
/
log.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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
var fs = require('fs'),
log = exports,
dir = './logs';
log.setDir = function (directory) {
if (require('path').existsSync(directory)) {
dir = directory;
}
else {
fs.mkdir(directory, null, function () {
console.log('-- Given log directory is invalid and could not ' +
'be created. Will use default.');
});
}
};
log.publicMsgOut = function (params) {
var now = getTime(),
path = dir + '/' + params.channel + '.log',
line = now + '| <' + params.sender + '> ' +
params.message;
append(path, line);
};
log.publicMsgIn = function (params) {
var now = getTime(),
path = dir + '/' + params.channel + '.log',
line = now + '| <' + params.sender + '> ' +
params.message;
append(path, line);
};
log.queryOut = function (params) {
var now = getTime(),
path = dir + '/' + params.destination + '.log',
line = now + '| <' + params.sender + '> ' + params.message;
append(path, line);
};
log.queryIn = function (params) {
var now = getTime(),
path = dir + '/' + params.sender + '.log',
line = now + '| <' + params.sender + '> ' + params.message;
append(path, line);
};
log.debug = function (params) {
var now = getTime(),
path = './debug.log',
line = now + '| ' + params.message;
console.log('-- ' + params.message);
append(path, line);
}
log.joined = function (params) {
var now = getTime(),
path = dir + '/' + params.channel + '.log',
line = now + '| ' + params.who + ' joined channel ' +
params.channel + '.';
append(path, line);
}
log.parted = function (params) {
var now = getTime(),
path = dir + '/' + params.channel + '.log',
line = now + '| ' + params.who + ' parted channel ' +
params.channel + '.';
append(path, line);
}
log.renamed = function (params) {
var now = getTime(),
path = dir + '/' + params.channel + '.log',
line = now + '| ' + params.old + ' changed nick to ' +
params.new_ + '.';
append(path, line);
}
function getTime() {
return new Date().toString().match(/[^G]+/);
}
function append(path, line) {
fs.open(path, 'a', 438, function (err, fd) {
if (err) {
console.log('There was a problem opening the log ' + path + '.');
}
fs.write(fd, line + '\n', null, 'utf8', function () {
fs.close(fd);
});
});
}