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) {
console.log("Stashing message " + n + " at " + (oindex - 1));
tslog("Stashing message %d at %d", n, oindex - 1);
this.msgQ[oindex - 1] = data;
if (!this.Qtimeout) {
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;
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 () {
if (this.data.length == 0)
@ -199,7 +199,7 @@ function TermSession(game, user, files, dims) {
};
this.remove = function () {
delete sessions[ss.sessid];
console.log("Session " + this.sessid + " removed.");
tslog("Session %s removed.", this.sessid);
};
}
@ -219,6 +219,11 @@ function randkey() {
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. */
function getCookies(req) {
cookies = [];
@ -318,7 +323,7 @@ function login(req, res, formdata) {
var dims = [formdata["h"], formdata["w"]];
if (!(gname in games)) {
sendError(res, 2, "No such game: " + gname);
console.log("Request for nonexistant game \"" + gname + "\"");
tslog("Request for nonexistant game \"%s\"", gname);
return;
}
var progressdir = "/dgldir/inprogress-" + games[gname].uname;
@ -338,12 +343,12 @@ function login(req, res, formdata) {
nsession.h};
res.write(JSON.stringify(reply));
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);
}
else {
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) {
@ -351,11 +356,11 @@ function login(req, res, formdata) {
if (code != 0) {
sendError(res, 3);
if (code == 1)
console.log("Password check failed for user " + username);
tslog("Password check failed for user %s", username);
else if (code == 2)
console.log("Attempted login by nonexistent user " + username);
tslog("Attempted login by nonexistent user %s", username);
else
console.log("Login failed: sqlickrypt error " + code);
tslog("Login failed: sqlickrypt error %d", code);
return;
}
// check for an existing game
@ -365,7 +370,7 @@ function login(req, res, formdata) {
for (var i = 0; i < files.length; i++) {
if (files[i].match(fre)) {
sendError(res, 4, null);
console.log(username + " is already playing " + gname);
tslog("%s is already playing %s", username, gname);
return;
}
}
@ -415,22 +420,22 @@ function register(req, res, formdata) {
function checkreg(code, signal) {
if (code == 4) {
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) {
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) {
sendError(res, 0, null);
console.log("sqlickrypt register failed with code " + code);
tslog("sqlickrypt register failed with code %d", code);
}
else {
res.writeHead(200, {'Content-Type': 'text/plain'});
var reply = {"t": "r", "d": uname};
res.write(JSON.stringify(reply));
res.end();
console.log("Added new user: " + uname);
tslog("Added new user: %s", uname);
regsetup(uname);
}
}
@ -635,7 +640,7 @@ process.on("exit", function () {
if (sessions[sessid].alive)
sessions[sessid].child.kill('SIGHUP');
}
console.log("Quitting...");
tslog("Quitting...");
return;
});
@ -643,21 +648,21 @@ process.on("exit", function () {
process.env["TERM"] = "xterm-256color";
if (process.getuid() != 0) {
console.log("Not running as root, cannot chroot.");
tslog("Not running as root, cannot chroot.");
process.exit(1);
}
try {
process.chdir(chrootDir);
}
catch (err) {
console.log("Cannot enter " + chrootDir + " : " + err);
tslog("Cannot enter %s: %s", chrootDir, err);
process.exit(1);
}
try {
daemon.chroot(chrootDir);
}
catch (err) {
console.log("chroot to " + chrootDir + " failed: " + err);
tslog("chroot to %s failed: %s", chrootDir, err);
process.exit(1);
}
try {
@ -666,9 +671,9 @@ try {
process.setuid(dropToUID);
}
catch (err) {
console.log("Could not drop permissions: " + err);
tslog("Could not drop permissions: %s", err);
process.exit(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/');