file top logger
authorChristian Weiske <cweiske@cweiske.de>
Fri, 24 Aug 2012 05:50:59 +0000 (07:50 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Fri, 24 Aug 2012 05:50:59 +0000 (07:50 +0200)
src/callnotifier/Logger/CallFileTop.php [new file with mode: 0644]

diff --git a/src/callnotifier/Logger/CallFileTop.php b/src/callnotifier/Logger/CallFileTop.php
new file mode 100644 (file)
index 0000000..6caa551
--- /dev/null
@@ -0,0 +1,100 @@
+<?php
+namespace callnotifier;
+
+/**
+ * Logs finished calls into a file, latest on top.
+ * The date is also show on a extra line, grouping the following calls.
+ *
+ * Suitable for Dreambox display with CurlyTx
+ *
+ * Example:
+ *
+ * 24.08.2012, Friday
+ *   07:48 nach Herbert                        00:00:03
+ *   08:13 von  02426140162                    00:02:21
+ */
+class Logger_CallFileTop extends Logger_CallBase
+{
+    protected $file;
+
+    /**
+     * Create a new file call logger. 
+     *
+     * @param string $file      Path to the file to log the calls in.
+     * @param string $callTypes Which types of call to log:
+     *                          - "i" - incoming calls only
+     *                          - "o" - outgoing calls only
+     *                          - "io" - both incoming and outgoing calls
+     * @param array  $msns      Array of MSN (Multi Subscriber Number) that
+     *                          calls to shall get logged.
+     *                          If the array is empty, calls to all MSNs get
+     *                          logged.
+     */
+    public function __construct(
+        $file,
+        $callTypes = 'io',
+        $msns = array()
+    ) {
+        $this->file      = $file;
+        $this->callTypes = $callTypes;
+        $this->msns      = (array)$msns;
+
+        $fileHdl = fopen($this->file, 'a');
+        if (!$fileHdl) {
+            throw new \Exception(
+                'Cannot open call log file for writing: ' . $this->file
+            );
+        }
+        fclose($fileHdl);
+    }
+
+    public function log($type, $arData)
+    {
+        if ($type != 'finishedCall') {
+            return;
+        }
+
+        $call = $arData['call'];
+        if (!$this->hasValidType($call)) {
+            return;
+        }
+        if (!$this->hasValidMsn($call)) {
+            return;
+        }
+
+        list($logline, $date) = $this->createLogEntry($call);
+        $arLines = file($this->file);
+        if (isset($arLines[0]) && $arLines[0] == $date) {
+            //same date as previous log entry
+            $arLines = array_pad($arLines, -count($arLines) - 1, '');
+        } else {
+            $arLines = array_pad($arLines, -count($arLines) - 2, '');
+        }
+        $arLines[0] = $date;
+        $arLines[1] = $logline;
+
+        //keep 50 lines only
+        array_splice($arLines, 50);
+
+        file_put_contents($this->file, implode('', $arLines));
+    }
+
+
+    protected function createLogEntry(CallMonitor_Call $call)
+    {
+        $this->addUnsetVars($call);
+        $str = '  ' . date('H:i', $call->start);
+        if ($call->type == CallMonitor_Call::INCOMING) {
+            $str .= ' von  ' . str_pad($this->getNumberString($call, 'from'), 30);
+        } else {
+            $str .= ' nach ' . str_pad($this->getNumberString($call, 'to'), 30);
+        }
+
+        $str .= ' ' . date('H:i:s', $call->end - $call->start - 3600);
+
+        return array($str . "\n", date("d.m.Y, l\n", $call->start));
+    }
+
+}
+
+?>