2014-01-26 18:53:31 +00:00
|
|
|
/*\
|
|
|
|
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";
|
|
|
|
|
2014-02-14 10:29:26 +00:00
|
|
|
var ALERT_TAG = "$:/tags/Alert";
|
|
|
|
|
2014-01-26 18:53:31 +00:00
|
|
|
/*
|
|
|
|
Make a new logger
|
|
|
|
*/
|
2017-09-04 13:55:12 +00:00
|
|
|
function Logger(componentName,options) {
|
|
|
|
options = options || {};
|
2014-02-14 07:53:41 +00:00
|
|
|
this.componentName = componentName || "";
|
2017-09-04 13:55:12 +00:00
|
|
|
this.colour = options.colour || "white";
|
2017-09-26 16:10:57 +00:00
|
|
|
this.enable = "enable" in options ? options.enable : true;
|
2020-03-30 14:24:05 +00:00
|
|
|
this.save = "save" in options ? options.save : true;
|
|
|
|
this.saveLimit = options.saveLimit || 100 * 1024;
|
|
|
|
this.saveBufferLogger = this;
|
|
|
|
this.buffer = "";
|
|
|
|
this.alertCount = 0;
|
2014-01-26 18:53:31 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 14:24:05 +00:00
|
|
|
Logger.prototype.setSaveBuffer = function(logger) {
|
|
|
|
this.saveBufferLogger = logger;
|
|
|
|
};
|
|
|
|
|
2014-01-26 18:53:31 +00:00
|
|
|
/*
|
|
|
|
Log a message
|
|
|
|
*/
|
|
|
|
Logger.prototype.log = function(/* args */) {
|
2020-03-30 14:24:05 +00:00
|
|
|
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";
|
2021-05-30 18:20:17 +00:00
|
|
|
this.saveBufferLogger.buffer = this.saveBufferLogger.buffer.slice(-this.saveBufferLogger.saveLimit);
|
2020-03-30 14:24:05 +00:00
|
|
|
}
|
|
|
|
if(console !== undefined && console.log !== undefined) {
|
2022-09-15 11:10:33 +00:00
|
|
|
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);
|
2020-03-30 14:24:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Read the message buffer
|
|
|
|
*/
|
|
|
|
Logger.prototype.getBuffer = function() {
|
|
|
|
return this.saveBufferLogger.buffer;
|
2014-01-26 18:53:31 +00:00
|
|
|
};
|
|
|
|
|
2019-05-10 14:56:01 +00:00
|
|
|
/*
|
|
|
|
Log a structure as a table
|
|
|
|
*/
|
|
|
|
Logger.prototype.table = function(value) {
|
|
|
|
(console.table || console.log)(value);
|
|
|
|
};
|
|
|
|
|
2014-02-14 07:53:41 +00:00
|
|
|
/*
|
|
|
|
Alert a message
|
|
|
|
*/
|
|
|
|
Logger.prototype.alert = function(/* args */) {
|
2017-09-26 16:10:57 +00:00
|
|
|
if(this.enable) {
|
|
|
|
// Prepare the text of the alert
|
|
|
|
var text = Array.prototype.join.call(arguments," ");
|
|
|
|
// Create alert tiddlers in the browser
|
|
|
|
if($tw.browser) {
|
|
|
|
// Check if there is an existing alert with the same text and the same component
|
|
|
|
var existingAlerts = $tw.wiki.getTiddlersWithTag(ALERT_TAG),
|
|
|
|
alertFields,
|
|
|
|
existingCount,
|
|
|
|
self = this;
|
|
|
|
$tw.utils.each(existingAlerts,function(title) {
|
|
|
|
var tiddler = $tw.wiki.getTiddler(title);
|
|
|
|
if(tiddler.fields.text === text && tiddler.fields.component === self.componentName && tiddler.fields.modified && (!alertFields || tiddler.fields.modified < alertFields.modified)) {
|
|
|
|
alertFields = $tw.utils.extend({},tiddler.fields);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if(alertFields) {
|
|
|
|
existingCount = alertFields.count || 1;
|
|
|
|
} else {
|
|
|
|
alertFields = {
|
|
|
|
title: $tw.wiki.generateNewTitle("$:/temp/alerts/alert",{prefix: ""}),
|
|
|
|
text: text,
|
|
|
|
tags: [ALERT_TAG],
|
|
|
|
component: this.componentName
|
|
|
|
};
|
|
|
|
existingCount = 0;
|
2020-03-30 14:24:05 +00:00
|
|
|
this.alertCount += 1;
|
2015-03-12 22:34:59 +00:00
|
|
|
}
|
2017-09-26 16:10:57 +00:00
|
|
|
alertFields.modified = new Date();
|
|
|
|
if(++existingCount > 1) {
|
|
|
|
alertFields.count = existingCount;
|
|
|
|
} else {
|
|
|
|
alertFields.count = undefined;
|
|
|
|
}
|
|
|
|
$tw.wiki.addTiddler(new $tw.Tiddler(alertFields));
|
|
|
|
// Log the alert as well
|
|
|
|
this.log.apply(this,Array.prototype.slice.call(arguments,0));
|
2015-03-12 22:34:59 +00:00
|
|
|
} else {
|
2017-09-26 16:10:57 +00:00
|
|
|
// Print an orange message to the console if not in the browser
|
|
|
|
console.error("\x1b[1;33m" + text + "\x1b[0m");
|
2021-05-30 18:20:17 +00:00
|
|
|
}
|
2014-02-14 10:29:26 +00:00
|
|
|
}
|
2014-02-14 07:53:41 +00:00
|
|
|
};
|
|
|
|
|
2020-03-30 14:24:05 +00:00
|
|
|
/*
|
|
|
|
Clear outstanding alerts
|
|
|
|
*/
|
|
|
|
Logger.prototype.clearAlerts = function() {
|
|
|
|
var self = this;
|
|
|
|
if($tw.browser && this.alertCount > 0) {
|
|
|
|
$tw.utils.each($tw.wiki.getTiddlersWithTag(ALERT_TAG),function(title) {
|
|
|
|
var tiddler = $tw.wiki.getTiddler(title);
|
|
|
|
if(tiddler.fields.component === self.componentName) {
|
|
|
|
$tw.wiki.deleteTiddler(title);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this.alertCount = 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-01-26 18:53:31 +00:00
|
|
|
exports.Logger = Logger;
|
|
|
|
|
|
|
|
})();
|