diff rlgwebd.js @ 26:9b58f8d3ea70

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.
author John "Elwin" Edwards <elwin@sdf.org>
date Mon, 04 Jun 2012 10:19:36 -0700
parents 21de24c08aed
children 83f9a799a374
line wrap: on
line diff
--- a/rlgwebd.js	Sun Jun 03 18:26:11 2012 -0700
+++ b/rlgwebd.js	Mon Jun 04 10:19:36 2012 -0700
@@ -129,7 +129,7 @@
       }
     }
     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 @@
       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 @@
   };
   this.remove = function () {
     delete sessions[ss.sessid];
-    console.log("Session " + this.sessid + " removed.");
+    tslog("Session %s removed.", this.sessid);
   };
 }
 
@@ -219,6 +219,11 @@
   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 @@
   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 @@
                    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 @@
     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 @@
         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 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 @@
     if (sessions[sessid].alive)
       sessions[sessid].child.kill('SIGHUP');
   }
-  console.log("Quitting...");
+  tslog("Quitting...");
   return;
 });
 
@@ -643,21 +648,21 @@
 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 @@
   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/');