[ Index ]

MailPress 544

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]     [ Statistics ]    

title

Body

[close]

/mp-includes/class/ -> MP_Log.class.php (source)

   1  <?php
   2  class MP_Log
   3  {
   4      const noMP_Log    = 123456789;
   5  
   6  	function __construct($name, $args = array())
   7      {
   8          $this->errors     = array (    1     => 'E_ERROR', 
   9                              2     => 'E_WARNING', 
  10                              4     => 'E_PARSE', 
  11                              8     => 'E_NOTICE', 
  12                              16     => 'E_CORE_ERROR', 
  13                              32     => 'E_CORE_WARNING', 
  14                              64     => 'E_COMPILE_ERROR', 
  15                              128     => 'E_COMPILE_WARNING', 
  16                              256     => 'E_USER_ERROR', 
  17                              512     => 'E_USER_WARNING * ', 
  18                              1024     => 'E_USER_NOTICE', 
  19                              2048     => 'E_STRICT', 
  20                              4096     => 'E_RECOVERABLE_ERROR', 
  21                              8191     => 'E_ALL' ); 
  22  
  23  
  24          $defaults = array(    'path'        => MP_ABSPATH,
  25                          'force'        => false,
  26                          'option_name'    => 'general',
  27          );
  28          $r = wp_parse_args( $args, $defaults );
  29          extract( $r );
  30  
  31          $this->name     = $name;
  32          $this->path     = $path . 'tmp';
  33          $this->option_name= $option_name;
  34  
  35          global $wpdb;
  36          $this->ftmplt    = 'MP_Log' . '_' . $wpdb->blogid . '_' . $this->name . '_';
  37  
  38          $this->file     = $this->path . '/' . $this->ftmplt . gmdate('Ymd', current_time('timestamp')) . '.txt';
  39  
  40          $logs = get_option(MailPress::option_name_logs);
  41          $this->log_options = (isset($logs[$this->option_name])) ? $logs[$this->option_name] : MailPress::$default_option_logs;
  42  
  43          $this->level     = (isset($this->log_options['level']))    ? (int) $this->log_options['level']     : self::noMP_Log ;
  44          $this->levels    = array (    1     => 1, 
  45                              2     => 2, 
  46                              4     => 4, 
  47                              8     => 8, 
  48                              16     => 16, 
  49                              32     => 32, 
  50                              64     => 64, 
  51                              128     => 128, 
  52                              256     => 256, 
  53                              512     => 512, 
  54                              1024     => 1024, 
  55                              2048     => 2048, 
  56                              4096     => 4096, 
  57                              8191     => 8191 );
  58          if ($force) 
  59          {
  60              foreach ($this->levels as $k => $v) $this->levels[$k] = 0;
  61              $this->level = 0;
  62          }
  63          if (!is_dir($this->path)) $this->level = self::noMP_Log ;
  64          if (self::noMP_Log == $this->level) return;
  65          if ( 0  != $this->level) set_error_handler(array($this, 'logError'), $this->level);
  66  
  67          $this->start($force);
  68      }
  69  
  70  	function start($force = false)
  71      {
  72          $plugin_version = ' **** (' . MP_Version . ')';
  73  
  74          $page = '';
  75          $page = $_SERVER['REQUEST_URI'];
  76  
  77          $this->data = "\n";
  78  
  79          if ($force) 
  80              $this->log (" **** Start logging **** {$this->name} *** log forced$plugin_version");
  81          elseif (!empty($page))
  82              $this->log (" **** Start logging **** {$this->name} *** level : {$this->level}$plugin_version **** $page");
  83          else
  84              $this->log (" **** Start logging **** {$this->name} *** level : {$this->level}$plugin_version");
  85  
  86  // purge log
  87          $this->dopurge();
  88  
  89          ob_start();
  90      }
  91  
  92  	function restart()
  93      {
  94          $this->stop();
  95          $this->data = "";
  96          ob_start();
  97      }
  98  
  99  	function log($x, $level=0)
 100      {
 101          if (stripos($x, 'simplepie') == true) return;
 102          if (strpos($x, ' WP_Http') == true)   return;
 103  
 104          if (self::noMP_Log    == $this->level) return;
 105          if ($level <= $this->level) $this->data .= date_i18n('Y-m-d H:i:s u') . " -- " . $x . "\n";
 106      }
 107  
 108  	function logError($error_level, $error_message, $error_file, $error_line, $error_context=false)
 109      { 
 110          if (strpos($error_message, 'Please use the instanceof operator') == true) return;
 111          $this->log ("PHP [" . $this->errors[$error_level] . "] $error_level : $error_message in $error_file at line $error_line ", $error_level);
 112      }
 113  
 114  	function stop()
 115      {
 116              if (self::noMP_Log == $this->level) return;
 117              if (0   != $this->level) restore_error_handler();
 118  
 119              $log = (ob_get_length()) ? ob_get_contents() : '';
 120          if (ob_get_length()) ob_end_clean();
 121          if (!empty($log)) $this->log($log);
 122  
 123          $this->fh = fopen($this->file , 'a+');
 124          fputs($this->fh, $this->data); 
 125          fclose($this->fh); 
 126      }
 127  
 128  	function end($y = true)
 129      {
 130              if (self::noMP_Log == $this->level) return;
 131              if (0   != $this->level) restore_error_handler();
 132  
 133              $log = (ob_get_length()) ? ob_get_contents() : '';
 134          if (ob_get_length()) ob_end_clean();
 135          if (!empty($log)) $this->log($log);
 136  
 137          $y = ($y) ? "TRUE" : "FALSE";
 138  
 139          $this->log (" **** End   logging **** {$this->name} *** level : $this->level **** status : $y ");
 140  
 141          $this->fh = fopen($this->file , 'a+');
 142          fputs($this->fh, $this->data); 
 143          fclose($this->fh); 
 144  
 145  // mem'ries ...
 146          $xs = array(     'this->data', 'this->errors', 'this->name', 'this->path', 'this->plug', 'this->ftmplt', 'this->level', 'this->levels', 'this->lastpurge', 'this->lognbr');
 147          foreach ($xs as $x) if (isset($$x)) unset($$x);
 148      }
 149  
 150  	function dopurge()
 151      {
 152          $now = date_i18n('Ymd', current_time('timestamp'));
 153          $this->lastpurge= (isset($this->log_options['lastpurge'])) ? $this->log_options['lastpurge']         : $now;
 154          $this->lognbr     = (isset($this->log_options['lognbr']))    ? (int) $this->log_options['lognbr']     : 1;
 155  
 156          if ($now == $this->lastpurge) return;
 157  
 158          $this->log_options['lastpurge'] = $now;
 159          $this->log_options['lognbr']    = $this->lognbr;
 160  
 161          $logs = get_option(MailPress::option_name_logs);
 162          $logs[$this->option_name] = $this->log_options;
 163          update_option (MailPress::option_name_logs, $logs);
 164  
 165  
 166          $xs = array();
 167          $l = opendir($this->path);
 168          if ($l) 
 169          {
 170              while ( ($file = readdir($l)) !== false ) if ( preg_match('#' . $this->ftmplt . '[0-9]#', $file) ) $xs[] = $file;
 171              @closedir($l);
 172          }
 173  
 174          sort($xs);
 175          $y = count($xs) - $this->lognbr + 1;
 176  
 177          while ($y > 0)
 178          {
 179              @unlink($file = $this->path . '/' . array_shift($xs));
 180              $this->log (" **** Purged log file **** " . $file);
 181              $y--;
 182          }
 183      }
 184  }


Generated: Thu Apr 28 18:38:52 2016 Cross-referenced by PHPXref 0.7.1