← Back to team overview

anewt-developers team mailing list archive

[Branch ~uws/anewt/anewt.uws] Rev 1774: [logging] Add log handler for Firebug/FirePHP

 

------------------------------------------------------------
revno: 1774
committer: Wouter Bolsterlee <uws@xxxxxxxxx>
branch nick: anewt.uws
timestamp: Fri 2010-03-26 22:34:27 +0100
message:
  [logging] Add log handler for Firebug/FirePHP
  
  The AnewtLogHandlerFirePHP log handler logs message in a
  format that FirePHP can display in the Firebug console.
modified:
  logging/loghandlers.lib.php


--
lp:anewt
https://code.launchpad.net/~uws/anewt/anewt.uws

Your team Anewt developers is subscribed to branch lp:anewt.
To unsubscribe from this branch go to https://code.launchpad.net/~uws/anewt/anewt.uws/+edit-subscription.
=== modified file 'logging/loghandlers.lib.php'
--- logging/loghandlers.lib.php	2010-03-21 14:45:20 +0000
+++ logging/loghandlers.lib.php	2010-03-26 21:34:27 +0000
@@ -256,4 +256,81 @@
 	}
 }
 
+
+/**
+ * Log handler for FirePHP that sends messages as response headers.
+ *
+ * Note that this log handler only works if no response body has been sent yet.
+ * The standard headers_sent() function is used to detect this. If the headers
+ * are already sent, all log messages will be silently discarded.
+ */
+final class AnewtLogHandlerFirePHP extends AnewtLogHandlerBase
+{
+	/**
+	 * The number of messages that are sent.
+	 *
+	 * This is used for the header name.
+	 */
+	private $n_messages = 0;
+
+	function __construct()
+	{
+		if (headers_sent())
+			return;
+
+		/* Send the headers needed for initialization */
+		header('X-Wf-Protocol-1: http://meta.wildfirehq.org/Protocol/JsonStream/0.2');
+		header('X-Wf-1-Structure-1: http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1');
+		header('X-Wf-1-Plugin-1: http://meta.firephp.org/Wildfire/Plugin/FirePHP/Library-FirePHPCore/0.3');
+	}
+
+	function log($domain, $level, $message)
+	{
+		if (headers_sent())
+			return;
+
+		assert('is_int($level)');
+		assert('is_string($message)');
+
+
+		/* Translate log levels to those supported by FirePHP */
+
+		switch($level)
+		{
+			case ANEWT_LOG_LEVEL_ERROR:
+				$level_firephp = 'ERROR';
+				break;
+			case ANEWT_LOG_LEVEL_CRITICAL:
+			case ANEWT_LOG_LEVEL_WARNING:
+				$level_firephp = 'WARN';
+				break;
+			case ANEWT_LOG_LEVEL_MESSAGE:
+			case ANEWT_LOG_LEVEL_INFO:
+				$level_firephp = 'INFO';
+				break;
+			case ANEWT_LOG_LEVEL_DEBUG:
+			default:
+				$level_firephp = 'LOG';
+				break;
+		}
+
+		$metadata = array(
+			'Type' => $level_firephp,
+		);
+
+		if (!is_null($domain))
+			$metadata['Label'] = $domain;
+
+		$value = json_encode(array($metadata, $message));
+
+		$this->n_messages++;
+		header(sprintf(
+			'X-Wf-1-1-1-%d: %d|%s|',
+			$this->n_messages,
+			strlen($value),
+			$value
+		));
+	}
+}
+
 ?>