[KSS-checkins] r49360 - in kukit/kukit.js/trunk: doc kukit
reebalazs at codespeak.net
reebalazs at codespeak.net
Wed Dec 5 08:38:27 CET 2007
Author: reebalazs
Date: Wed Dec 5 08:38:26 2007
New Revision: 49360
Modified:
kukit/kukit.js/trunk/doc/HISTORY.txt
kukit/kukit.js/trunk/kukit/dom.js
kukit/kukit.js/trunk/kukit/utils.js
Log:
Implement loglevels based on cookies. Merge of branch ree-loglevels.
Modified: kukit/kukit.js/trunk/doc/HISTORY.txt
==============================================================================
--- kukit/kukit.js/trunk/doc/HISTORY.txt (original)
+++ kukit/kukit.js/trunk/doc/HISTORY.txt Wed Dec 5 08:38:26 2007
@@ -6,6 +6,12 @@
- ...
+ - Implement loglevels based on cookies
+ Add cookie handling code to kss.dom
+ Change logging on FireBug to avoid line info in debug
+ level messages
+ [ree]
+
- Implement event binding based on the ids fetched
dynamically from the dom, by value providers.
[ree]
Modified: kukit/kukit.js/trunk/kukit/dom.js
==============================================================================
--- kukit/kukit.js/trunk/kukit/dom.js (original)
+++ kukit/kukit.js/trunk/kukit/dom.js Wed Dec 5 08:38:26 2007
@@ -432,5 +432,30 @@
}
};
+
+/*
+ * Cookie handling code taken from:
+ * http://www.quirksmode.org/js/cookies.html
+ */
+
+this.createCookie = function(name, value, days) {
+ if (days) {
+ var date = new Date();
+ date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
+ var expires = "; expires=" + date.toGMTString();
+ }
+ else var expires = "";
+ document.cookie = name + "=" + value + expires + "; path=/";
+}
+
+// we get this from kukit utils.js. We needed an early
+// definition there, because logging is needed from the
+// very beginning.
+this.readCookie = kukit.readCookie;
+
+this.eraseCookie = function(name) {
+ createCookie(name, "", -1);
+}
+
}(); /// MODULE END
Modified: kukit/kukit.js/trunk/kukit/utils.js
==============================================================================
--- kukit/kukit.js/trunk/kukit/utils.js (original)
+++ kukit/kukit.js/trunk/kukit/utils.js Wed Dec 5 08:38:26 2007
@@ -66,6 +66,94 @@
;;; kukit.logWarning(msg);
;;; };
+/*
+ * Cookie handling code taken from:
+ * http://www.quirksmode.org/js/cookies.html
+ * Cookie handling is in dom.js, but this method
+ * is needed right here for log handling.
+ */
+
+kukit.readCookie = function(name) {
+ var nameEQ = name + "=";
+ var ca = document.cookie.split(';');
+ for(var i=0; i < ca.length; i++) {
+ var c = ca[i];
+ while (c.charAt(0) == ' ') c = c.substring(1, c.length);
+ if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
+ }
+ return null;
+}
+
+// a null function that is used for logging
+kukit._null = function() {};
+
+kukit._COOKIE_LOGLEVEL = '__kss_loglevel';
+
+// an empty logger
+kukit._logger = new function() {
+
+ this.updateLogLevel = function() {
+;;; // set default level
+;;; this.loglevel = 0;
+;;; // read the cookie
+;;; var cookie = kukit.readCookie(kukit._COOKIE_LOGLEVEL);
+;;; if (cookie) {
+;;; // decode it to a numeric level
+;;; cookie = cookie.toLowerCase();
+;;; // Cookies are quoted in Zope, for some reason (???)
+;;; // ie we get '"VALUE"' here. Let's compensate this.
+;;; if (cookie.substr(0, 1) == '"') {
+;;; cookie = cookie.substr(1, cookie.length - 2);
+;;; }
+;;; if (cookie == 'debug') this.loglevel = 0;
+;;; if (cookie == 'info') this.loglevel = 1;
+;;; if (cookie == 'warning') this.loglevel = 2;
+;;; if (cookie == 'error') this.loglevel = 3;
+;;; };
+ // Call the function that sets up the handlers
+ this._setupHandlers();
+ // Wrap the just set up handlers, to include wrapping
+;;; this.logDebug = this._logFilter(this.logDebug, 0);
+;;; this.log = this._logFilter(this.log, 1);
+;;; this.logWarning = this._logFilter(this.logWarning, 2);
+;;; this.logError = this._logFilter(this.logError, 3);
+;;; this.logFatal = this._logFilter(this.logFatal, 3);
+ };
+
+ // Log filter, for use from the handlers.
+;;; this._logFilter = function(f, currentlevel) {
+;;; return (currentlevel >= this.loglevel) ? f : kukit._null;
+;;; };
+
+ // This sets up the handlers and allows to set them
+ // up again with a different cookie setting.
+ // Will be overwritten by different loggers.
+ this._setupHandlers = function() {
+ this.logDebug = kukit._null;
+ this.log = kukit._null;
+ this.logWarning = kukit._null;
+ this.logError = kukit._null;
+ this.logFatal = kukit._null;
+ };
+}();
+
+// Stub functions that can be used for logging
+kukit.logDebug = function(message) {kukit._logger.logDebug(message);};
+kukit.log = function(message) {kukit._logger.log(message);};
+kukit.logWarning = function(message) {kukit._logger.logWarning(message);};
+kukit.logError = function(message) {kukit._logger.logError(message);};
+kukit.logFatal = function(message) {kukit._logger.logFatal(message);};
+
+// Function to change the log level from javascript
+// level must be one of "DEBUG", "INFO", "WARNING", "ERROR".
+// (Small caps are tolerated as well)
+kukit.setLogLevel = function(level) {
+;;; // Store it in the cookie so that it persists through requests.
+;;; kukit.dom.createCookie(kukit._COOKIE_LOGLEVEL, level);
+;;; // re-establish the log handlers, based on this cookie setting
+;;; kukit._logger.updateLogLevel();
+}
+
// We want a way of knowing if Firebug is available :
// it is very convenient to log a node in Firebug;
// you get a clickable result that brings you to Firebug inspector.
@@ -73,74 +161,77 @@
// if (kukit.hasFirebug) {
// kukit.log(node);
// }
-kukit.hasFirebug = false;
;;; // check whether the logging stuff of Firebug is available
;;; kukit.hasFirebug = function() {
-;;; var result = typeof kukit.log == 'undefined';
-;;; result = result && typeof console != 'undefined';
+;;; var result = typeof console != 'undefined';
;;; result = result && typeof console.log != 'undefined';
;;; result = result && typeof console.debug != 'undefined';
;;; result = result && typeof console.error != 'undefined';
;;; result = result && typeof console.warn != 'undefined';
;;; return result;
-;;; }
-;;; if (kukit.hasFirebug()) {
-;;; kukit.log = console.log;
-;;; kukit.logDebug = console.debug;
-;;; kukit.logFatal = console.error;
-;;; kukit.logError = console.error;
-;;; kukit.logWarning = console.warn;
-;;; kukit.hasFirebug = true;
+;;; }();
+
+;;; // Set up logging for FireBug
+;;; if (kukit.hasFirebug) {
+;;; kukit._logger._setupHandlers = function() {
+;;; // for debug level we also log as 'info', because we do
+;;; // not want FireBug to display line information.
+;;; this.logDebug = console.log;
+;;; this.log = console.log;
+;;; this.logWarning = console.warn;
+;;; this.logError = console.error;
+;;; this.logFatal = console.error;
+;;; }
;;; }
;;; // check whether the logging stuff of MochiKit is available
;;; kukit.hasMochiKit = function() {
-;;; var result = typeof kukit.log == 'undefined';
-;;; result = result && typeof MochiKit != 'undefined';
+;;; var result = typeof MochiKit != 'undefined';
;;; result = result && typeof MochiKit.Logging != 'undefined';
;;; result = result && typeof MochiKit.Logging.log != 'undefined';
;;; return result;
-;;; }
-;;; if (kukit.hasMochiKit()) {
-;;; kukit.log = MochiKit.Logging.log;
-;;; kukit.logError = MochiKit.Logging.logError;
-;;; kukit.logDebug = MochiKit.Logging.logDebug;
-;;; kukit.logFatal = MochiKit.Logging.logFatal;
-;;; kukit.logWarning = MochiKit.Logging.logWarning;
-;;; // make convenience url
-;;; // javascript:kukit.showLog();
-;;; // instead of the need to say
-;;; // javascript:void(createLoggingPane(true));
-;;; kukit.showLog = function() {
-;;; createLoggingPane(true);
-;;; };
+;;; }();
+
+;;; // Set up logging for MochiKit
+;;; if (! kukit.hasFirebug && kukit.hasMochiKit) {
+;;; kukit._logger._setupHandlers = function() {
+;;; this.logDebug = MochiKit.Logging.logDebug;
+;;; this.log = MochiKit.Logging.log;
+;;; this.logWarning = MochiKit.Logging.logWarning;
+;;; this.logError = MochiKit.Logging.logError;
+;;; this.logFatal = MochiKit.Logging.logFatal;
+;;; }
+;;; // make convenience url
+;;; // javascript:kukit.showLog();
+;;; // instead of the need to say
+;;; // javascript:void(createLoggingPane(true));
+;;; kukit.showLog = function() {
+;;; createLoggingPane(true);
+;;; };
;;; }
;;; // check whether the logging stuff of Safari is available
;;; kukit.hasSafari = function() {
-;;; var result = typeof kukit.log == 'undefined';
-;;; result = result && typeof console != 'undefined';
+;;; var result = typeof console != 'undefined';
;;; result = result && typeof console.log != 'undefined';
;;; return result;
-;;; }
-;;; if (kukit.hasSafari()) {
-;;; kukit.log = function(str) { console.log('INFO: '+str); };
-;;; kukit.logError = function(str) { console.log('ERROR: '+str); };
-;;; kukit.logDebug = function(str) { console.log('DEBUG: '+str); };
-;;; kukit.logFatal = function(str) { console.log('FATAL: '+str); };
-;;; kukit.logWarning = function(str) { console.log('WARNING: '+str); };
-;;; }
+;;; }();
-/* no logging solution available */
-;;; if (typeof kukit.log == 'undefined') {
- kukit.log = function(str){};
- kukit.logError = kukit.log;
- kukit.logDebug = kukit.log;
- kukit.logFatal = kukit.log;
- kukit.logWarning = kukit.log;
+;;; // Set up logging for Safari
+;;; if (! kukit.hasFirebug && ! kukit.hasMochiKit && kukit.hasSafari) {
+;;; kukit._logger._setupHandlers = function() {
+;;; this.logDebug = function(str) { console.log('DEBUG: '+str); };
+;;; this.log = function(str) { console.log('INFO: '+str); };
+;;; this.logWarning = function(str) { console.log('WARNING: '+str); };
+;;; this.logError = function(str) { console.log('ERROR: '+str); };
+;;; this.logFatal = function(str) { console.log('FATAL: '+str); };
+;;; }
;;; }
+// Initialize the logger with the solution we've just detected
+kukit._logger.updateLogLevel();
+
// log a startup message
;;; kukit.log('Loading KSS engine.');
More information about the Kukit-checkins
mailing list