-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.js
84 lines (73 loc) · 2.17 KB
/
logger.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
/**
* Name: logger.js
* Desc: The logging and analytics system portion of the Creative Commits
* Twitter bot.
* Auth: Cezary Wojcik
*/
// ---- [ includes ] ----------------------------------------------------------
var fs = require("fs");
var mkdirp = require("mkdirp");
var settings = require("./settings.js");
// ---- [ misc ] --------------------------------------------------------------
var days = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
];
var lastMessageLogged = "";
// ---- [ private functions ] -------------------------------------------------
function ensureLogDirExists() {
if (!fs.existsSync(settings.logDir)) {
mkdirp(settings.logDir, function(e) {
if (e !== null) {
console.log("-ERROR creating log dir: " + e);
}
})
}
}
function ensureFileExists(path, defaultString) {
if (!fs.existsSync(path)) {
fs.writeFile(path, defaultString, function(e) {
if (e !== null) {
console.log("-ERROR creating log file: " + e);
}
});
}
}
// ---- [ exported functions ] ------------------------------------------------
exports.initLogger = function() {
ensureLogDirExists();
ensureFileExists(settings.logDir + settings.logFile,
"day,hour,minute,numfucks\n");
ensureFileExists(settings.logDir + settings.messagesFile, "");
}
exports.logCommit = function(dateString, message) {
if (message === lastMessageLogged) {
return;
}
var fucks = message.toLowerCase().match(/fuck/g);
if (fucks !== null) {
lastMessageLogged = message;
console.log("-Logging " + fucks.length + " fuck" + (fucks.length > 1
? "s" : "") + ".");
fs.appendFile(settings.logDir + settings.messagesFile, message + "\n",
function(e) {
if (e !== null) {
console.log("-ERROR writing to messages file: " + e);
}
});
var date = new Date(dateString);
fs.appendFile(settings.logDir + settings.logFile,
days[date.getDay()] + "," + date.getHours() + "," + date.getMinutes()
+ "," + fucks.length + "\n",
function(e) {
if (e !== null) {
console.log("-ERROR writing to log file: " + e);
}
});
}
};