Add debug message output to call loggers
[auerswald-callnotifier.git] / src / callnotifier / Log.php
1 <?php
2 namespace callnotifier;
3
4 class Log
5 {
6     public $debug = false;
7
8     /**
9      * Array of logger object arrays.
10      * Key is the notification type, value is an array of logger objects
11      * that want to get notified about the type.
12      *
13      * @var array
14      */
15     protected $logger = array(
16         'msgData' => array(),
17         'edss1msg' => array(),
18         'startingCall' => array(),
19         'finishedCall' => array(),
20     );
21
22     /**
23      * Add a logger
24      *
25      * @param Logger       $logger Logger object to register
26      * @param array|string $types  Single notification type or array of such
27      *                             types. "*" means "register for all types".
28      *
29      * @return self
30      */
31     public function addLogger(Logger $logger, $types)
32     {
33         if ($types == '*') {
34             $types = array_keys($this->logger);
35         }
36         $types = (array)$types;
37
38         if ($this->debug) {
39             $logger->debug = $this->debug;
40         }
41
42         foreach ($types as $type) {
43             if (!isset($this->logger[$type])) {
44                 throw new \Exception('Unknown log type: ' . $type);
45             }
46             $this->logger[$type][] = $logger;
47         }
48     }
49
50     public function log($type, $arData)
51     {
52         if (!isset($this->logger[$type])) {
53             throw new \Exception('Unknown log type: ' . $type);
54         }
55
56         if (count($this->logger[$type])) {
57             foreach ($this->logger[$type] as $logger) {
58                 if ($this->debug) {
59                     echo "Logging to " . get_class($logger) . "\n";
60                 }
61                 $logger->log($type, $arData);
62             }
63         }
64     }
65
66 }
67
68 ?>