[KSS-checkins] r49028 - in kukit/kukit.js/branch/ree-loglevels: doc kukit

reebalazs at codespeak.net reebalazs at codespeak.net
Sat Nov 24 13:08:28 CET 2007


Author: reebalazs
Date: Sat Nov 24 13:08:28 2007
New Revision: 49028

Modified:
   kukit/kukit.js/branch/ree-loglevels/doc/HISTORY.txt
   kukit/kukit.js/branch/ree-loglevels/kukit/dom.js
   kukit/kukit.js/branch/ree-loglevels/kukit/utils.js
Log:
Implement loglevels based on cookies

Modified: kukit/kukit.js/branch/ree-loglevels/doc/HISTORY.txt
==============================================================================
--- kukit/kukit.js/branch/ree-loglevels/doc/HISTORY.txt	(original)
+++ kukit/kukit.js/branch/ree-loglevels/doc/HISTORY.txt	Sat Nov 24 13:08:28 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/branch/ree-loglevels/kukit/dom.js
==============================================================================
--- kukit/kukit.js/branch/ree-loglevels/kukit/dom.js	(original)
+++ kukit/kukit.js/branch/ree-loglevels/kukit/dom.js	Sat Nov 24 13:08:28 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/branch/ree-loglevels/kukit/utils.js
==============================================================================
--- kukit/kukit.js/branch/ree-loglevels/kukit/utils.js	(original)
+++ kukit/kukit.js/branch/ree-loglevels/kukit/utils.js	Sat Nov 24 13:08:28 2007
@@ -66,6 +66,56 @@
 ;;;     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() {};
+
+;;; // Logging filter. This allows filtering log levels based on
+;;; // a browser cookie.
+;;;
+;;; kukit._loglevel = function() {
+;;;     // set default level
+;;;     var loglevel = 0;
+;;;     // read the cookie
+;;;     var cookie = kukit.readCookie('__kss_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') loglevel = 0;
+;;;         if (cookie == 'info') loglevel = 1;
+;;;         if (cookie == 'warning') loglevel = 2;
+;;;         if (cookie == 'error') loglevel = 3;
+;;;     }
+;;;     // return the value
+;;;     return loglevel;
+;;; }();
+;;; 
+;;; kukit._logFilter = function(f, currentlevel) {
+;;;     return (currentlevel >= kukit._loglevel) ? f : kukit._null;
+;;; };
+
 // 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.
@@ -86,11 +136,13 @@
 ;;;    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;
+;;;         // for debug level we also log as 'info', because we do
+;;;         // not want FireBug to display line information.
+;;;         kukit.logDebug = kukit._logFilter(console.log, 0);
+;;;         kukit.log = kukit._logFilter(console.log, 1);
+;;;         kukit.logWarning = kukit._logFilter(console.warn, 2);
+;;;         kukit.logError = kukit._logFilter(console.error, 3);
+;;;         kukit.logFatal = kukit._logFilter(console.error, 3);
 ;;;         kukit.hasFirebug = true;
 ;;; }
 
@@ -103,11 +155,11 @@
 ;;;    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;
+;;;         kukit.logDebug = kukit._logFilter(MochiKit.Logging.logDebug, 0);
+;;;         kukit.log = kukit._logFilter(MochiKit.Logging.log, 1);
+;;;         kukit.logWarning = kukit._logFilter(MochiKit.Logging.logWarning, 2);
+;;;         kukit.logError = kukit._logFilter(MochiKit.Logging.logError, 3);
+;;;         kukit.logFatal = kukit._logFilter(MochiKit.Logging.logFatal, 3);
 ;;;         // make convenience url
 ;;;         //    javascript:kukit.showLog();
 ;;;         // instead of the need to say
@@ -125,20 +177,20 @@
 ;;;    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); };
+;;;         kukit.logDebug = kukit._logFilter(function(str) { console.log('DEBUG: '+str); }, 0);
+;;;         kukit.log = kukit._logFilter(function(str) { console.log('INFO: '+str); }, 1);
+;;;         kukit.logWarning = kukit._logFilter(function(str) { console.log('WARNING: '+str); }, 2);
+;;;         kukit.logError = kukit._logFilter(function(str) { console.log('ERROR: '+str); }, 3);
+;;;         kukit.logFatal = kukit._logFilter(function(str) { console.log('FATAL: '+str); }, 3);
 ;;; }
 
 /* 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;
+        kukit.logDebug = kukit._null;
+        kukit.log = kukit._null;
+        kukit.logWarning = kukit._null;
+        kukit.logError = kukit._null;
+        kukit.logFatal = kukit._null;
 ;;; }
 
 // log a startup message


More information about the Kukit-checkins mailing list