1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-01 07:36:18 +00:00
TiddlyWiki5/core/modules/utils/logger.js

86 lines
2.3 KiB
JavaScript
Raw Normal View History

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";
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 || {};
this.componentName = componentName || "";
2017-09-04 13:55:12 +00:00
this.colour = options.colour || "white";
this.enable = "enable" in options ? options.enable : true;
2014-01-26 18:53:31 +00:00
}
/*
Log a message
*/
Logger.prototype.log = function(/* args */) {
if(this.enable && console !== undefined && console.log !== undefined) {
2017-09-04 13:55:12 +00:00
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()));
2014-01-26 18:53:31 +00:00
}
};
/*
Alert a message
*/
Logger.prototype.alert = function(/* args */) {
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;
}
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));
} else {
// Print an orange message to the console if not in the browser
console.error("\x1b[1;33m" + text + "\x1b[0m");
}
}
};
2014-01-26 18:53:31 +00:00
exports.Logger = Logger;
})();