public
Created

  • Download Gist
gistfile1.txt
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
var Log = function () {
this.fs = require('fs');
this.initialized = false;
}
 
Log.prototype.setLogPathName = function (filename) {
this.filename = filename;
try {
this.fs.unlinkSync(this.filename);
}
catch (e) {
console.log(e);
}
var fd = this.fs.openSync(this.filename, "a+", 0666);
this.fs.writeSync(fd, "<html><head><style type=\"text/css\">" +
"p {white-space:nowrap; font-family: Courier New; font-size: 12px; line-height:20%;}" +
".time {color:orange;} .a1 {color:red;} .a2 {color:blue;} .a3 {color:green;}" +
"</style></head><body>");
this.fs.closeSync(fd);
this.initialized = true;
}
 
Log.prototype.logLine = function (msg) {
if (!this.initialized) {
console.log("You must call Log.setFullFilename() first.");
}
msg = msg.replace("{0}", "<span class=\"a1\">" + arguments[1] + "</span>");
msg = msg.replace("{1}", "<span class=\"a2\">" + arguments[2] + "</span>");
msg = msg.replace("{2}", "<span class=\"a3\">" + arguments[3] + "</span>");
var fd = this.fs.openSync(this.filename, "a+", 0666);
this.fs.writeSync(fd, "<p>" + "<span class=\"time\">" + getTime() + "</span>: " + msg + "</p>");
this.fs.closeSync(fd);
}
 
var getTime = function() {
var dTime = new Date();
var hour = dTime.getHours() + "";
if (hour.length == 1) { hour = "0" + hour; }
var minute = dTime.getMinutes() + "";
if (minute.length == 1) { minute = "0" + minute; }
var seconds = dTime.getSeconds() + "";
if (seconds.length == 1) { seconds = "0" + seconds; }
var ms = dTime.getMilliseconds() + "";
if (ms.length == 1) { ms = "00" + ms; }
else if (ms.length == 2) { ms = "0" + ms; }
return hour + ":" + minute + ":" + seconds + ":" + ms;
}
 
module.exports = new Log();

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.