From: Christian Weiske Date: Mon, 6 Aug 2012 15:16:23 +0000 (+0200) Subject: file call logger X-Git-Tag: v1.0.0~45 X-Git-Url: https://git.cweiske.de/auerswald-callnotifier.git/commitdiff_plain/f4bd5ef0c711aa9a3d0f6b468370a8c14c0e247a file call logger --- diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..397b4a7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.log diff --git a/src/callnotifier/CLI.php b/src/callnotifier/CLI.php index 16c95b0..6165f15 100644 --- a/src/callnotifier/CLI.php +++ b/src/callnotifier/CLI.php @@ -36,6 +36,7 @@ class CLI ); $callMonitor = new CallMonitor($this->config, $log); + /* $callMonitor->addDetailler( new CallMonitor_Detailler_LDAP( array( @@ -53,6 +54,16 @@ class CLI 'opengeodb' ) ); + */ + + $log->addLogger( + new Logger_CallFile('incoming.log', 'i', '40862'), + array('finishedCall') + ); + $log->addLogger( + new Logger_CallFile('all.log'), + array('finishedCall') + ); $handler = new MessageHandler($this->config, $log, $callMonitor); diff --git a/src/callnotifier/Logger/CallFile.php b/src/callnotifier/Logger/CallFile.php new file mode 100644 index 0000000..85bf287 --- /dev/null +++ b/src/callnotifier/Logger/CallFile.php @@ -0,0 +1,118 @@ +file = $file; + $this->callTypes = $callTypes; + $this->msns = (array)$msns; + + $this->fileHdl = fopen($this->file, 'a'); + if (!$this->fileHdl) { + throw new \Exception( + 'Cannot open call log file for writing: ' . $this->file + ); + } + } + + public function log($type, $arData) + { + if ($type != 'finishedCall') { + return; + } + + $call = $arData['call']; + + //check if call type matches + if ($call->type == CallMonitor_Call::INCOMING && $this->callTypes == 'o') { + return; + } + if ($call->type == CallMonitor_Call::OUTGOING && $this->callTypes == 'i') { + return; + } + + if ($call->type == CallMonitor_Call::INCOMING) { + $msn = $call->to; + } else { + $msn = $call->from; + } + if (count($this->msns) > 0 && !in_array($msn, $this->msns)) { + //msn shall not be logged + return; + } + + fwrite($this->fileHdl, $this->createLogEntry($call)); + } + + + protected function createLogEntry(CallMonitor_Call $call) + { + $this->addUnsetVars($call); + $str = date('Y-m-d H:i:s', $call->start); + if ($call->type == CallMonitor_Call::INCOMING) { + $str .= ' ' . $call->to + . ' von ' . $call->fromName; + if ($call->fromLocation) { + $str .= ' aus ' . $call->fromLocation; + } + $str .= ' ' . $this->getNumber($call->from); + } else { + $str .= ' ' . $call->from + . ' nach ' . $call->toName; + if ($call->toLocation) { + $str .= ' aus ' . $call->toLocation; + } + $str .= ' ' . $this->getNumber($call->to); + } + + $str .= ', Dauer ' . date('H:i:s', $call->end - $call->start - 3600); + + return $str . "\n"; + } + + protected function getNumber($number) + { + if ($number == '') { + $number = '*anonym*'; + } + return str_pad($number, 12, ' ', STR_PAD_RIGHT); + } + + protected function addUnsetVars($call) + { + static $expectedVars = array( + 'toName', 'fromName', 'toLocation', 'fromLocation' + ); + foreach ($expectedVars as $varName) { + if (!isset($call->$varName)) { + $call->$varName = null; + } + } + } + +} + +?> diff --git a/src/callnotifier/MessageHandler.php b/src/callnotifier/MessageHandler.php index 78a726d..90cd87c 100644 --- a/src/callnotifier/MessageHandler.php +++ b/src/callnotifier/MessageHandler.php @@ -88,7 +88,7 @@ class MessageHandler } $this->dumpHdl = fopen($this->config->dumpFile, 'w'); if (!$this->dumpHdl) { - throw new Exception('Cannot open dump file for writing'); + throw new \Exception('Cannot open dump file for writing'); } }