echo logger
authorChristian Weiske <cweiske@cweiske.de>
Sat, 14 Jul 2012 20:56:33 +0000 (22:56 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Sat, 14 Jul 2012 20:56:33 +0000 (22:56 +0200)
src/callnotifier/CLI.php
src/callnotifier/Logger.php [new file with mode: 0644]
src/callnotifier/Logger/Echo.php [new file with mode: 0644]
src/callnotifier/MessageHandler.php
src/callnotifier/Source/File.php

index 928e4ce929cac281f34ac9118a2efd87609f6ed1..f9a4937742bb192a854792bdff20cccf8929fc24 100644 (file)
@@ -24,6 +24,8 @@ class CLI
         $this->fillConfig($this->config, $result);
 
         $handler = new MessageHandler($this->config);
         $this->fillConfig($this->config, $result);
 
         $handler = new MessageHandler($this->config);
+        $handler->addLogger(new Logger_Echo(), '*');
+
         if ($this->config->replayFile !== null) {
             $sourceClass = 'callnotifier\Source_File';
         } else {
         if ($this->config->replayFile !== null) {
             $sourceClass = 'callnotifier\Source_File';
         } else {
diff --git a/src/callnotifier/Logger.php b/src/callnotifier/Logger.php
new file mode 100644 (file)
index 0000000..48a5ecf
--- /dev/null
@@ -0,0 +1,9 @@
+<?php
+namespace callnotifier;
+
+interface Logger
+{
+    public function log($type, $arData);
+}
+
+?>
diff --git a/src/callnotifier/Logger/Echo.php b/src/callnotifier/Logger/Echo.php
new file mode 100644 (file)
index 0000000..eec9bb1
--- /dev/null
@@ -0,0 +1,27 @@
+<?php
+namespace callnotifier;
+
+class Logger_Echo implements Logger
+{
+    public function __construct()
+    {
+        $cc = new \Console_Color2();
+        $this->begin = $cc->convert('%y');
+        $this->end = $cc->convert('%n');
+        $this->blue = $cc->convert('%b');
+    }
+
+    public function log($type, $arData)
+    {
+        if ($type == 'msgData') {
+            echo $this->begin . $arData['type'] . $this->end
+                . ': ' . $arData['details'] . "\n";
+        } else {
+            echo $this->blue . $type . $this->end . ': '
+                . var_export($arData, true) . "\n";
+        }
+    }
+
+}
+
+?>
index b4ed6455706a882129527b05d4d1b8fd94dcae50..89d07e72c73464a51ee0346b4ed4cd338a2b900d 100644 (file)
@@ -5,6 +5,18 @@ class MessageHandler
 {
     protected $dumpHdl;
 
 {
     protected $dumpHdl;
 
+    /**
+     * Array of logger object arrays.
+     * Key is the notification type, value is an array of logger objects
+     * that want to get notified about the type.
+     *
+     * @var array
+     */
+    protected $logger = array(
+        'msgData' => array(),
+        'incomingCall' => array()
+    );
+
 
     public function __construct($config)
     {
 
     public function __construct($config)
     {
@@ -12,6 +24,30 @@ class MessageHandler
         $this->prepareDump();
     }
 
         $this->prepareDump();
     }
 
+    /**
+     * Add a logger
+     *
+     * @param Logger       $logger Logger object to register
+     * @param array|string $types  Single notification type or array of such
+     *                             types. "*" means "register for all types".
+     *
+     * @return self
+     */
+    public function addLogger(Logger $logger, $types)
+    {
+        if ($types == '*') {
+            $types = array_keys($this->logger);
+        }
+        $types = (array)$types;
+
+        foreach ($types as $type) {
+            if (!isset($this->logger[$type])) {
+                throw new \Exception('Unknown log type: ' . $type);
+            }
+            $this->logger[$type][] = $logger;
+        }
+    }
+
     public function handle($msg)
     {
         if ($this->config->dumpFile !== null) {
     public function handle($msg)
     {
         if ($this->config->dumpFile !== null) {
@@ -27,17 +63,37 @@ class MessageHandler
             return false;
         }
         list(, $type, $someid, $details) = $matches;
             return false;
         }
         list(, $type, $someid, $details) = $matches;
+        $this->log(
+            'msgData',
+            array(
+                'type' => $type,
+                'id' => $someid,
+                'details' => $details
+            )
+        );
 
         if ($type != 'Info') {
             //we only want info messages
 
         if ($type != 'Info') {
             //we only want info messages
-            var_dump($type . ': ' . $details);
             return;
         }
         //Vegw/Ets-Cref:[0xffef]/[0x64] - VEGW_SETUP from upper layer to internal destination: CGPN[**22]->CDPN[41], 
             return;
         }
         //Vegw/Ets-Cref:[0xffef]/[0x64] - VEGW_SETUP from upper layer to internal destination: CGPN[**22]->CDPN[41], 
-        var_dump($details);
         $regex = '#CGPN\\[([^\\]]+)\\]->CDPN\\[([^\\]]+)\\]#';
         if (preg_match($regex, $details, $matches)) {
         $regex = '#CGPN\\[([^\\]]+)\\]->CDPN\\[([^\\]]+)\\]#';
         if (preg_match($regex, $details, $matches)) {
-            var_dump('a call!', $matches);
+            list(, $from, $to) = $matches;
+            $this->log('incomingCall', array('from' => $from, 'to' => $to));
+        }
+    }
+
+    protected function log($type, $arData)
+    {
+        if (!isset($this->logger[$type])) {
+            throw new \Exception('Unknown log type: ' . $type);
+        }
+        
+        if (count($this->logger[$type])) {
+            foreach ($this->logger[$type] as $logger) {
+                $logger->log($type, $arData);
+            }
         }
     }
 
         }
     }
 
index c358397e96978b0f59a3bbfdc89e738d1999855f..9b6a03ea07069d28ab4906301abc11a71caeb7a7 100644 (file)
@@ -13,19 +13,19 @@ class Source_File
     {
         $file = $this->config->replayFile;
         if (!file_exists($file)) {
     {
         $file = $this->config->replayFile;
         if (!file_exists($file)) {
-            throw new Exception('Replay file does not exist');
+            throw new \Exception('Replay file does not exist');
         }
 
         $handle = fopen($file, 'r');
         if (!$handle) {
         }
 
         $handle = fopen($file, 'r');
         if (!$handle) {
-            throw new Exception('Cannot open replay file for reading');
+            throw new \Exception('Cannot open replay file for reading');
         }
 
         while (($line = fgets($handle, 4096)) !== false) {
             $this->handler->handle($line);
         }
         if (!feof($handle)) {
         }
 
         while (($line = fgets($handle, 4096)) !== false) {
             $this->handler->handle($line);
         }
         if (!feof($handle)) {
-            throw new Exception('unexpected fgets() fail');
+            throw new \Exception('unexpected fgets() fail');
         }
         fclose($handle);
     }
         }
         fclose($handle);
     }