org.symbian.tools.wrttools/projecttemplates/WRTKit/Utils/Logger.js
changeset 309 c01f5ab28a11
parent 308 c521df56b15d
child 310 e9484be98cfe
equal deleted inserted replaced
308:c521df56b15d 309:c01f5ab28a11
     1 /**
       
     2  * Copyright (c) 2009-2010 Symbian Foundation and/or its subsidiary(-ies).
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of the License "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  * 
       
    12  * Contributors:
       
    13  * 
       
    14  * Description:
       
    15  * 
       
    16  */
       
    17 
       
    18 ///////////////////////////////////////////////////////////////////////////////
       
    19 // Logger utility class that uses the Firebug console class.
       
    20 
       
    21 // Constructor (everything is static so this is empty).
       
    22 function Logger() {
       
    23     // Set default logger level.
       
    24     this.level = this.LOG_LEVEL_OFF;
       
    25 }
       
    26 
       
    27 // Logger levels.
       
    28 Logger.prototype.LOG_LEVEL_DEBUG = 0;
       
    29 Logger.prototype.LOG_LEVEL_INFO = 1;
       
    30 Logger.prototype.LOG_LEVEL_WARN = 2;
       
    31 Logger.prototype.LOG_LEVEL_ERROR = 3;
       
    32 Logger.prototype.LOG_LEVEL_OFF = 4;
       
    33 
       
    34 Logger.prototype.level = null;
       
    35 Logger.prototype.filter = null;
       
    36 
       
    37 // Disable logging on other browsers except Firefox.
       
    38 Logger.prototype.enabled = (navigator.userAgent.indexOf("Firefox") != -1);
       
    39 
       
    40 // Dumps an objects properties and methods to the console.
       
    41 Logger.prototype.dump = function(obj) {
       
    42     if (this.enabled) {
       
    43         console.dir(obj);
       
    44     }
       
    45 };
       
    46 
       
    47 // Dumps a stracktrace to the console.
       
    48 Logger.prototype.trace = function() {
       
    49     if (this.enabled) {
       
    50         console.trace();
       
    51     }
       
    52 };
       
    53 
       
    54 // Prints a debug message to the console.
       
    55 Logger.prototype.debug = function(str) {
       
    56     if (this.enabled && this.level <= this.LOG_LEVEL_DEBUG) {
       
    57         if (this.filter == null) {
       
    58             console.debug(str);
       
    59         } else {
       
    60             var show = false;
       
    61             for (i in this.filter) {
       
    62                 if (str.indexOf(this.filter[i]) >= 0) {
       
    63                     show = true;
       
    64                     break;
       
    65                 }
       
    66             }
       
    67             if (show) {
       
    68                 console.debug(str);
       
    69             }
       
    70         }
       
    71     }
       
    72 };
       
    73 
       
    74 // Prints an info message to the console.
       
    75 Logger.prototype.info = function(str) {
       
    76     if (this.enabled && this.level <= this.LOG_LEVEL_INFO) {
       
    77         console.info(str);
       
    78     }
       
    79 };
       
    80 
       
    81 // Prints a warning message to the console.
       
    82 Logger.prototype.warn = function(str) {
       
    83     if (this.enabled && this.level <= this.LOG_LEVEL_WARN) {
       
    84         console.warn(str);
       
    85     }
       
    86 };
       
    87 
       
    88 // Prints an error message to the console.
       
    89 Logger.prototype.error = function(str) {
       
    90     if (this.enabled && this.level <= this.LOG_LEVEL_ERROR) {
       
    91         console.error(str);
       
    92     }
       
    93 };