From 22a85e8fc7e6aeb69fd306e6e5b898f42b1a6e77 Mon Sep 17 00:00:00 2001 From: Jermolene Date: Thu, 17 Jan 2019 15:20:24 +0000 Subject: [PATCH] Initial improvements --- core/modules/commands/fetch.js | 2 +- core/modules/commands/render.js | 2 +- core/modules/commands/save.js | 2 +- core/modules/server/server.js | 11 ++++++----- core/modules/utils/logger.js | 21 ++++++++++++++++++++- 5 files changed, 29 insertions(+), 9 deletions(-) diff --git a/core/modules/commands/fetch.js b/core/modules/commands/fetch.js index 8abd0b1bb..656308a3c 100644 --- a/core/modules/commands/fetch.js +++ b/core/modules/commands/fetch.js @@ -120,7 +120,7 @@ Command.prototype.fetchFile = function(url,options,callback,redirectCount) { } }); response.on("error",function(e) { - console.log("Error on GET request: " + e); + self.commander.log("Error on GET request: " + e); callback(e); }); }); diff --git a/core/modules/commands/render.js b/core/modules/commands/render.js index e9321c2b0..639fbb7c0 100644 --- a/core/modules/commands/render.js +++ b/core/modules/commands/render.js @@ -52,7 +52,7 @@ Command.prototype.execute = function() { var text = type === "text/html" ? container.innerHTML : container.textContent, filepath = path.resolve(self.commander.outputPath,wiki.filterTiddlers(filenameFilter,$tw.rootWidget,wiki.makeTiddlerIterator([title]))[0]); if(self.commander.verbose) { - console.log("Rendering \"" + title + "\" to \"" + filepath + "\""); + self.commander.log("Rendering \"" + title + "\" to \"" + filepath + "\""); } $tw.utils.createFileDirectories(filepath); fs.writeFileSync(filepath,text,"utf8"); diff --git a/core/modules/commands/save.js b/core/modules/commands/save.js index d8c44e1f3..1b03f267f 100644 --- a/core/modules/commands/save.js +++ b/core/modules/commands/save.js @@ -40,7 +40,7 @@ Command.prototype.execute = function() { contentTypeInfo = $tw.config.contentTypeInfo[type] || {encoding: "utf8"}, filepath = path.resolve(self.commander.outputPath,wiki.filterTiddlers(filenameFilter,$tw.rootWidget,wiki.makeTiddlerIterator([title]))[0]); if(self.commander.verbose) { - console.log("Saving \"" + title + "\" to \"" + filepath + "\""); + self.commander.log("Saving \"" + title + "\" to \"" + filepath + "\""); } $tw.utils.createFileDirectories(filepath); fs.writeFileSync(filepath,tiddler.fields.text,contentTypeInfo.encoding); diff --git a/core/modules/server/server.js b/core/modules/server/server.js index 02268d2a2..c30faef51 100644 --- a/core/modules/server/server.js +++ b/core/modules/server/server.js @@ -31,6 +31,8 @@ function Server(options) { this.authenticators = options.authenticators || []; this.wiki = options.wiki; this.servername = $tw.utils.transliterateToSafeASCII(this.wiki.getTiddlerText("$:/SiteTitle") || "TiddlyWiki5"); + this.logger = new $tw.utils.Logger("server",{colour: "cyan"}); + this.logger.setPrefix(":" + process.pid + "-" + (Number(new Date()) - 1095776640000)); // Initialise the variables this.variables = $tw.utils.extend({},this.defaultVariables); if(options.variables) { @@ -186,9 +188,9 @@ Server.prototype.requestHandler = function(request,response) { var route = self.findMatchingRoute(request,state); // Optionally output debug info if(self.get("debug-level") !== "none") { - console.log("Request path:",JSON.stringify(state.urlInfo)); - console.log("Request headers:",JSON.stringify(request.headers)); - console.log("authenticatedUsername:",state.authenticatedUsername); + self.logger.log("Request path:",JSON.stringify(state.urlInfo.href)); + self.logger.log("Request headers:",JSON.stringify(request.headers)); + self.logger.log("authenticatedUsername:",state.authenticatedUsername); } // Return a 404 if we didn't find a route if(!route) { @@ -239,8 +241,7 @@ Server.prototype.listen = function(port,host) { if(parseInt(port,10).toString() !== port) { port = process.env[port] || 8080; } - $tw.utils.log("Serving on " + this.protocol + "://" + host + ":" + port,"brown/orange"); - $tw.utils.log("(press ctrl-C to exit)","red"); + this.logger.log("Serving on " + this.protocol + "://" + host + ":" + port,"(press ctrl-C to exit)"); // Warn if required plugins are missing if(!$tw.wiki.getTiddler("$:/plugins/tiddlywiki/tiddlyweb") || !$tw.wiki.getTiddler("$:/plugins/tiddlywiki/filesystem")) { $tw.utils.warning("Warning: Plugins required for client-server operation (\"tiddlywiki/filesystem\" and \"tiddlywiki/tiddlyweb\") are missing from tiddlywiki.info file"); diff --git a/core/modules/utils/logger.js b/core/modules/utils/logger.js index ee14c5b2b..3b64865fb 100644 --- a/core/modules/utils/logger.js +++ b/core/modules/utils/logger.js @@ -21,15 +21,34 @@ function Logger(componentName,options) { options = options || {}; this.componentName = componentName || ""; this.colour = options.colour || "white"; + this.prefix = options.prefix || ""; this.enable = "enable" in options ? options.enable : true; } +/* +Change the output colour +*/ +Logger.prototype.setColour = function(colour) { + this.colour = colour || "white"; +}; + +/* +Change the prefix +*/ +Logger.prototype.setPrefix = function(prefix) { + this.prefix = prefix || ""; +}; + /* Log a message */ Logger.prototype.log = function(/* args */) { if(this.enable && console !== undefined && console.log !== undefined) { - return Function.apply.call(console.log, console, [$tw.utils.terminalColour(this.colour),this.componentName + ":"].concat(Array.prototype.slice.call(arguments,0)).concat($tw.utils.terminalColour())); + return Function.apply.call(console.log,console,[ + (new Date()).toISOString() + " [" + + this.componentName + this.prefix + "]" + + $tw.utils.terminalColour(this.colour) + ].concat(Array.prototype.slice.call(arguments,0)).concat($tw.utils.terminalColour())); } };