From 3ea58f544baf54f65ae9b850f1bd7788163d5755 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Fri, 24 Aug 2012 07:50:59 +0200 Subject: [PATCH] file top logger --- src/callnotifier/Logger/CallFileTop.php | 100 ++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 src/callnotifier/Logger/CallFileTop.php diff --git a/src/callnotifier/Logger/CallFileTop.php b/src/callnotifier/Logger/CallFileTop.php new file mode 100644 index 0000000..6caa551 --- /dev/null +++ b/src/callnotifier/Logger/CallFileTop.php @@ -0,0 +1,100 @@ +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)); + } + +} + +?> -- 2.30.2