Introduce a flag for debug logging
authorRaphaël Jakse <raphael.jakse@univ-grenoble-alpes.fr>
Sat, 4 Apr 2020 14:57:44 +0000 (16:57 +0200)
committerRaphaël Jakse <raphael.jakse@univ-grenoble-alpes.fr>
Sat, 4 Apr 2020 14:57:44 +0000 (16:57 +0200)
trivabble-server.js

index 04c333b8d1d2afa1cad782dc259ef0b56099e7e1..eb133ea90fa58287446b94f00b7161e1fded7728 100644 (file)
@@ -31,12 +31,19 @@ const port = parseInt(process.env.TRIVABBLE_PORT || "3000");
 const SAVE_TIMEOUT = 5000;
 const KEEP_ALIVE = 30000;
 
-const DEV_ENABLE_SERVING_FILES = (process.env.DEV_ENABLE_SERVING_FILES || "").toLowerCase() === "true";
+function envTrue(name) {
+    return (process.env[name] || "").toLowerCase() === "true";
+}
+
+const DEV_ENABLE_SERVING_FILES = envTrue('DEV_ENABLE_SERVING_FILES');
+const DEBUG_LOG = DEV_ENABLE_SERVING_FILES || envTrue('DEBUG_LOG');
 
 if (DEV_ENABLE_SERVING_FILES) {
     console.log("DEV_ENABLE_SERVING_FILES: Serving files in the current directory. Please never do this on a production server, this is for development purposes only.");
 }
 
+var debuglog = DEBUG_LOG ? console.log.bind(console) : () => null;
+
 var http = require("http");
 var fs   = require("fs");
 
@@ -569,7 +576,7 @@ function handleCommand(cmd, gameNumber, playerName, response) {
         var initialRackCount = countTiles(rack);
         var rackCount = countTiles(cmd.rack);
 
-        console.log(rack, cmd.rack);
+        debuglog(rack, cmd.rack);
 
         if (initialRackCount !== rackCount) {
             response.write(
@@ -680,7 +687,7 @@ function handleCommands(cmds, responseAndIsES) {
     response.end("]");
 
     if (games[cmds.gameNumber]) {
-        console.log("pendingEvents COMMIT", games[cmds.gameNumber].pendingEvents);
+        debuglog("pendingEvents COMMIT", games[cmds.gameNumber].pendingEvents);
         games[cmds.gameNumber].commit();
     }
 }
@@ -704,7 +711,7 @@ function handleRequest(request, response) {
                 request.url = "/index.html";
             }
 
-            console.log("Serving " + request.url);
+            debuglog("Serving " + request.url);
 
             fs.exists("." + request.url, function (exists) {
                 if (exists) {
@@ -756,7 +763,7 @@ function handleRequest(request, response) {
         }
 
         dateNow = Date.now();
-        console.log("RECEIVED", request.url, post, isEventSource);
+        debuglog("RECEIVED", request.url, post, isEventSource);
         handleCommands(post && JSON.parse(post), [response, isEventSource]);
     });
 }