rlgwebd.js: add timestamps to log messages.

Wrap console.log() with the tslog() function, which prepends timestamps.
Clean up some of the messages as well.
This commit is contained in:
John "Elwin" Edwards 2012-06-04 10:19:36 -07:00
parent 6ed3b7c2ce
commit 8599ec081d

View file

@ -129,7 +129,7 @@ function TermSession(game, user, files, dims) {
} }
} }
else if (oindex > 0 && oindex <= 1024) { else if (oindex > 0 && oindex <= 1024) {
console.log("Stashing message " + n + " at " + (oindex - 1)); tslog("Stashing message %d at %d", n, oindex - 1);
this.msgQ[oindex - 1] = data; this.msgQ[oindex - 1] = data;
if (!this.Qtimeout) { if (!this.Qtimeout) {
var nextn = this.nrecv + this.msgQ.length + 1; var nextn = this.nrecv + this.msgQ.length + 1;
@ -163,7 +163,7 @@ function TermSession(game, user, files, dims) {
var nextn = session.nrecv + session.msgQ.length + 1; var nextn = session.nrecv + session.msgQ.length + 1;
session.Qtimeout = setTimeout(session.flushQ, 30000, session, nextn); session.Qtimeout = setTimeout(session.flushQ, 30000, session, nextn);
} }
console.log("Flushing queue for session " + session.sessid); tslog("Flushing queue for session %s", session.sessid);
}; };
this.read = function () { this.read = function () {
if (this.data.length == 0) if (this.data.length == 0)
@ -199,7 +199,7 @@ function TermSession(game, user, files, dims) {
}; };
this.remove = function () { this.remove = function () {
delete sessions[ss.sessid]; delete sessions[ss.sessid];
console.log("Session " + this.sessid + " removed."); tslog("Session %s removed.", this.sessid);
}; };
} }
@ -219,6 +219,11 @@ function randkey() {
return hexstr; return hexstr;
} }
function tslog() {
arguments[0] = new Date().toISOString() + ": " + String(arguments[0]);
console.log.apply(console, arguments);
}
/* Returns a list of the cookies in the request, obviously. */ /* Returns a list of the cookies in the request, obviously. */
function getCookies(req) { function getCookies(req) {
cookies = []; cookies = [];
@ -318,7 +323,7 @@ function login(req, res, formdata) {
var dims = [formdata["h"], formdata["w"]]; var dims = [formdata["h"], formdata["w"]];
if (!(gname in games)) { if (!(gname in games)) {
sendError(res, 2, "No such game: " + gname); sendError(res, 2, "No such game: " + gname);
console.log("Request for nonexistant game \"" + gname + "\""); tslog("Request for nonexistant game \"%s\"", gname);
return; return;
} }
var progressdir = "/dgldir/inprogress-" + games[gname].uname; var progressdir = "/dgldir/inprogress-" + games[gname].uname;
@ -338,12 +343,12 @@ function login(req, res, formdata) {
nsession.h}; nsession.h};
res.write(JSON.stringify(reply)); res.write(JSON.stringify(reply));
res.end(); res.end();
console.log("%s playing %s (key %s, pid %d)", username, gname, tslog("%s playing %s (key %s, pid %d)", username, gname,
nsession.sessid, nsession.child.pid); nsession.sessid, nsession.child.pid);
} }
else { else {
sendError(res, 5, "Failed to open TTY"); sendError(res, 5, "Failed to open TTY");
console.log("Unable to allocate TTY for " + gname); tslog("Unable to allocate TTY for %s", gname);
} }
} }
function checkit(code, signal) { function checkit(code, signal) {
@ -351,11 +356,11 @@ function login(req, res, formdata) {
if (code != 0) { if (code != 0) {
sendError(res, 3); sendError(res, 3);
if (code == 1) if (code == 1)
console.log("Password check failed for user " + username); tslog("Password check failed for user %s", username);
else if (code == 2) else if (code == 2)
console.log("Attempted login by nonexistent user " + username); tslog("Attempted login by nonexistent user %s", username);
else else
console.log("Login failed: sqlickrypt error " + code); tslog("Login failed: sqlickrypt error %d", code);
return; return;
} }
// check for an existing game // check for an existing game
@ -365,7 +370,7 @@ function login(req, res, formdata) {
for (var i = 0; i < files.length; i++) { for (var i = 0; i < files.length; i++) {
if (files[i].match(fre)) { if (files[i].match(fre)) {
sendError(res, 4, null); sendError(res, 4, null);
console.log(username + " is already playing " + gname); tslog("%s is already playing %s", username, gname);
return; return;
} }
} }
@ -415,22 +420,22 @@ function register(req, res, formdata) {
function checkreg(code, signal) { function checkreg(code, signal) {
if (code == 4) { if (code == 4) {
sendError(res, 2, "Invalid characters in name or email."); sendError(res, 2, "Invalid characters in name or email.");
console.log("Attempted registration: " + uname + " " + email); tslog("Attempted registration: %s %s", uname, email);
} }
else if (code == 1) { else if (code == 1) {
sendError(res, 2, "Username " + uname + " is already being used."); sendError(res, 2, "Username " + uname + " is already being used.");
console.log("Attempted duplicate registration: " + uname); tslog("Attempted duplicate registration: %s", uname);
} }
else if (code != 0) { else if (code != 0) {
sendError(res, 0, null); sendError(res, 0, null);
console.log("sqlickrypt register failed with code " + code); tslog("sqlickrypt register failed with code %d", code);
} }
else { else {
res.writeHead(200, {'Content-Type': 'text/plain'}); res.writeHead(200, {'Content-Type': 'text/plain'});
var reply = {"t": "r", "d": uname}; var reply = {"t": "r", "d": uname};
res.write(JSON.stringify(reply)); res.write(JSON.stringify(reply));
res.end(); res.end();
console.log("Added new user: " + uname); tslog("Added new user: %s", uname);
regsetup(uname); regsetup(uname);
} }
} }
@ -635,7 +640,7 @@ process.on("exit", function () {
if (sessions[sessid].alive) if (sessions[sessid].alive)
sessions[sessid].child.kill('SIGHUP'); sessions[sessid].child.kill('SIGHUP');
} }
console.log("Quitting..."); tslog("Quitting...");
return; return;
}); });
@ -643,21 +648,21 @@ process.on("exit", function () {
process.env["TERM"] = "xterm-256color"; process.env["TERM"] = "xterm-256color";
if (process.getuid() != 0) { if (process.getuid() != 0) {
console.log("Not running as root, cannot chroot."); tslog("Not running as root, cannot chroot.");
process.exit(1); process.exit(1);
} }
try { try {
process.chdir(chrootDir); process.chdir(chrootDir);
} }
catch (err) { catch (err) {
console.log("Cannot enter " + chrootDir + " : " + err); tslog("Cannot enter %s: %s", chrootDir, err);
process.exit(1); process.exit(1);
} }
try { try {
daemon.chroot(chrootDir); daemon.chroot(chrootDir);
} }
catch (err) { catch (err) {
console.log("chroot to " + chrootDir + " failed: " + err); tslog("chroot to %s failed: %s", chrootDir, err);
process.exit(1); process.exit(1);
} }
try { try {
@ -666,9 +671,9 @@ try {
process.setuid(dropToUID); process.setuid(dropToUID);
} }
catch (err) { catch (err) {
console.log("Could not drop permissions: " + err); tslog("Could not drop permissions: %s", err);
process.exit(1); process.exit(1);
} }
http.createServer(handler).listen(8080, "127.0.0.1"); http.createServer(handler).listen(8080, "127.0.0.1");
console.log('rlgwebd running at http://127.0.0.1:8080/'); tslog('rlgwebd running at http://127.0.0.1:8080/');