Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add console-error to the Logger class #7125

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 73 additions & 8 deletions core/modules/utils/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ var ALERT_TAG = "$:/tags/Alert";

/*
Make a new logger
componentName: logger name, eg: "filesystem" as found in: $:/plugins/tiddlywiki/filesystem/filesystemadaptor.js
Options:
- colour ... default colour used to style text output
- enable ... default: true - logger is enabled
- save ... default: true - message will be saved to buffer
- saveLimit ... default 100kByte - limit buffer size
*/
function Logger(componentName,options) {
options = options || {};
Expand All @@ -33,26 +39,78 @@ Logger.prototype.setSaveBuffer = function(logger) {
this.saveBufferLogger = logger;
};

Logger.prototype.writeToBuffer = function(/* args */) {
var self = this;
this.saveBufferLogger.buffer += $tw.utils.formatDateString(new Date(),"YYYY-0MM-0DD 0hh:0mm:0ss.0XXX") + " #";
$tw.utils.each(Array.prototype.slice.call(arguments,0),function(arg,index) {
self.saveBufferLogger.buffer += " " + arg.join(" - ");
});
this.saveBufferLogger.buffer += "\n";
this.saveBufferLogger.buffer = this.saveBufferLogger.buffer.slice(-this.saveBufferLogger.saveLimit);
};

/*
Log a message
*/
Logger.prototype.log = function(/* args */) {
var self = this;
if(this.enable) {
if(this.saveBufferLogger.save) {
this.saveBufferLogger.buffer += $tw.utils.formatDateString(new Date(),"YYYY MM DD 0hh:0mm:0ss.0XXX") + ":";
$tw.utils.each(Array.prototype.slice.call(arguments,0),function(arg,index) {
self.saveBufferLogger.buffer += " " + arg;
});
this.saveBufferLogger.buffer += "\n";
this.saveBufferLogger.buffer = this.saveBufferLogger.buffer.slice(-this.saveBufferLogger.saveLimit);
this.writeToBuffer(Array.prototype.slice.call(arguments,0));
}
if(console !== undefined && console.log !== undefined) {
var logMessage = [$tw.utils.terminalColour(this.colour) + this.componentName + ":"].concat(Array.prototype.slice.call(arguments,0));
logMessage[logMessage.length-1] += $tw.utils.terminalColour();
return Function.apply.call(console.log, console, logMessage);
}
}
}
};

/*
Log an "error" message to console.error so it also gets a red colour in browsers
*/
Logger.prototype.error = function(/* args */) {
if(this.enable) {
if(this.saveBufferLogger.save) {
this.writeToBuffer(Array.prototype.slice.call(arguments,0));
}
if(console !== undefined && console.error !== undefined) {
var logMessage = [$tw.utils.terminalColour("red") + this.componentName + ":"].concat(Array.prototype.slice.call(arguments,0));
logMessage[logMessage.length-1] += $tw.utils.terminalColour();
return Function.apply.call(console.error, console, logMessage);
}
}
};

/*
Log an "info" message to console.info so it also gets an (i) info icon in front
*/
Logger.prototype.info = function(/* args */) {
if(this.enable) {
if(this.saveBufferLogger.save) {
this.writeToBuffer(Array.prototype.slice.call(arguments,0));
}
if(console !== undefined && console.info !== undefined) {
var logMessage = [$tw.utils.terminalColour(this.colour) + this.componentName + ":"].concat(Array.prototype.slice.call(arguments,0));
logMessage[logMessage.length-1] += $tw.utils.terminalColour();
return Function.apply.call(console.info, console, logMessage);
}
}
};

/*
Log an "warning" message to console.info so it also gets an (!) warning icon in front
*/
Logger.prototype.warn = function(/* args */) {
if(this.enable) {
if(this.saveBufferLogger.save) {
this.writeToBuffer(Array.prototype.slice.call(arguments,0));
}
if(console !== undefined && console.warn !== undefined) {
var logMessage = [$tw.utils.terminalColour("orange") + this.componentName + ":"].concat(Array.prototype.slice.call(arguments,0));
logMessage[logMessage.length-1] += $tw.utils.terminalColour();
return Function.apply.call(console.warn, console, logMessage);
}
}
};

/*
Expand All @@ -62,6 +120,13 @@ Logger.prototype.getBuffer = function() {
return this.saveBufferLogger.buffer;
};

/*
Clear the message buffer
*/
Logger.prototype.clearBuffer = function() {
this.saveBufferLogger.buffer = "";
};

/*
Log a structure as a table
*/
Expand Down
22 changes: 14 additions & 8 deletions plugins/tiddlywiki/browser-storage/startup.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,15 @@ exports.startup = function() {
}
// Compute our prefix for local storage keys
var prefix = "tw5#" + window.location.pathname + "#";
// Make a logger
var logger = new $tw.utils.Logger("browser-storage",{
colour: "cyan"
});
// Add browserStorage object to $tw
$tw.browserStorage = new BrowserStorageUtil($tw.wiki,{
enabledTitle: ENABLED_TITLE,
prefix: prefix,
logger: logger
prefix: prefix
});
// Add a logger
$tw.browserStorage.logger = new $tw.utils.Logger("browser-storage",{
colour: "green"
});
// Function to compile the filter
var filterFn,
compileFilter = function() {
Expand All @@ -67,9 +66,16 @@ exports.startup = function() {
},
requestPersistence = function() {
setPersistedState("requested");
$tw.browserStorage.logger.info("Show user request for browser PERSISTED storage");
navigator.storage.persist().then(function(persisted) {
console.log("Request for persisted storage " + (persisted ? "granted" : "denied"));
setPersistedState(persisted ? "granted" : "denied");
if(persisted) {
$tw.browserStorage.logger.info("User GRANTED access to browser PERSISTED storage");
setPersistedState("granted");
}
else {
$tw.browserStorage.logger.info("User DENIED access to persisted storage. Storage MAY BE CLEARED by the UA under storage pressure.");
setPersistedState("denied");
}
});
},
persistPermissionRequested = false,
Expand Down
17 changes: 7 additions & 10 deletions plugins/tiddlywiki/browser-storage/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ BrowserStorageUtil.prototype.addCachedTiddlers = function() {
};

BrowserStorageUtil.prototype.removeTiddlerFromLocalStorage = function(title) {
console.log("browser-storage: Removing", title);
this.logger.log("Removing", title);
window.localStorage.removeItem(this.options.prefix + title);
};

Expand All @@ -55,7 +55,7 @@ BrowserStorageUtil.prototype.saveTiddlerToLocalStorage = function(title) {
if(tiddler) {
if(this.wiki.tiddlerExists(title)) {
// This is not a shadow tiddler
console.log("browser-storage: Saving",title);
this.logger.log("Saving",title);
// Get the JSON of the tiddler
var json = JSON.stringify(tiddler.getFieldStrings());
// Try to save it to local storage
Expand All @@ -65,13 +65,11 @@ BrowserStorageUtil.prototype.saveTiddlerToLocalStorage = function(title) {
if(e.name === "QuotaExceededError") {
// Complain if we failed
var msg = $tw.wiki.getTiddlerText(this.QUOTA_EXCEEDED_ALERT_TITLE,this.DEFAULT_QUOTA_EXCEEDED_ALERT_PREFIX + title + this.DEFAULT_QUOTA_EXCEEDED_ALERT_SUFFIX);
if(this.options.logger) {
this.options.logger.alert(msg);
}
this.logger.alert(msg);
// No point in keeping old values around for this tiddler
window.localStorage.removeItem(this.options.prefix + title);
} else {
console.log("Browser-storage error:",e);
this.logger.log("Error:",e);
}
}
} else {
Expand All @@ -83,18 +81,17 @@ BrowserStorageUtil.prototype.saveTiddlerToLocalStorage = function(title) {
// In local storage, use the special value of empty string to mark the tiddler as deleted
// On future page loads, if the tiddler is already gone from startup then the blank entry
// will be removed from localstorage. Otherwise, the tiddler will be deleted.
console.log("browser-storage: Blanking",title);
this.logger.log("Blanking",title);
try {
window.localStorage.setItem(this.options.prefix + title, "");
} catch(e) {
console.log("Browser-storage error:",e);
this.logger.error("Error:",e);
}
}
};

BrowserStorageUtil.prototype.clearLocalStorage = function() {
var url = window.location.pathname,
log = [];
var url = window.location.pathname;
// Step through each browser storage item
if(window.localStorage) {
for(var index=window.localStorage.length - 1; index>=0; index--) {
Expand Down
Loading