Get rid of debug log singleton
Get rid of debug log singleton

--- a/include/DebugLog.class.php
+++ b/include/DebugLog.class.php
@@ -9,13 +9,6 @@
 class GitPHP_DebugLog implements GitPHP_Observer_Interface
 {
 	/**
-	 * Stores the singleton instance
-	 *
-	 * @var GitPHP_DebugLog
-	 */
-	protected static $instance;
-
-	/**
 	 * Stores whether logging is enabled
 	 *
 	 * @var boolean
@@ -49,29 +42,6 @@
 	 * @var string[]
 	 */
 	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
@@ -79,7 +49,7 @@
 	 * @param boolean $enabled whether log 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->startMem = memory_get_usage();

--- a/include/controller/ControllerBase.class.php
+++ b/include/controller/ControllerBase.class.php
@@ -87,9 +87,7 @@
 	{
 		$this->config = GitPHP_Config::GetInstance();
 
-		$log = GitPHP_DebugLog::GetInstance();
-		if ($log && $log->GetEnabled())
-			$this->log = $log;
+		$this->EnableLogging();
 
 		$this->InitializeGitExe();
 
@@ -192,6 +190,26 @@
 	}
 
 	/**
+	 * Enable logging
+	 */
+	public function EnableLogging()
+	{
+		if ($this->log)
+			return;
+
+		$debug = $this->config->GetValue('debug', false);
+		if ($debug) {
+			$this->log = new GitPHP_DebugLog($debug, $this->config->GetValue('benchmark', false));
+			$this->log->SetStartTime(GITPHP_START_TIME);
+			$this->log->SetStartMemory(GITPHP_START_MEM);
+			if ($this->exe)
+				$this->exe->AddObserver($this->log);
+			if ($this->projectList)
+				$this->projectList->AddObserver($this->log);
+		}
+	}
+
+	/**
 	 * Disable logging
 	 */
 	protected function DisableLogging()
@@ -200,6 +218,7 @@
 			return;
 
 		$this->projectList->RemoveObserver($this->log);
+		$this->exe->RemoveObserver($this->log);
 
 		$this->log->SetEnabled(false);
 

file:a/index.php -> file:b/index.php
--- a/index.php
+++ b/index.php
@@ -101,14 +101,6 @@
 		GitPHP_Resource::Instantiate(GitPHP_Config::GetInstance()->GetValue('locale', 'en_US'));
 	}
 
-	/*
-	 * Debug
-	 */
-	if (GitPHP_DebugLog::GetInstance()->GetEnabled()) {
-		GitPHP_DebugLog::GetInstance()->SetStartTime(GITPHP_START_TIME);
-		GitPHP_DebugLog::GetInstance()->SetStartMemory(GITPHP_START_MEM);
-	}
-
 	$controller = GitPHP_Controller::GetController((isset($_GET['a']) ? $_GET['a'] : null));
 	if ($controller) {
 		$controller->RenderHeaders();
@@ -147,18 +139,18 @@
 GitPHP_Resource::DestroyInstance();
 GitPHP_Config::DestroyInstance();
 
-$log = $controller->GetLog();
-if ($log && $log->GetEnabled()) {
-	$entries = $log->GetEntries();
-	foreach ($entries as $logline) {
-		echo "<br />\n" . htmlspecialchars($logline, ENT_QUOTES, 'UTF-8', true);
+if (isset($controller)) {
+	$log = $controller->GetLog();
+	if ($log && $log->GetEnabled()) {
+		$entries = $log->GetEntries();
+		foreach ($entries as $logline) {
+			echo "<br />\n" . htmlspecialchars($logline, ENT_QUOTES, 'UTF-8', true);
+		}
+		unset($logline);
+		unset($entries);
 	}
-	unset($logline);
-	unset($entries);
+	unset($controller);
 }
-unset($controller);
-
-GitPHP_DebugLog::DestroyInstance();
 
 ?>
 

comments