<?php |
<?php |
/** |
/** |
* 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 |
class GitPHP_DebugLog implements GitPHP_Observer_Interface |
{ |
{ |
/** |
/** |
* Stores the singleton instance |
|
* |
|
* @var GitPHP_DebugLog |
|
*/ |
|
protected static $instance; |
|
|
|
/** |
|
* Stores whether logging is enabled |
* Stores whether logging is enabled |
* |
* |
* @var boolean |
* @var boolean |
*/ |
*/ |
protected $enabled = false; |
protected $enabled = false; |
|
|
/** |
/** |
* Stores whether benchmarking is enabled |
* Stores whether benchmarking is enabled |
* |
* |
* @var boolean |
* @var boolean |
*/ |
*/ |
protected $benchmark = false; |
protected $benchmark = false; |
|
|
/** |
/** |
* Stores the starting instant |
* Stores the starting instant |
* |
* |
* @var float |
* @var float |
*/ |
*/ |
protected $startTime; |
protected $startTime; |
|
|
/** |
/** |
* Stores the starting memory |
* Stores the starting memory |
* |
* |
* @var int |
* @var int |
*/ |
*/ |
protected $startMem; |
protected $startMem; |
|
|
/** |
/** |
* Stores the log entries |
* Stores the log entries |
* |
* |
* @var string[] |
* @var string[] |
*/ |
*/ |
protected $entries = array(); |
protected $entries = array(); |
|
|
/** |
|
* Returns the singleton instance |
|
* |
|
* @return GitPHP_DebugLog instance of logging class |
|
*/ |
|
public static function GetInstance() |
|
{ |
|
if (!self::$instance) { |
|
$config = GitPHP_Config::GetInstance(); |
|
self::$instance = new GitPHP_DebugLog($config->GetValue('debug', false), $config->GetValue('benchmark', false)); |
|
} |
|
|
|
return self::$instance; |
|
} |
|
|
|
/** |
|
* Releases the singleton instance |
|
*/ |
|
public static function DestroyInstance() |
|
{ |
|
self::$instance = null; |
|
} |
|
|
|
/** |
/** |
* Constructor |
* Constructor |
* |
* |
* @param boolean $enabled whether log should be enabled |
* @param boolean $enabled whether log should be enabled |
* @param boolean $benchmark whether benchmarking should be enabled |
* @param boolean $benchmark whether benchmarking should be enabled |
*/ |
*/ |
private function __construct($enabled = false, $benchmark = false) |
public function __construct($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; |
} |
} |
|
|
/** |
/** |
* Sets start time |
* Sets start time |
* |
* |
* @param float $start starting microtime |
* @param float $start starting microtime |
*/ |
*/ |
public function SetStartTime($start) |
public function SetStartTime($start) |
{ |
{ |
$this->startTime = $start; |
$this->startTime = $start; |
} |
} |
|
|
/** |
/** |
* Sets start memory |
* Sets start memory |
* |
* |
* @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 an entry |
* Log an entry |
* |
* |
* @param string $message message to log |
* @param string $message message to log |
*/ |
*/ |
public function Log($message) |
public function Log($message) |
{ |
{ |
if (!$this->enabled) |
if (!$this->enabled) |
return; |
return; |
|
|
$entry = array(); |
$entry = array(); |
|
|
if ($this->benchmark) { |
if ($this->benchmark) { |
$entry['time'] = microtime(true); |
$entry['time'] = microtime(true); |
$entry['mem'] = memory_get_usage(); |
$entry['mem'] = memory_get_usage(); |
} |
} |
|
|
$entry['msg'] = $message; |
$entry['msg'] = $message; |
$this->entries[] = $entry; |
$this->entries[] = $entry; |
} |
} |
|
|
/** |
/** |
* Gets whether logging is enabled |
* Gets whether logging is enabled |
* |
* |
* @return boolean true if logging is enabled |
* @return boolean true if logging is enabled |
*/ |
*/ |
public function GetEnabled() |
public function GetEnabled() |
{ |
{ |
return $this->enabled; |
return $this->enabled; |
} |
} |
|
|
/** |
/** |
* Sets whether logging is enabled |
* Sets whether logging is enabled |
* |
* |
* @param boolean $enable true if logging is enabled |
* @param boolean $enable true if logging is enabled |
*/ |
*/ |
public function SetEnabled($enable) |
public function SetEnabled($enable) |
{ |
{ |
$this->enabled = $enable; |
$this->enabled = $enable; |
} |
} |
|
|
/** |
/** |
* Gets whether benchmarking is enabled |
* Gets whether benchmarking is enabled |
* |
* |
* @return boolean true if benchmarking is enabled |
* @return boolean true if benchmarking is enabled |
*/ |
*/ |
public function GetBenchmark() |
public function GetBenchmark() |
{ |
{ |
return $this->benchmark; |
return $this->benchmark; |
} |
} |
|
|
/** |
/** |
* Sets whether benchmarking is enabled |
* Sets whether benchmarking is enabled |
* |
* |
* @param boolean $bench true if benchmarking is enabled |
* @param boolean $bench true if benchmarking is enabled |
*/ |
*/ |
public function SetBenchmark($bench) |
public function SetBenchmark($bench) |
{ |
{ |
$this->benchmark = $bench; |
$this->benchmark = $bench; |
} |
} |
|
|
/** |
/** |
* Gets log entries |
* Gets log entries |
* |
* |
* @return string[] log entries |
* @return string[] log entries |
*/ |
*/ |
public function GetEntries() |
public function GetEntries() |
{ |
{ |
$data = array(); |
$data = array(); |
|
|
if ($this->enabled) { |
if ($this->enabled) { |
|
|
if ($this->benchmark) { |
if ($this->benchmark) { |
$endTime = microtime(true); |
$endTime = microtime(true); |
$endMem = memory_get_usage(); |
$endMem = memory_get_usage(); |
|
|
$lastTime = $this->startTime; |
$lastTime = $this->startTime; |
$lastMem = $this->startMem; |
$lastMem = $this->startMem; |
|
|
$data[] = 'DEBUG: [' . $this->startTime . '] [' . $this->startMem . ' bytes] Start'; |
$data[] = 'DEBUG: [' . $this->startTime . '] [' . $this->startMem . ' bytes] Start'; |
|
|
} |
} |
|
|
foreach ($this->entries as $entry) { |
foreach ($this->entries as $entry) { |
if ($this->benchmark) { |
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']; |
$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']; |
$lastTime = $entry['time']; |
$lastMem = $entry['mem']; |
$lastMem = $entry['mem']; |
} else { |
} else { |
$data[] = 'DEBUG: ' . $entry['msg']; |
$data[] = 'DEBUG: ' . $entry['msg']; |
} |
} |
} |
} |
|
|
if ($this->benchmark) { |
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'; |
$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; |
return $data; |
} |
} |
|
|
/** |
/** |
* Notify that observable object changed |
* Notify that observable object changed |
* |
* |
* @param GitPHP_Observable_Interface $object object |
* @param GitPHP_Observable_Interface $object object |
* @param int $changeType type of change |
* @param int $changeType type of change |
* @param array $args argument array |
* @param array $args argument array |
*/ |
*/ |
public function ObjectChanged($object, $changeType, $args = array()) |
public function ObjectChanged($object, $changeType, $args = array()) |
{ |
{ |
if ($changeType !== GitPHP_Observer_Interface::LoggableChange) |
if ($changeType !== GitPHP_Observer_Interface::LoggableChange) |
return; |
return; |
|
|
if (!$this->enabled) |
if (!$this->enabled) |
return; |
return; |
|
|
if (!isset($args[0]) || empty($args[0])) |
if (!isset($args[0]) || empty($args[0])) |
return; |
return; |
|
|
$msg = $args[0]; |
$msg = $args[0]; |
|
|
$this->Log($msg); |
$this->Log($msg); |
} |
} |
|
|
} |
} |
|
|