Rename Log class to DebugLog to avoid confusion
[gitphp.git] / include / DebugLog.class.php
blob:a/include/DebugLog.class.php -> blob:b/include/DebugLog.class.php
<?php <?php
/** /**
* GitPHP DebugLog * Debug logging class
*  
* Debug Logging class  
* *
* @author Christopher Han <xiphux@gmail.com> * @author Christopher Han <xiphux@gmail.com>
* @copyright Copyright (c) 2010 Christopher Han * @copyright Copyright (c) 2010 Christopher Han
* @package GitPHP * @package GitPHP
*/ */
  class GitPHP_DebugLog implements GitPHP_Observer_Interface
/**  
* Debug logging class  
*  
* @package GitPHP  
*/  
class GitPHP_DebugLog  
{ {
/** /**
* instance  
*  
* Stores the singleton instance  
*  
* @access protected  
* @static  
*/  
protected static $instance;  
   
/**  
* enabled  
*  
* Stores whether logging is enabled * Stores whether logging is enabled
* *
* @access protected * @var boolean
*/ */
protected $enabled = false; protected $enabled = false;
   
/** /**
* benchmark  
*  
* Stores whether benchmarking is enabled * Stores whether benchmarking is enabled
* *
* @access protected * @var boolean
*/ */
protected $benchmark = false; protected $benchmark = false;
   
/** /**
* startTime  
*  
* Stores the starting instant * Stores the starting instant
* *
* @access protected * @var float
*/ */
protected $startTime; protected $startTime;
   
/** /**
* startMem  
*  
* Stores the starting memory * Stores the starting memory
* *
* @access protected * @var int
*/ */
protected $startMem; protected $startMem;
   
/** /**
* entries  
*  
* Stores the log entries * Stores the log entries
* *
* @access protected * @var string[]
*/ */
protected $entries = array(); protected $entries = array();
   
/** /**
* GetInstance * Stores the timers
* *
* Returns the singleton instance * @var float[]
* */
* @access public protected $timers = array();
* @static  
* @return mixed instance of logging clas /**
  * @return GitPHP_DebugLog
*/ */
public static function GetInstance() public static function GetInstance()
{ {
if (!self::$instance) { static $instance;
$config = GitPHP_Config::GetInstance(); if (!$instance) $instance = new self();
self::$instance = new GitPHP_DebugLog($config->GetValue('debug', false), $config->GetValue('benchmark', false)); return $instance;
} }
   
return self::$instance; /**
} * You must use GetInstance()
  */
/** private function __construct()
* DestroyInstance {
* }
* Releases the singleton instance  
* /**
* @access public  
* @static  
*/  
public static function DestroyInstance()  
{  
self::$instance = null;  
}  
   
/**  
* __construct  
*  
* Constructor * Constructor
* *
* @access private * @param boolean $enabled whether log should be enabled
* @return Log object * @param boolean $benchmark whether benchmarking should be enabled
*/ */
private function __construct($enabled = false, $benchmark = false) public function init($enabled = false, $benchmark = false)
{ {
$this->startTime = microtime(true); $this->startTime = microtime(true);
$this->startMem = memory_get_usage(); $this->startMem = memory_get_usage();
   
$this->enabled = $enabled; $this->enabled = $enabled;
$this->benchmark = $benchmark; $this->benchmark = $benchmark;
} }
   
/** /**
* SetStartTime  
*  
* Sets start time * Sets start time
* *
* @access public  
* @param float $start starting microtime * @param float $start starting microtime
*/ */
public function SetStartTime($start) public function SetStartTime($start)
{ {
$this->startTime = $start; $this->startTime = $start;
} }
   
/** /**
* SetStartMemory  
*  
* Sets start memory * Sets start memory
* *
* @access public  
* @param integer $start starting memory * @param integer $start starting memory
*/ */
public function SetStartMemory($start) public function SetStartMemory($start)
{ {
$this->startMem = $start; $this->startMem = $start;
} }
   
/** /**
* Log * Shortcut to start timer
* */
  public function TimerStart()
  {
  if (!$this->benchmark) return;
  $this->Log('', '', 'start');
  }
   
  /**
  * Shortcut to stop timer
  *
  * @param $msg
  * @param $msg_data
  */
  public function TimerStop($msg, $msg_data = '')
  {
  if (!$this->benchmark) return;
  $this->Log($msg, $msg_data, 'stop');
  }
   
  /**
* Log an entry * Log an entry
* *
* @access public * @param string $msg message to log
* @param string $message message to log */
*/ public function Log($msg, $msg_data = '', $type = 'ts')
public function Log($message)  
{ {
if (!$this->enabled) if (!$this->enabled)
return; return;
   
$entry = array(); $entry = array();
   
if ($this->benchmark) { if ($type == 'start') {
$entry['time'] = microtime(true); array_push($this->timers, microtime(true));
$entry['mem'] = memory_get_usage(); return;
} } else if ($type == 'stop') {
  $timer = array_pop($this->timers);
$entry['msg'] = $message; $entry['time'] = $duration = microtime(true) - $timer;
$this->entries[] = $entry; foreach ($this->timers as &$item) $item += $duration;
} } else {
   
/**  
* GetEnabled  
*  
* Gets whether logging is enabled  
*  
* @access public  
* @return boolean true if logging is enabled  
*/  
public function GetEnabled()  
{  
return $this->enabled;  
}  
   
/**  
* SetEnabled  
*  
* Sets whether logging is enabled  
*  
* @access public  
* @param boolean $enable true if logging is enabled  
*/  
public function SetEnabled($enable)  
{  
$this->enabled = $enable;  
}  
   
/**  
* GetBenchmark  
*  
* Gets whether benchmarking is enabled  
*  
* @access public  
* @return boolean true if benchmarking is enabled  
*/  
public function GetBenchmark()  
{  
return $this->benchmark;  
}  
   
/**  
* SetBenchmark  
*  
* Sets whether benchmarking is enabled  
*  
* @access public  
* @param boolean $bench true if benchmarking is enabled  
*/  
public function SetBenchmark($bench)  
{  
$this->benchmark = $bench;  
}  
   
/**  
* GetEntries  
*  
* Calculates times and gets log entries  
*  
* @access public  
* @return array log entries  
*/  
public function GetEntries()  
{  
$data = array();  
   
if ($this->enabled) {  
   
if ($this->benchmark) { if ($this->benchmark) {
$endTime = microtime(true); $entry['time'] = (microtime(true) - $this->startTime);
$endMem = memory_get_usage(); $entry['reltime'] = true;
  $entry['mem'] = memory_get_usage();
$lastTime = $this->startTime;  
$lastMem = $this->startMem;  
   
$data[] = 'DEBUG: [' . $this->startTime . '] [' . $this->startMem . ' bytes] Start';  
   
}  
   
foreach ($this->entries as $entry) {  
if ($this->benchmark) {  
$data[] = 'DEBUG: [' . $entry['time'] . '] [' . ($entry['time'] - $this->startTime) . ' sec since start] [' . ($entry['time'] - $lastTime) . ' sec since last] [' . $entry['mem'] . ' bytes] [' . ($entry['mem'] - $this->startMem) . ' bytes since start] [' . ($entry['mem'] - $lastMem) . ' bytes since last] ' . $entry['msg'];  
$lastTime = $entry['time'];  
$lastMem = $entry['mem'];  
} else {  
$data[] = 'DEBUG: ' . $entry['msg'];  
}  
}  
   
if ($this->benchmark) {  
$data[] = 'DEBUG: [' . $endTime . '] [' . ($endTime - $this->startTime) . ' sec since start] [' . ($endTime - $lastTime) . ' sec since last] [' . $endMem . ' bytes] [' . ($endMem - $this->startMem) . ' bytes since start] [' . ($endMem - $lastMem) . ' bytes since last] End';  
} }
} }
   
return $data; $entry['name'] = $msg;
  $entry['value'] = $msg_data;
  $bt = explode("\n", new Exception());
  array_shift($bt);
  array_shift($bt);
  $entry['bt'] = implode("\n", $bt);
  $this->entries[] = $entry;
  }
   
  /**
  * Gets whether logging is enabled
  *
  * @return boolean true if logging is enabled
  */
  public function GetEnabled()
  {
  return $this->enabled;
  }
   
  /**
  * Sets whether logging is enabled
  *
  * @param boolean $enable true if logging is enabled
  */
  public function SetEnabled($enable)
  {
  $this->enabled = $enable;
  }
   
  /**
  * Gets whether benchmarking is enabled
  *
  * @return boolean true if benchmarking is enabled
  */
  public function GetBenchmark()
  {
  return $this->benchmark;
  }
   
  /**
  * Sets whether benchmarking is enabled
  *
  * @param boolean $bench true if benchmarking is enabled
  */
  public function SetBenchmark($bench)
  {
  $this->benchmark = $bench;
  }
   
  /**
  * Clears the log
  */
  public function Clear()
  {
  $this->entries = array();
  }
   
  /**
  * Gets the log entries
  *
  * @return array entry data
  */
  public function GetEntries()
  {
  return $this->entries;
  }
   
  /**
  * Notify that observable object changed
  *
  * @param GitPHP_Observable_Interface $object object
  * @param int $changeType type of change
  * @param array $args argument array
  */
  public function ObjectChanged($object, $changeType, $args = array())
  {
  if ($changeType !== GitPHP_Observer_Interface::LoggableChange)
  return;
   
  if (!$this->enabled)
  return;
   
  if (!isset($args[0]) || empty($args[0]))
  return;
   
  $msg = $args[0];
  $msg_data = !empty($args[1]) ? $args[1] : '';
  $type = !empty($args[2]) ? $args[2] : 'ts';
   
  $this->Log($msg, $msg_data, $type);
} }
   
} }
   
comments