CyTube/lib/logger.js

62 lines
1.5 KiB
JavaScript
Raw Normal View History

var fs = require("graceful-fs");
var path = require("path");
2013-03-27 19:28:51 +00:00
function getTimeString() {
2013-05-04 16:43:38 +00:00
var d = new Date();
return d.toDateString() + " " + d.toTimeString().split(" ")[0];
2013-03-27 19:28:51 +00:00
}
var Logger = function(filename) {
this.filename = filename;
this.writer = fs.createWriteStream(filename, {
flags: "a",
encoding: "utf-8"
});
2013-03-27 19:28:51 +00:00
}
Logger.prototype.log = function () {
2013-07-29 23:32:51 +00:00
var msg = "";
for(var i in arguments)
msg += arguments[i];
if(this.dead) {
return;
}
2013-07-29 23:32:51 +00:00
var str = "[" + getTimeString() + "] " + msg + "\n";
try {
this.writer.write(str);
} catch(e) {
errlog.log("WARNING: Attempted logwrite failed: " + this.filename);
errlog.log("Message was: " + msg);
errlog.log(e);
}
2013-03-27 19:28:51 +00:00
}
Logger.prototype.close = function () {
try {
this.writer.end();
} catch(e) {
errlog.log("Log close failed: " + this.filename);
2013-07-29 22:16:11 +00:00
}
2013-03-27 19:28:51 +00:00
}
2014-01-28 00:37:48 +00:00
function makeConsoleLogger(filename) {
var log = new Logger(filename);
log._log = log.log;
log.log = function () {
console.log.apply(console, arguments);
this._log.apply(this, arguments);
}
return log;
}
var errlog = makeConsoleLogger(path.join(__dirname, "..", "error.log"));
var syslog = makeConsoleLogger(path.join(__dirname, "..", "sys.log"));
var eventlog = makeConsoleLogger(path.join(__dirname, "..", "events.log"));
2013-03-27 19:28:51 +00:00
exports.Logger = Logger;
exports.errlog = errlog;
exports.syslog = syslog;
2014-02-01 19:03:08 +00:00
exports.eventlog = eventlog;