-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheventHandler.js
67 lines (56 loc) · 2.07 KB
/
eventHandler.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
/**
* Event Handler
*/
var fs = require('fs');
var util = require('util');
module.exports = {
directory: ".",
callbacks: {},
//Send to the callbacks
sendCallback: function (event, arg, domain) {
if (this.callbacks[event] === undefined) { return; }
//Add to log file
var log = fs.createWriteStream(this.directory + '/logs.txt', { flags: 'a' });
var date = new Date();
var dateString = date.getDay() + "/" + date.getMonth() + "/" + date.getFullYear() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
log.write(dateString + " => " + "[" + event() + "][" + domain + "] - " + arg + "\n");
console.log = function (d) {
log.write(dateString + " => " + "[console] - " + util.format(d) + "\n");
process.stdout.write(util.format(d) + "\n");
}
for (var i = 0; i < this.callbacks[event].length; i++) {
this.callbacks[event][i](arg, domain);
}
},
//Add a callback
on: function (event, callback) {
if (this.callbacks[event] === undefined) { this.callbacks[event] = []; }
this.callbacks[event].push(callback);
},
//Remove a callback
remove: function (event, callback) {
for (var i in this.callbacks) {
if (i == event) {
for (var j = 0; j < this.callbacks[event].length; j++) {
if (this.callbacks[event][j] == callback) {
this.callbacks[event].splice(j, 1);
}
}
}
}
},
/////////////////////////////////
//Specific calls
error: function (error, domain) {
if (error === undefined) { return "error"; }
this.sendCallback(this.error, error, domain);
},
info: function (info, domain) {
if (info === undefined) { return "info"; }
this.sendCallback(this.info, info, domain);
},
warning: function (warn, domain) {
if (warn === undefined) { return "warn"; }
this.sendCallback(this.warning, warn, domain);
}
}