Author: reebalazs
Date: Sat Nov 24 21:17:47 2007
New Revision: 49058

Modified:
   kukit/kukit.js/branch/ree-loglevels/kukit/utils.js
Log:
Refactor, and allow a method to set the log level from javascript on the fly 
(for FireKiss)

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 21:17:47 2007
@@ -87,14 +87,16 @@
 // 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() {
+kukit._COOKIE_LOGLEVEL = '__kss_loglevel';
+
+// an empty logger
+kukit._logger = new function() {
+
+    this.updateLogLevel = function() {
 ;;;     // set default level
-;;;     var loglevel = 0;
+;;;     this.loglevel = 0;
 ;;;     // read the cookie
-;;;     var cookie = kukit.readCookie('__kss_loglevel');
+;;;     var cookie = kukit.readCookie(kukit._COOKIE_LOGLEVEL);
 ;;;     if (cookie) {
 ;;;         // decode it to a numeric level
 ;;;         cookie = cookie.toLowerCase();
@@ -103,19 +105,47 @@
 ;;;         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;
+;;;         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();
+    };
+
+    // 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
+kukit.setLogLevel = function(level) {
+;;;     // Store it in the cookie so that it persists through requests.
+;;;     kukit.dom.writeCookie(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.
@@ -123,76 +153,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()) {
+;;; }();
+
+;;; // 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.
-;;;         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;
+;;;         this.logDebug = this._logFilter(console.log, 0);
+;;;         this.log = this._logFilter(console.log, 1);
+;;;         this.logWarning = this._logFilter(console.warn, 2);
+;;;         this.logError = this._logFilter(console.error, 3);
+;;;         this.logFatal = this._logFilter(console.error, 3);
+;;;     }
 ;;; }
 
 ;;; // 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.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
-;;;         //    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 = this._logFilter(MochiKit.Logging.logDebug, 0);
+;;;         this.log = this._logFilter(MochiKit.Logging.log, 1);
+;;;         this.logWarning = this._logFilter(MochiKit.Logging.logWarning, 2);
+;;;         this.logError = this._logFilter(MochiKit.Logging.logError, 3);
+;;;         this.logFatal = this._logFilter(MochiKit.Logging.logFatal, 3);
+;;;     }
+;;;     // 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.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.logDebug = kukit._null;
-        kukit.log = kukit._null;
-        kukit.logWarning = kukit._null;
-        kukit.logError = kukit._null;
-        kukit.logFatal = kukit._null;
+;;; // Set up logging for Safari
+;;; if (! kukit.hasFirebug && ! kukit.hasMochiKit && kukit.hasSafari) {
+;;;     kukit._logger._setupHandlers = function() {
+;;;         this.logDebug = this._logFilter(function(str) { 
console.log('DEBUG: '+str); }, 0);
+;;;         this.log = this._logFilter(function(str) { console.log('INFO: 
'+str); }, 1);
+;;;         this.logWarning = this._logFilter(function(str) { 
console.log('WARNING: '+str); }, 2);
+;;;         this.logError = this._logFilter(function(str) { 
console.log('ERROR: '+str); }, 3);
+;;;         this.logFatal = this._logFilter(function(str) { 
console.log('FATAL: '+str); }, 3);
+;;;     }
 ;;; }
 
+// Initialize the logger with the solution we've just detected
+kukit._logger.updateLogLevel();
+
 // log a startup message
 ;;; kukit.log('Loading KSS engine.');
 
_______________________________________________
Kukit-checkins mailing list
[email protected]
http://codespeak.net/mailman/listinfo/kukit-checkins

Reply via email to