1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-04-06 02:37:14 +00:00

Merge 8851ab0b23fcec17496bd56dce97fab69dd87b08 into 961e74f73d230d0028efb586db07699120eac888

This commit is contained in:
Mario Pietsch 2025-04-04 15:00:27 +02:00 committed by GitHub
commit e2f96996a1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 94 additions and 26 deletions

View File

@ -13,6 +13,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 || {};
@ -30,26 +36,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);
}
}
};
/*
@ -59,6 +117,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
*/

View File

@ -30,16 +30,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() {
@ -64,9 +63,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,

View File

@ -41,7 +41,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);
};
@ -51,7 +51,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
@ -61,13 +61,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 {
@ -79,18 +77,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--) {