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
|
|
|
|
*/
|
2014-02-14 07:53:41 +00:00
|
|
|
function Logger(componentName) {
|
|
|
|
this.componentName = componentName || "";
|
2014-01-26 18:53:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Log a message
|
|
|
|
*/
|
|
|
|
Logger.prototype.log = function(/* args */) {
|
|
|
|
if(console !== undefined && console.log !== undefined) {
|
2014-02-14 07:53:41 +00:00
|
|
|
return Function.apply.call(console.log, console, [this.componentName + ":"].concat(Array.prototype.slice.call(arguments,0)));
|
2014-01-26 18:53:31 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-02-14 07:53:41 +00:00
|
|
|
/*
|
|
|
|
Alert a message
|
|
|
|
*/
|
|
|
|
Logger.prototype.alert = function(/* args */) {
|
2014-02-14 10:29:26 +00:00
|
|
|
// Prepare the text of the alert
|
|
|
|
var text = Array.prototype.join.call(arguments," ");
|
|
|
|
// 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 = {
|
2014-02-14 07:53:41 +00:00
|
|
|
title: $tw.wiki.generateNewTitle("$:/temp/alerts/alert",{prefix: ""}),
|
|
|
|
text: text,
|
2014-02-14 10:29:26 +00:00
|
|
|
tags: [ALERT_TAG],
|
|
|
|
component: this.componentName
|
2014-02-14 07:53:41 +00:00
|
|
|
};
|
2014-02-14 10:29:26 +00:00
|
|
|
existingCount = 0;
|
|
|
|
}
|
|
|
|
alertFields.modified = new Date();
|
|
|
|
if(++existingCount > 1) {
|
|
|
|
alertFields.count = existingCount;
|
|
|
|
} else {
|
|
|
|
alertFields.count = undefined;
|
|
|
|
}
|
|
|
|
$tw.wiki.addTiddler(new $tw.Tiddler(alertFields));
|
2014-02-14 07:53:41 +00:00
|
|
|
// Log it too
|
|
|
|
this.log.apply(this,Array.prototype.slice.call(arguments,0));
|
|
|
|
};
|
|
|
|
|
2014-01-26 18:53:31 +00:00
|
|
|
exports.Logger = Logger;
|
|
|
|
|
|
|
|
})();
|