Mercurial > hg > rlgwebd
comparison 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 |
comparison
equal
deleted
inserted
replaced
25:f275d816e857 | 26:9b58f8d3ea70 |
---|---|
127 clearTimeout(this.Qtimeout); | 127 clearTimeout(this.Qtimeout); |
128 this.Qtimeout = null; | 128 this.Qtimeout = null; |
129 } | 129 } |
130 } | 130 } |
131 else if (oindex > 0 && oindex <= 1024) { | 131 else if (oindex > 0 && oindex <= 1024) { |
132 console.log("Stashing message " + n + " at " + (oindex - 1)); | 132 tslog("Stashing message %d at %d", n, oindex - 1); |
133 this.msgQ[oindex - 1] = data; | 133 this.msgQ[oindex - 1] = data; |
134 if (!this.Qtimeout) { | 134 if (!this.Qtimeout) { |
135 var nextn = this.nrecv + this.msgQ.length + 1; | 135 var nextn = this.nrecv + this.msgQ.length + 1; |
136 this.Qtimeout = setTimeout(this.flushQ, 30000, this, nextn); | 136 this.Qtimeout = setTimeout(this.flushQ, 30000, this, nextn); |
137 } | 137 } |
161 /* Now set another timeout if necessary. */ | 161 /* Now set another timeout if necessary. */ |
162 if (session.msgQ.length != 0) { | 162 if (session.msgQ.length != 0) { |
163 var nextn = session.nrecv + session.msgQ.length + 1; | 163 var nextn = session.nrecv + session.msgQ.length + 1; |
164 session.Qtimeout = setTimeout(session.flushQ, 30000, session, nextn); | 164 session.Qtimeout = setTimeout(session.flushQ, 30000, session, nextn); |
165 } | 165 } |
166 console.log("Flushing queue for session " + session.sessid); | 166 tslog("Flushing queue for session %s", session.sessid); |
167 }; | 167 }; |
168 this.read = function () { | 168 this.read = function () { |
169 if (this.data.length == 0) | 169 if (this.data.length == 0) |
170 return null; | 170 return null; |
171 var pos = 0; | 171 var pos = 0; |
197 else | 197 else |
198 ss.remove(); | 198 ss.remove(); |
199 }; | 199 }; |
200 this.remove = function () { | 200 this.remove = function () { |
201 delete sessions[ss.sessid]; | 201 delete sessions[ss.sessid]; |
202 console.log("Session " + this.sessid + " removed."); | 202 tslog("Session %s removed.", this.sessid); |
203 }; | 203 }; |
204 } | 204 } |
205 | 205 |
206 /* A few utility functions */ | 206 /* A few utility functions */ |
207 function timestamp() { | 207 function timestamp() { |
215 rnum = Math.floor(Math.random() * 65536 * 65536); | 215 rnum = Math.floor(Math.random() * 65536 * 65536); |
216 hexstr = rnum.toString(16); | 216 hexstr = rnum.toString(16); |
217 while (hexstr.length < 8) | 217 while (hexstr.length < 8) |
218 hexstr = "0" + hexstr; | 218 hexstr = "0" + hexstr; |
219 return hexstr; | 219 return hexstr; |
220 } | |
221 | |
222 function tslog() { | |
223 arguments[0] = new Date().toISOString() + ": " + String(arguments[0]); | |
224 console.log.apply(console, arguments); | |
220 } | 225 } |
221 | 226 |
222 /* Returns a list of the cookies in the request, obviously. */ | 227 /* Returns a list of the cookies in the request, obviously. */ |
223 function getCookies(req) { | 228 function getCookies(req) { |
224 cookies = []; | 229 cookies = []; |
316 var password = formdata["pw"]; | 321 var password = formdata["pw"]; |
317 var gname = formdata["game"]; | 322 var gname = formdata["game"]; |
318 var dims = [formdata["h"], formdata["w"]]; | 323 var dims = [formdata["h"], formdata["w"]]; |
319 if (!(gname in games)) { | 324 if (!(gname in games)) { |
320 sendError(res, 2, "No such game: " + gname); | 325 sendError(res, 2, "No such game: " + gname); |
321 console.log("Request for nonexistant game \"" + gname + "\""); | 326 tslog("Request for nonexistant game \"%s\"", gname); |
322 return; | 327 return; |
323 } | 328 } |
324 var progressdir = "/dgldir/inprogress-" + games[gname].uname; | 329 var progressdir = "/dgldir/inprogress-" + games[gname].uname; |
325 | 330 |
326 // This sets up the game once starting is approved. | 331 // This sets up the game once starting is approved. |
336 res.writeHead(200, {'Content-Type': 'text/plain'}); | 341 res.writeHead(200, {'Content-Type': 'text/plain'}); |
337 var reply = {"t": "l", "id": nsession.sessid, "w": nsession.w, "h": | 342 var reply = {"t": "l", "id": nsession.sessid, "w": nsession.w, "h": |
338 nsession.h}; | 343 nsession.h}; |
339 res.write(JSON.stringify(reply)); | 344 res.write(JSON.stringify(reply)); |
340 res.end(); | 345 res.end(); |
341 console.log("%s playing %s (key %s, pid %d)", username, gname, | 346 tslog("%s playing %s (key %s, pid %d)", username, gname, |
342 nsession.sessid, nsession.child.pid); | 347 nsession.sessid, nsession.child.pid); |
343 } | 348 } |
344 else { | 349 else { |
345 sendError(res, 5, "Failed to open TTY"); | 350 sendError(res, 5, "Failed to open TTY"); |
346 console.log("Unable to allocate TTY for " + gname); | 351 tslog("Unable to allocate TTY for %s", gname); |
347 } | 352 } |
348 } | 353 } |
349 function checkit(code, signal) { | 354 function checkit(code, signal) { |
350 // check the password | 355 // check the password |
351 if (code != 0) { | 356 if (code != 0) { |
352 sendError(res, 3); | 357 sendError(res, 3); |
353 if (code == 1) | 358 if (code == 1) |
354 console.log("Password check failed for user " + username); | 359 tslog("Password check failed for user %s", username); |
355 else if (code == 2) | 360 else if (code == 2) |
356 console.log("Attempted login by nonexistent user " + username); | 361 tslog("Attempted login by nonexistent user %s", username); |
357 else | 362 else |
358 console.log("Login failed: sqlickrypt error " + code); | 363 tslog("Login failed: sqlickrypt error %d", code); |
359 return; | 364 return; |
360 } | 365 } |
361 // check for an existing game | 366 // check for an existing game |
362 fs.readdir(progressdir, function(err, files) { | 367 fs.readdir(progressdir, function(err, files) { |
363 if (!err) { | 368 if (!err) { |
364 var fre = RegExp("^" + username + ":"); | 369 var fre = RegExp("^" + username + ":"); |
365 for (var i = 0; i < files.length; i++) { | 370 for (var i = 0; i < files.length; i++) { |
366 if (files[i].match(fre)) { | 371 if (files[i].match(fre)) { |
367 sendError(res, 4, null); | 372 sendError(res, 4, null); |
368 console.log(username + " is already playing " + gname); | 373 tslog("%s is already playing %s", username, gname); |
369 return; | 374 return; |
370 } | 375 } |
371 } | 376 } |
372 } | 377 } |
373 // If progressdir isn't readable, start a new game anyway. | 378 // If progressdir isn't readable, start a new game anyway. |
413 else | 418 else |
414 email = formdata["email"]; | 419 email = formdata["email"]; |
415 function checkreg(code, signal) { | 420 function checkreg(code, signal) { |
416 if (code == 4) { | 421 if (code == 4) { |
417 sendError(res, 2, "Invalid characters in name or email."); | 422 sendError(res, 2, "Invalid characters in name or email."); |
418 console.log("Attempted registration: " + uname + " " + email); | 423 tslog("Attempted registration: %s %s", uname, email); |
419 } | 424 } |
420 else if (code == 1) { | 425 else if (code == 1) { |
421 sendError(res, 2, "Username " + uname + " is already being used."); | 426 sendError(res, 2, "Username " + uname + " is already being used."); |
422 console.log("Attempted duplicate registration: " + uname); | 427 tslog("Attempted duplicate registration: %s", uname); |
423 } | 428 } |
424 else if (code != 0) { | 429 else if (code != 0) { |
425 sendError(res, 0, null); | 430 sendError(res, 0, null); |
426 console.log("sqlickrypt register failed with code " + code); | 431 tslog("sqlickrypt register failed with code %d", code); |
427 } | 432 } |
428 else { | 433 else { |
429 res.writeHead(200, {'Content-Type': 'text/plain'}); | 434 res.writeHead(200, {'Content-Type': 'text/plain'}); |
430 var reply = {"t": "r", "d": uname}; | 435 var reply = {"t": "r", "d": uname}; |
431 res.write(JSON.stringify(reply)); | 436 res.write(JSON.stringify(reply)); |
432 res.end(); | 437 res.end(); |
433 console.log("Added new user: " + uname); | 438 tslog("Added new user: %s", uname); |
434 regsetup(uname); | 439 regsetup(uname); |
435 } | 440 } |
436 } | 441 } |
437 var child_adder = child_process.spawn("/bin/sqlickrypt", ["register"]); | 442 var child_adder = child_process.spawn("/bin/sqlickrypt", ["register"]); |
438 child_adder.on("exit", checkreg); | 443 child_adder.on("exit", checkreg); |
633 process.on("exit", function () { | 638 process.on("exit", function () { |
634 for (var sessid in sessions) { | 639 for (var sessid in sessions) { |
635 if (sessions[sessid].alive) | 640 if (sessions[sessid].alive) |
636 sessions[sessid].child.kill('SIGHUP'); | 641 sessions[sessid].child.kill('SIGHUP'); |
637 } | 642 } |
638 console.log("Quitting..."); | 643 tslog("Quitting..."); |
639 return; | 644 return; |
640 }); | 645 }); |
641 | 646 |
642 /* Initialization STARTS HERE */ | 647 /* Initialization STARTS HERE */ |
643 process.env["TERM"] = "xterm-256color"; | 648 process.env["TERM"] = "xterm-256color"; |
644 | 649 |
645 if (process.getuid() != 0) { | 650 if (process.getuid() != 0) { |
646 console.log("Not running as root, cannot chroot."); | 651 tslog("Not running as root, cannot chroot."); |
647 process.exit(1); | 652 process.exit(1); |
648 } | 653 } |
649 try { | 654 try { |
650 process.chdir(chrootDir); | 655 process.chdir(chrootDir); |
651 } | 656 } |
652 catch (err) { | 657 catch (err) { |
653 console.log("Cannot enter " + chrootDir + " : " + err); | 658 tslog("Cannot enter %s: %s", chrootDir, err); |
654 process.exit(1); | 659 process.exit(1); |
655 } | 660 } |
656 try { | 661 try { |
657 daemon.chroot(chrootDir); | 662 daemon.chroot(chrootDir); |
658 } | 663 } |
659 catch (err) { | 664 catch (err) { |
660 console.log("chroot to " + chrootDir + " failed: " + err); | 665 tslog("chroot to %s failed: %s", chrootDir, err); |
661 process.exit(1); | 666 process.exit(1); |
662 } | 667 } |
663 try { | 668 try { |
664 // drop gid first, that requires UID=0 | 669 // drop gid first, that requires UID=0 |
665 process.setgid(dropToGID); | 670 process.setgid(dropToGID); |
666 process.setuid(dropToUID); | 671 process.setuid(dropToUID); |
667 } | 672 } |
668 catch (err) { | 673 catch (err) { |
669 console.log("Could not drop permissions: " + err); | 674 tslog("Could not drop permissions: %s", err); |
670 process.exit(1); | 675 process.exit(1); |
671 } | 676 } |
672 | 677 |
673 http.createServer(handler).listen(8080, "127.0.0.1"); | 678 http.createServer(handler).listen(8080, "127.0.0.1"); |
674 console.log('rlgwebd running at http://127.0.0.1:8080/'); | 679 tslog('rlgwebd running at http://127.0.0.1:8080/'); |