aboutsummaryrefslogtreecommitdiff
path: root/src/callnotifier/Log.php
blob: a831066540196d587782fd7e484c625bf3a51da9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
namespace callnotifier;

class Log
{
    public $debug = false;

    /**
     * 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(),
        'edss1msg' => array(),
        'startingCall' => array(),
        'finishedCall' => array(),
    );

    /**
     * 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;

        if ($this->debug) {
            $logger->debug = $this->debug;
        }

        foreach ($types as $type) {
            if (!isset($this->logger[$type])) {
                throw new \Exception('Unknown log type: ' . $type);
            }
            $this->logger[$type][] = $logger;
        }
    }

    public 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) {
                if ($this->debug) {
                    echo "Logging to " . get_class($logger) . "\n";
                }
                $logger->log($type, $arData);
            }
        }
    }

}

?>