Browse Source

Don't mix data model and UI: use template to display debug log

Chris Han 10 years ago
parent
commit
08261ef691
5 changed files with 62 additions and 51 deletions
  1. 7 0
      css/gitphpskin.css
  2. 10 41
      include/DebugLog.class.php
  3. 5 0
      include/controller/ControllerBase.class.php
  4. 0 10
      index.php
  5. 40 0
      templates/debug.tpl

+ 7 - 0
css/gitphpskin.css

@@ -612,12 +612,19 @@ span.searchmatch {
 .debug_toggle {
 	color: #88a; border-bottom: 1px dashed blue;
 }
+
 .debug_key {
 	background: #ccf; border-bottom: 1px solid #888;
 }
+
 .debug_value {
 	background: #ccc; border-bottom: 1px solid #888;
 }
+
+.debug_value .debug_addl {
+	font-style: italic;
+}
+
 .debug_time {
 	background: #cff; border-bottom: 1px solid #888;
 }

+ 10 - 41
include/DebugLog.class.php

@@ -207,6 +207,16 @@ class GitPHP_DebugLog implements GitPHP_Observer_Interface
 		$this->entries = array();
 	}
 
+	/**
+	 * Gets the log entries
+	 *
+	 * @return array entry data
+	 */
+	public function GetEntries()
+	{
+		return $this->entries;
+	}
+
 	/**
 	 * Notify that observable object changed
 	 *
@@ -232,45 +242,4 @@ class GitPHP_DebugLog implements GitPHP_Observer_Interface
 		$this->Log($msg, $msg_data, $type);
 	}
 
-	public function PrintHtml()
-	{
-		if (!$this->enabled) return;
-
-		foreach ($this->entries as $i => $e) {
-			if (strlen($e['value']) > 512) {
-				$contents  = htmlspecialchars(substr($e['value'], 0, 512) . "...");
-				$contents .= "\n\n<i>" . (strlen($e['value']) - 512) . " bytes more in output</i>";
-			} else {
-				$contents = htmlspecialchars($e['value']);
-			}
-			echo "<tr>
-				<td class='debug_key'>$e[name]</td>
-				<td class='debug_value'>
-					" . nl2br($contents) . ($contents != "" ? "<br§ />" : "") . "
-					<span class='debug_toggle'>trace</span>&nbsp;
-					<div class='debug_bt'>$e[bt]</div>
-				</td>
-				<td class='debug_time'>
-					" . ($e['time'] ? sprintf("%.1f", $e['time'] * 1000) : '') . "
-					" . ($e['time'] ? (!empty($e['reltime']) ? " ms from start" : " ms") : '') . "
-				</td>
-			</tr>";
-		}
-	}
-
-	public function PrintHtmlHeader()
-	{
-		if (!$this->enabled) return;
-
-		echo
-<<<HEREDOC
-		<table class="debug"><tbody>
-HEREDOC;
-	}
-
-	public function PrintHtmlFooter()
-	{
-		if (!$this->enabled) return;
-		echo '</tbody></table>';
-	}
 }

+ 5 - 0
include/controller/ControllerBase.class.php

@@ -652,6 +652,11 @@ abstract class GitPHP_ControllerBase
 
 		if ($this->projectList)
 			$log->Log('MemoryCache count: ' . $this->projectList->GetMemoryCache()->GetCount());
+
+		if ($log->GetEnabled()) {
+			$this->tpl->assign('debuglog', $log);
+			$this->tpl->display('debug.tpl');
+		}
 	}
 
 	/**

+ 0 - 10
index.php

@@ -100,14 +100,4 @@ try {
 
 unset($router);
 
-if (isset($controller)) {
-	$log = $controller->GetLog();
-	if ($log && $log->GetEnabled()) {
-		$log->PrintHtmlHeader();
-		$log->PrintHtml();
-		$log->PrintHtmlFooter();
-	}
-	unset($controller);
-}
-
 ?>

+ 40 - 0
templates/debug.tpl

@@ -0,0 +1,40 @@
+{*
+ * Debug
+ *
+ * Debug log template
+ *
+ * @author Christopher Han <xiphux@gmail.com>
+ * @copyright Copyright (c) 2013 Christopher Han
+ * @packge GitPHP
+ * @subpackage Template
+ *}
+<table class"debug">
+<tbody>
+{foreach from=$debuglog->GetEntries() item=entry}
+  <tr>
+    <td class="debug_key">
+      {$entry.name|escape}
+    </td>
+    <td class="debug_value">
+      {if $entry.value}
+        {if strlen($entry.value) > 512}
+          {$entry.value|truncate:512:'...'|escape}
+          <br />
+          <span class="debug_addl">{strlen($entry.value)-512} bytes more in output</span>
+        {else}
+          {$entry.value|escape}
+        {/if}
+        <br />
+      {/if}
+      <span class="debug_toggle">trace</span>
+      <div class="debug_bt">{$entry.bt|escape}</div>
+    </td>
+    <td class="debug_time">
+      {if $entry.time}
+        {$entry.time*1000|string_format:"%.1f"} {if $entry.reltime}ms from start{else}ms{/if}
+      {/if}
+    </td>
+  </tr>
+{/foreach}
+</tbody>
+</table>