[ Index ]

PHP Cross Reference of MantisBT

title

Body

[close]

/core/ -> logging_api.php (source)

   1  <?php
   2  # MantisBT - A PHP based bugtracking system
   3  
   4  # MantisBT is free software: you can redistribute it and/or modify
   5  # it under the terms of the GNU General Public License as published by
   6  # the Free Software Foundation, either version 2 of the License, or
   7  # (at your option) any later version.
   8  #
   9  # MantisBT is distributed in the hope that it will be useful,
  10  # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  # GNU General Public License for more details.
  13  #
  14  # You should have received a copy of the GNU General Public License
  15  # along with MantisBT.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Logging API
  19   *
  20   * Provides functionality to log system events other than bug related history.
  21   *
  22   * @package CoreAPI
  23   * @subpackage LoggingAPI
  24   * @copyright Copyright (C) 2000 - 2002  Kenzaburo Ito - kenito@300baud.org
  25   * @copyright Copyright (C) 2002 - 2011  MantisBT Team - mantisbt-dev@lists.sourceforge.net
  26   * @link http://www.mantisbt.org
  27   *
  28   * @uses config_api.php
  29   * @uses constant_inc.php
  30   * @uses event_api.php
  31   * @uses utility_api.php
  32   */
  33  
  34  require_api( 'config_api.php' );
  35  require_api( 'constant_inc.php' );
  36  require_api( 'event_api.php' );
  37  require_api( 'utility_api.php' );
  38  
  39  $g_log_levels = array(
  40      LOG_EMAIL => 'MAIL',
  41      LOG_EMAIL_RECIPIENT => 'RECIPIENT',
  42      LOG_FILTERING => 'FILTER',
  43      LOG_AJAX => 'AJAX',
  44      LOG_LDAP => 'LDAP',
  45      LOG_DATABASE => 'DB',
  46  );
  47  
  48  /**
  49   * Log an event
  50   * @param int $p_level Valid debug log level
  51   * @param string|array $p_msg Either a string, or an array structured as (string,execution time)
  52   * @param object $p_backtrace [Optional] debug_backtrace() stack to use 
  53   * @return null
  54   */
  55  function log_event( $p_level, $p_msg, $p_backtrace = null ) {
  56      global $g_log_levels;
  57  
  58      # check to see if logging is enabled
  59      $t_sys_log = config_get_global( 'log_level' );
  60  
  61      if ( 0 == ( $t_sys_log & $p_level ) ) {
  62          return;
  63      }
  64  
  65      if( is_array( $p_msg ) ) {
  66          $t_event = $p_msg;
  67          $s_msg = var_export( $p_msg, true );
  68      } else {
  69          $t_event = array( $p_msg, 0 );
  70          $s_msg = $p_msg;
  71      }
  72  
  73      if( $p_backtrace === null ) {
  74          $t_backtrace = debug_backtrace();
  75      } else {
  76          $t_backtrace = $p_backtrace;
  77      }
  78      $t_caller = basename( $t_backtrace[0]['file'] );
  79      $t_caller .= ":" . $t_backtrace[0]['line'];
  80  
  81      # Is this called from another function?
  82      if( isset( $t_backtrace[1] ) ) {
  83          $t_caller .= ' ' . $t_backtrace[1]['function'] . '()';
  84      } else {
  85          # or from a script directly?
  86          $t_caller .= ' ' . $_SERVER['SCRIPT_NAME'];
  87      }
  88  
  89      $t_now = date( config_get_global( 'complete_date_format' ) );
  90      $t_level = $g_log_levels[$p_level];
  91  
  92      $t_plugin_event = '[' . $t_level . '] ' . $p_msg;
  93      if( function_exists( 'event_signal' ) )
  94          event_signal( 'EVENT_LOG', array( $t_plugin_event ) );
  95  
  96      $t_log_destination = config_get_global( 'log_destination' );
  97  
  98      if ( is_blank( $t_log_destination ) ) {
  99          $t_destination = '';
 100      } else {
 101          @list( $t_destination, $t_modifiers ) = explode( ':', $t_log_destination, 2 );
 102      }
 103  
 104      switch( $t_destination ) {
 105          case 'none':
 106              break;
 107          case 'file':
 108              $t_php_event = $t_now . ' ' . $t_level . ' ' . $s_msg;
 109              error_log( $t_php_event . PHP_EOL, 3, $t_modifiers );
 110              break;
 111          case 'page':
 112              global $g_log_events;
 113              $g_log_events[] = array( time(), $p_level, $t_event, $t_caller);
 114              break;
 115          case 'firebug':
 116              if( !class_exists( 'FirePHP' ) ) {
 117                  if( file_exists( config_get_global( 'library_path' ) . 'FirePHPCore' . DIRECTORY_SEPARATOR . 'FirePHP.class.php' ) ) {
 118                      require_lib( 'FirePHPCore' . DIRECTORY_SEPARATOR . 'FirePHP.class.php' );
 119                  }
 120              }
 121              if( class_exists( 'FirePHP' ) ) {
 122                  static $firephp;
 123                  if( $firephp === null ) {
 124                      $firephp = FirePHP::getInstance(true);
 125                  }
 126                  $t_php_event = $t_now . ' ' . $t_level . ' ' . $s_msg;
 127                  $firephp->log( $p_msg, $t_php_event );
 128                  return;
 129              }
 130              // if firebug is not available, fall through
 131          default:
 132              # use default PHP error log settings
 133              $t_php_event = $t_now . ' ' . $t_level . ' ' . $s_msg;
 134              error_log( $t_php_event . PHP_EOL );
 135              break;
 136      }
 137  }
 138  
 139  function log_print_to_page() {
 140      if ( config_get_global( 'log_destination' ) === 'page' && auth_is_user_authenticated() && access_has_global_level( config_get( 'show_log_threshold' ) ) ) {
 141          global $g_log_events, $g_log_levels;
 142          echo "\n\n<!--Mantis Debug Log Output-->";
 143          echo "<hr />\n";
 144          echo "<table id=\"log-event-list\">\n";
 145          echo "\t<thead>\n";
 146          echo "\t\t<tr>\n";
 147          echo "\t\t\t<th>" . lang_get( 'log_page_number' ) . "</th>\n";
 148          echo "\t\t\t<th>" . lang_get( 'log_page_time' ) . "</th>\n";
 149          echo "\t\t\t<th>" . lang_get( 'log_page_caller' ) . "</th>\n";
 150          echo "\t\t\t<th>" . lang_get( 'log_page_event' ) . "</th>\n";
 151          echo "\t\t</tr>\n";
 152          echo "\t</thead>\n";
 153          echo "\t<tbody>\n";
 154  
 155          $t_unique_queries_count = 0;
 156          $t_total_query_execution_time = 0;
 157          $t_unique_queries = array();
 158          $t_total_queries_count = 0;
 159          $t_total_event_count = count( $g_log_events );
 160  
 161          if( $t_total_event_count == 0 ) {
 162              echo "\t</tbody>\n\t</table>\n";
 163              echo "<!--END Mantis Debug Log Output-->\n\n";
 164              return;
 165          }
 166  
 167          for ( $i = 0; $i < $t_total_event_count; $i++ ) {
 168              if( $g_log_events[$i][1] == LOG_DATABASE ) {
 169                  if( !in_array( $g_log_events[$i][2][0], $t_unique_queries ) ) {
 170                      $t_unique_queries_count++;
 171                      $g_log_events[$i][2][2] = false;
 172                      array_push( $t_unique_queries, $g_log_events[$i][2][0] );
 173                  } else {
 174                      $g_log_events[$i][2][2] = true;
 175                  }
 176                  $t_total_query_execution_time += $g_log_events[$i][2][1];
 177              }
 178          }
 179  
 180          $t_count = array();
 181          foreach( $g_log_events as $t_log_event ) {
 182              $t_level = $g_log_levels[$t_log_event[1]];
 183              $t_count[$t_log_event[1]]++;
 184              switch ( $t_log_event[1] ) {
 185                  case LOG_DATABASE:
 186                      $t_total_queries_count++;
 187                      $t_query_duplicate_class = '';
 188                      if ( $t_log_event[2][2] ) {
 189                          $t_query_duplicate_class = ' class="duplicate-query"';
 190                      }
 191                      echo "\t\t<tr$t_query_duplicate_class><td>" . $t_level . '-' . $t_count[$t_log_event[1]] . "</td><td>" . $t_log_event[2][1] . "</td><td>" . string_html_specialchars ( $t_log_event[3] ) . "</td><td>" . string_html_specialchars( $t_log_event[2][0] ) . "</td></tr>\n";
 192                      break;
 193                  default:
 194                      echo "\t\t<tr><td>" . $t_level . '-' . $t_count[$t_log_event[1]] . "</td><td>" . $t_log_event[2][1] . "</td><td>" . string_html_specialchars ( $t_log_event[3] ) . "</td><td>" . string_html_specialchars( $t_log_event[2][0] ) . "</td></tr>\n";
 195              }
 196          }
 197  
 198          # output any summary data
 199          if ( $t_unique_queries_count != 0 ) {
 200              $t_unique_queries_executed = sprintf( lang_get( 'unique_queries_executed' ), $t_unique_queries_count );
 201              echo "\t\t<tr><td>" . $g_log_levels[LOG_DATABASE] . '</td><td colspan="3">' . $t_unique_queries_executed . "</td></tr>\n";
 202          }
 203          if ( $t_total_queries_count != 0 ) {
 204              $t_total_queries_executed = sprintf( lang_get( 'total_queries_executed' ), $t_total_queries_count );
 205              echo "\t\t<tr><td>" . $g_log_levels[LOG_DATABASE] . '</td><td colspan="3">' . $t_total_queries_executed . "</td></tr>\n";
 206          }
 207          if ( $t_total_query_execution_time != 0 ) {
 208              $t_total_query_time = sprintf( lang_get( 'total_query_execution_time' ), $t_total_query_execution_time );
 209              echo "\t\t<tr><td>" . $g_log_levels[LOG_DATABASE] . '</td><td colspan="3">' . $t_total_query_time . "</td></tr>\n";
 210          }
 211          echo "\t</tbody>\n\t</table>\n";
 212      }
 213      echo "<!--END Mantis Debug Log Output-->\n\n";
 214  }


Generated: Thu Jul 28 15:48:31 2011 Cross-referenced by PHPXref 0.7