From 3f9561dd95b689645c3d7e3f7a7176c3d912b084 Mon Sep 17 00:00:00 2001 From: Jermolene Date: Sun, 26 Jan 2014 18:53:31 +0000 Subject: [PATCH] Better logging --- boot/boot.js | 9 ---- core/modules/startup.js | 7 ++- core/modules/syncer.js | 16 ++----- core/modules/utils/logger.js | 43 +++++++++++++++++++ .../filesystem/filesystemadaptor.js | 10 ++--- 5 files changed, 57 insertions(+), 28 deletions(-) create mode 100644 core/modules/utils/logger.js diff --git a/boot/boot.js b/boot/boot.js index 9358ce1d7..9310e464b 100644 --- a/boot/boot.js +++ b/boot/boot.js @@ -46,15 +46,6 @@ if($tw.node) { /////////////////////////// Utility functions -/* -Log a message -*/ -$tw.utils.log = function(/* args */) { - if(console !== undefined && console.log !== undefined) { - return Function.apply.call(console.log, console, arguments); - } -}; - /* Check if an object has a property */ diff --git a/core/modules/startup.js b/core/modules/startup.js index a1fe1b537..463a8dbd4 100755 --- a/core/modules/startup.js +++ b/core/modules/startup.js @@ -16,10 +16,13 @@ var widget = require("$:/core/modules/widgets/widget.js"); exports.startup = function() { var modules,n,m,f,commander; - // Load modules + // Load utility modules and initialise the logger + $tw.modules.applyMethods("utils",$tw.utils); + $tw.logger = new $tw.utils.Logger(); + $tw.log = $tw.logger.log; + // Load other modules $tw.modules.applyMethods("global",$tw); $tw.modules.applyMethods("config",$tw.config); - $tw.modules.applyMethods("utils",$tw.utils); if($tw.browser) { $tw.utils.getBrowserInfo($tw.browser); } diff --git a/core/modules/syncer.js b/core/modules/syncer.js index 4c68e26ce..4e2bd3019 100644 --- a/core/modules/syncer.js +++ b/core/modules/syncer.js @@ -19,6 +19,8 @@ wiki: wiki to be synced function Syncer(options) { var self = this; this.wiki = options.wiki; + // Make a logger + this.log = $tw.logger.makeLog("syncer"); // Find a working syncadaptor this.syncadaptor = undefined; $tw.modules.forEachModuleOfType("syncadaptor",function(title,module) { @@ -37,17 +39,7 @@ Error handling */ Syncer.prototype.showError = function(error) { alert("Syncer error: " + error); - $tw.utils.log("Syncer error: " + error); -}; - -/* -Message logging -*/ -Syncer.prototype.log = function(/* arguments */) { - var args = Array.prototype.slice.call(arguments,0); - args[0] = "Syncer: " + args[0]; - // Temporarily disable logging to help the wood vs. trees situation; we need better filtering of log messages - //$tw.utils.log.apply(null,args); + this.log("Syncer error: " + error); }; /* @@ -384,7 +376,7 @@ Syncer.prototype.processTaskQueue = function() { // Dispatch the task this.dispatchTask(task,function(err) { if(err) { - console.log("Sync error while processing '" + task.title + "':\n" + err); + self.showError("Sync error while processing '" + task.title + "':\n" + err); } // Mark that this task is no longer in progress delete self.taskInProgress[task.title]; diff --git a/core/modules/utils/logger.js b/core/modules/utils/logger.js new file mode 100644 index 000000000..90afcb38b --- /dev/null +++ b/core/modules/utils/logger.js @@ -0,0 +1,43 @@ +/*\ +title: $:/core/modules/utils/logger.js +type: application/javascript +module-type: utils + +A basic logging implementation + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +/* +Make a new logger +*/ +function Logger() { + +} + +/* +Make a log function for a particular component +*/ +Logger.prototype.makeLog = function(componentName) { + var self = this; + return function(/* args */) { + self.log.apply(self.log,[componentName + ":"].concat(Array.prototype.slice.call(arguments,0))); + }; +}; + +/* +Log a message +*/ +Logger.prototype.log = function(/* args */) { + if(console !== undefined && console.log !== undefined) { + return Function.apply.call(console.log, console, arguments); + } +}; + +exports.Logger = Logger; + +})(); diff --git a/plugins/tiddlywiki/filesystem/filesystemadaptor.js b/plugins/tiddlywiki/filesystem/filesystemadaptor.js index e074aa01c..c80cbb0a1 100644 --- a/plugins/tiddlywiki/filesystem/filesystemadaptor.js +++ b/plugins/tiddlywiki/filesystem/filesystemadaptor.js @@ -21,12 +21,12 @@ function FileSystemAdaptor(syncer) { this.syncer = syncer; this.watchers = {}; this.pending = {}; - + this.log = $tw.logger.makeLog("FileSystem"); this.setwatcher = function(filename, title) { return undefined; return this.watchers[filename] = this.watchers[filename] || fs.watch(filename, {persistent: false}, function(e) { - console.log("Filesystem:", e, filename); + self.log("Error:",e,filename); if(e === "change") { var tiddlers = $tw.loadTiddlersFromFile(filename).tiddlers; for(var t in tiddlers) { @@ -161,7 +161,7 @@ FileSystemAdaptor.prototype.saveTiddler = function(tiddler,callback) { if(err) { return callback(err); } -console.log("FileSystem: Saved file",fileInfo.filepath); + self.log("Saved file",fileInfo.filepath); _finish(); }); }); @@ -173,7 +173,7 @@ console.log("FileSystem: Saved file",fileInfo.filepath); if(err) { return callback(err); } -console.log("FileSystem: Saved file",fileInfo.filepath); + self.log("Saved file",fileInfo.filepath); _finish(); }); } @@ -211,7 +211,7 @@ FileSystemAdaptor.prototype.deleteTiddler = function(title,callback) { if(err) { return callback(err); } -console.log("FileSystem: Deleted file",fileInfo.filepath); + self.log("Deleted file",fileInfo.filepath); // Delete the metafile if present if(fileInfo.hasMetaFile) { fs.unlink(fileInfo.filepath + ".meta",function(err) {