Get rid of debug log singleton
[gitphp.git] / include / DebugLog.class.php
blob:a/include/DebugLog.class.php -> blob:b/include/DebugLog.class.php
--- a/include/DebugLog.class.php
+++ b/include/DebugLog.class.php
@@ -44,12 +44,36 @@
 	protected $entries = array();
 
 	/**
+	 * Stores the timers
+	 *
+	 * @var float[]
+	 */
+	protected $timers = array();
+
+	/**
+	 * @return GitPHP_DebugLog
+	 */
+	public static function GetInstance()
+	{
+		static $instance;
+		if (!$instance) $instance = new self();
+		return $instance;
+	}
+
+	/**
+	 * You must use GetInstance()
+	 */
+	private function __construct()
+	{
+	}
+
+	/**
 	 * Constructor
 	 *
 	 * @param boolean $enabled whether log should be enabled
 	 * @param boolean $benchmark whether benchmarking should be enabled
 	 */
-	public function __construct($enabled = false, $benchmark = false)
+	public function init($enabled = false, $benchmark = false)
 	{
 		$this->startTime = microtime(true);
 		$this->startMem = memory_get_usage();
@@ -79,104 +103,118 @@
 	}
 
 	/**
+	 * 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
 	 *
-	 * @param string $message message to log
-	 */
-	public function Log($message)
+	 * @param string $msg message to log
+	 */
+	public function Log($msg, $msg_data = '', $type = 'ts')
 	{
 		if (!$this->enabled)
 			return;
 
 		$entry = array();
-		
-		if ($this->benchmark) {
-			$entry['time'] = microtime(true);
-			$entry['mem'] = memory_get_usage();
-		}
-
-		$entry['msg'] = $message;
-		$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;
-	}
-
-	/**
-	 * Gets log entries
-	 *
-	 * @return string[] log entries
-	 */
-	public function GetEntries()
-	{
-		$data = array();
-	
-		if ($this->enabled) {
-
+
+		if ($type == 'start') {
+			array_push($this->timers, microtime(true));
+			return;
+		} else if ($type == 'stop') {
+			$timer = array_pop($this->timers);
+			$entry['time'] = $duration = microtime(true) - $timer;
+			foreach ($this->timers as &$item) $item += $duration;
+		} else {
 			if ($this->benchmark) {
-				$endTime = microtime(true);
-				$endMem = 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';
+				$entry['time'] = (microtime(true) - $this->startTime);
+				$entry['reltime'] = true;
+				$entry['mem'] = memory_get_usage();
 			}
 		}
 
-		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;
 	}
 
 	/**
@@ -198,8 +236,10 @@
 			return;
 
 		$msg = $args[0];
-
-		$this->Log($msg);
+		$msg_data = !empty($args[1]) ? $args[1] : '';
+		$type = !empty($args[2]) ? $args[2] : 'ts';
+
+		$this->Log($msg, $msg_data, $type);
 	}
 
 }

comments