thirdparty/google_appengine/templates/logging_console.js
changeset 109 620f9b141567
equal deleted inserted replaced
108:261778de26ff 109:620f9b141567
       
     1 // Copyright 2007 Google Inc.
       
     2 // All Rights Reserved.
       
     3 
       
     4 // Licensed under the Apache License, Version 2.0 (the "License");
       
     5 // you may not use this file except in compliance with the License.
       
     6 // You may obtain a copy of the License at
       
     7 // 
       
     8 //     http://www.apache.org/licenses/LICENSE-2.0
       
     9 // 
       
    10 // Unless required by applicable law or agreed to in writing, software
       
    11 // distributed under the License is distributed on an "AS IS" BASIS,
       
    12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    13 // See the License for the specific language governing permissions and
       
    14 // limitations under the License.
       
    15 
       
    16 /**
       
    17  * @fileoverview Defines the code for the application logging console.
       
    18  */
       
    19 
       
    20 /**
       
    21  * The namespace we're using for all javascript classes and functions related
       
    22  * to the logging console.
       
    23  */
       
    24 var AH = {};
       
    25 
       
    26 
       
    27 /**
       
    28  * A collection of utility functions for reading and writing cookies.
       
    29  * @constructor
       
    30  */
       
    31 AH.CookieUtil = function() {};
       
    32 
       
    33 
       
    34 /**
       
    35  * Creates and adds a cookie with the specified expiration.
       
    36  * @param {String} name  The name of the desired cookie.
       
    37  * @param {String} value  The value of the desired cookie.
       
    38  * @param {Number} opt_days  If non-negative, the expiration time in days of the
       
    39  *     desired cookie. If not provided, the default value is 1.
       
    40  */
       
    41 AH.CookieUtil.prototype.setCookie = function(name, value, opt_days) {
       
    42   if (opt_days == null) {
       
    43     opt_days = 1;
       
    44   }
       
    45 
       
    46   var expires = '';
       
    47   if (opt_days < 0) {
       
    48     var date = new Date;
       
    49     date.setTime(date.getTime() + (opt_days * 24 * 60 * 60 * 1000));
       
    50     expires = '; expires=' + date.toGMTString();
       
    51   }
       
    52   document.cookie = name + '=' + value + expires + '; path=/';
       
    53 };
       
    54 
       
    55 
       
    56 /**
       
    57  * Returns the value of the requested cookie if it is available, and otherwise
       
    58  * returns a default value.
       
    59  * @param {String} name  The name of the requested cookie.
       
    60  * @param {String} defaultValue  The value to return if the requested cookie
       
    61  *     cannot be found.
       
    62  * @return {String} The requested cookie's value, or the default value.
       
    63  */
       
    64 AH.CookieUtil.prototype.getCookie = function(name, defaultValue) {
       
    65   var nameEQ = name + '=';
       
    66   var cookiePieces = document.cookie.split(';');
       
    67   for (var i = 0; i < cookiePieces.length; i++) {
       
    68     var c = cookiePieces[i];
       
    69     c = c.replace(/^\s+/, '');
       
    70     if (c.indexOf(nameEQ) == 0) {
       
    71       return c.substring(nameEQ.length, c.length);
       
    72     }
       
    73   }
       
    74   return defaultValue;
       
    75 };
       
    76 
       
    77 
       
    78 /**
       
    79  * Deletes the specified cookie.
       
    80  * @param {String} name  The name of the specified cookie.
       
    81  */
       
    82 AH.CookieUtil.prototype.removeCookie = function(name) {
       
    83   this.setCookie(name, '', -100);
       
    84 };
       
    85 
       
    86 
       
    87 /**
       
    88  * The logging console is a div that displays log statements generated by the
       
    89  * application during it's execution. It can be moved around, and the verbosity
       
    90  * can be adjusted.
       
    91  * @constructor
       
    92  */
       
    93 AH.LoggingConsole = function() {
       
    94   this.baseDiv = document.getElementById('_ah_base');
       
    95   this.logSeverityLevels = ['debug', 'info', 'warning', 'error', 'critical'];
       
    96   this.cookieUtil = new AH.CookieUtil;
       
    97 };
       
    98 
       
    99 
       
   100 /**
       
   101  * Creates and positions the logging console based on preferences available from
       
   102  * the cookies '_ah_severity' and '_ah_position'.
       
   103  */
       
   104 AH.LoggingConsole.prototype.initConsole = function() {
       
   105   // Define the font colors for the different log severity levels.
       
   106   this.addCssRule('._ah_logline_debug_prefix', 'color:#110000');
       
   107   this.addCssRule('._ah_logline_info_prefix', 'color:#440000');
       
   108   this.addCssRule('._ah_logline_warning_prefix', 'color:#880000');
       
   109   this.addCssRule('._ah_logline_error_prefix', 'color:#CC0000');
       
   110   this.addCssRule('._ah_logline_critical_prefix', 'color:#FF0000');
       
   111 
       
   112   // Change to the severity level stored in the cookie, defaulting to the lowest
       
   113   // severity level.
       
   114   this.changeLogSeverity(this.cookieUtil.getCookie('_ah_severity', 'debug'));
       
   115 
       
   116   // Move the console to position stored in the cookie, defaulting to the
       
   117   // bottom right position.
       
   118   this.moveBaseDiv(this.cookieUtil.getCookie('_ah_position', 'down_right'));
       
   119 };
       
   120 
       
   121 
       
   122 /**
       
   123  * Add CSS rules to the document to modify the presentation for elements of the
       
   124  * specified class name. This works for IE and Firefox, but does not work for
       
   125  * Safari.
       
   126  * @param {String} selector  A selector for the style class rule to modify.
       
   127  * @param {String} styleRules  The rules to add to the specified style class.
       
   128  */
       
   129 AH.LoggingConsole.prototype.addCssRule = function(selector, styleRules) {
       
   130   // If no sheet exists for the document, create one.
       
   131   var sheet;
       
   132   if (document.createStyleSheet) {
       
   133     // For IE:
       
   134     sheet = document.createStyleSheet();
       
   135   } else {
       
   136     // For Firefox:
       
   137     var styleElement = document.createElement('style');
       
   138     document.getElementsByTagName('head')[0].appendChild(styleElement);
       
   139     sheet = (styleElement.styleSheet ?
       
   140              styleElement.styleSheet :
       
   141              styleElement.sheet);
       
   142   }
       
   143 
       
   144   // Add the new style rules to the style sheet.
       
   145   if (sheet.addRule) {
       
   146     // For IE:
       
   147     sheet.addRule(selector, styleRules);
       
   148   } else if (sheet.insertRule) {
       
   149     // For Firefox:
       
   150     sheet.insertRule(selector + ' { ' + styleRules + ' }',
       
   151                      sheet.cssRules.length);
       
   152   }
       
   153 };
       
   154 
       
   155 
       
   156 /**
       
   157  * Change the log severity level, and persist the change to the '_ah_severity'
       
   158  * cookie.
       
   159  * @param {String} newSeverity  The desired log severity level.
       
   160  */
       
   161 AH.LoggingConsole.prototype.changeLogSeverity = function(newSeverity) {
       
   162   // First, find the numeric level for the provided severity.
       
   163   var severityLevel = -1;
       
   164   for (var i = 0; i < this.logSeverityLevels.length; i++) {
       
   165     if (newSeverity == this.logSeverityLevels[i]) {
       
   166       severityLevel = i;
       
   167     }
       
   168   }
       
   169 
       
   170   // An unknown logging severity was provided, so ignore the call.
       
   171   if (severityLevel == -1) {
       
   172     return;
       
   173   }
       
   174 
       
   175   // Display log lines if they have severity greater than or equal to the
       
   176   // desired severity.
       
   177   for (var i = 0; i < this.logSeverityLevels.length; i++) {
       
   178     var selector =
       
   179         '._ah_logline_' + this.logSeverityLevels[i];
       
   180     if (i < severityLevel) {
       
   181       this.addCssRule(selector, 'display:none');
       
   182     } else {
       
   183       this.addCssRule(selector, 'display:block');
       
   184     }
       
   185   }
       
   186 
       
   187   // Update the link text colors for the severity controls, and blur the links.
       
   188   for (var i = 0; i < this.logSeverityLevels.length; i++) {
       
   189     var linkToUpdate = document.getElementById(
       
   190         '_ah_show_' + this.logSeverityLevels[i]);
       
   191     if (i == severityLevel) {
       
   192       linkToUpdate.style.color = 'red';
       
   193     } else {
       
   194       linkToUpdate.style.color = 'blue';
       
   195     }
       
   196     linkToUpdate.blur();
       
   197   }
       
   198 
       
   199   // Save the new severity level to a cookie.
       
   200   this.cookieUtil.setCookie('_ah_severity', newSeverity);
       
   201 };
       
   202 
       
   203 
       
   204 /**
       
   205  * Set the colors for the whole navigation table to white.
       
   206  */
       
   207 AH.LoggingConsole.prototype.clearNavigationTable = function() {
       
   208   document.getElementById('_ah_up_left').style.backgroundColor = 'white';
       
   209   document.getElementById('_ah_up_right').style.backgroundColor = 'white';
       
   210   document.getElementById('_ah_down_left').style.backgroundColor = 'white';
       
   211   document.getElementById('_ah_down_right').style.backgroundColor = 'white';
       
   212 };
       
   213 
       
   214 
       
   215 /**
       
   216  * Moves the logging console to the desired position.
       
   217  * @param {String} newPosition  The desired position, which must be one of
       
   218  *     'up_left', 'up_right', 'down_left', or 'down_right'.
       
   219  */
       
   220 AH.LoggingConsole.prototype.moveBaseDiv = function(newPosition) {
       
   221   // Move the logging console to the desired position on the page.
       
   222   var newPositionPieces = newPosition.split('_');
       
   223   var newVerticalPosition = newPositionPieces[0];
       
   224   var newHorizontalPosition = newPositionPieces[1];
       
   225   if (newVerticalPosition == 'up') {
       
   226     this.baseDiv.style.top = '10px';
       
   227     this.baseDiv.style.bottom = '';
       
   228   } else {
       
   229     this.baseDiv.style.top = '';
       
   230     this.baseDiv.style.bottom = '10px';
       
   231   }
       
   232   if (newHorizontalPosition == 'left') {
       
   233     this.baseDiv.style.left = '10px';
       
   234     this.baseDiv.style.right = '';
       
   235   } else {
       
   236     this.baseDiv.style.left = '';
       
   237     this.baseDiv.style.right = '10px';
       
   238   }
       
   239 
       
   240   // Update the navigation table cell colors to reflect the new position.
       
   241   this.clearNavigationTable();
       
   242   document.getElementById('_ah_' + newPosition).style.backgroundColor = 'red';
       
   243 
       
   244   // Save the new position to a cookie.
       
   245   this.cookieUtil.setCookie('_ah_position', newPosition);
       
   246 };
       
   247 
       
   248 
       
   249 /**
       
   250  * Disable the logging console, and delete all cookies which were storing
       
   251  * logging console preferences.
       
   252  */
       
   253 AH.LoggingConsole.prototype.closeEverything = function() {
       
   254   this.cookieUtil.removeCookie('_ah_severity');
       
   255   this.cookieUtil.removeCookie('_ah_position');
       
   256   this.baseDiv.style.display = 'none';
       
   257 };