[KSS-checkins] r49365 - in kukit/kukit.js/branch/1.2: doc kukit

reebalazs at codespeak.net reebalazs at codespeak.net
Wed Dec 5 10:13:32 CET 2007


Author: reebalazs
Date: Wed Dec  5 10:13:31 2007
New Revision: 49365

Modified:
   kukit/kukit.js/branch/1.2/doc/HISTORY.txt
   kukit/kukit.js/branch/1.2/kukit/dom.js
   kukit/kukit.js/branch/1.2/kukit/utils.js
Log:
Implement loglevels based on cookies. Merge from trunk -r49360, -r49363.

Modified: kukit/kukit.js/branch/1.2/doc/HISTORY.txt
==============================================================================
--- kukit/kukit.js/branch/1.2/doc/HISTORY.txt	(original)
+++ kukit/kukit.js/branch/1.2/doc/HISTORY.txt	Wed Dec  5 10:13:31 2007
@@ -6,8 +6,18 @@
 
     - ...
 
+    - 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]
+
 kukit.js - 1.2.3 Released 2007-11-08
 
+    - Implement event binding based on the ids fetched 
+      dynamically from the dom, by value providers.
+      [ree]
+
 kukit.js - 1.2.2 Released 2007-10-05
 
     - Some code for FireKiss

Modified: kukit/kukit.js/branch/1.2/kukit/dom.js
==============================================================================
--- kukit/kukit.js/branch/1.2/kukit/dom.js	(original)
+++ kukit/kukit.js/branch/1.2/kukit/dom.js	Wed Dec  5 10:13:31 2007
@@ -483,3 +483,27 @@
     return tags;
 };
 
+/*
+ * Cookie handling code taken from: 
+ * http://www.quirksmode.org/js/cookies.html
+ */
+
+kukit.dom.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.
+kukit.dom.readCookie = kukit.readCookie;
+
+kukit.dom.eraseCookie = function(name) {
+    createCookie(name, "", -1);
+};
+

Modified: kukit/kukit.js/branch/1.2/kukit/utils.js
==============================================================================
--- kukit/kukit.js/branch/1.2/kukit/utils.js	(original)
+++ kukit/kukit.js/branch/1.2/kukit/utils.js	Wed Dec  5 10:13:31 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