From: Raphaƫl Jakse Date: Sat, 4 Apr 2020 14:57:44 +0000 (+0200) Subject: Introduce a flag for debug logging X-Git-Url: https://secure.softndesign.org/git/?a=commitdiff_plain;h=171e91acceaa500b7fe4022d999596853f0cd992;p=trivabble.git Introduce a flag for debug logging --- diff --git a/trivabble-server.js b/trivabble-server.js index 04c333b..eb133ea 100644 --- a/trivabble-server.js +++ b/trivabble-server.js @@ -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]); }); }