6f24bad66c00c703cbbec8862daf841b4220e77e
[auerswald-callnotifier.git] / src / callnotifier / Logger / Debug.php
1 <?php
2 namespace callnotifier;
3
4 class Logger_Debug implements Logger
5 {
6     public $edss1MsgOnly = false;
7
8     public function __construct()
9     {
10         $cc = new \Console_Color2();
11         $this->begin  = $cc->convert('%y');
12         $this->end    = $cc->convert('%n');
13         $this->blue   = $cc->convert('%b');
14         $this->red    = $cc->convert('%r');
15         $this->white  = $cc->convert('%w');
16         $this->purple = $cc->convert('%p');
17     }
18
19     public function log($type, $arData)
20     {
21         if ($type == 'msgData') {
22             $this->echoMsgData($arData);
23         } else if ($type == 'edss1msg') {
24             $this->echoEDSS1($arData['msg']);
25         } else {
26             //other data
27             echo $this->blue . $type . $this->end . ': '
28                 . var_export($arData, true) . "\n";
29         }
30     }
31
32
33     protected function echoMsgData($arData)
34     {
35         if ($this->edss1MsgOnly
36             && substr($arData['details'], 16, 4) != ' 08 '
37         ) {
38             //only E-DSS-1, no other packets
39             return;
40         }
41
42         echo $this->begin . $arData['type'] . $this->end
43             . ': ' . $arData['details'] . "\n";
44
45         //Show bytes of N01|N02|T01|T02 etc
46         if (preg_match('#^[A-Z][0-9]{2}: (.+)$#', $arData['details'], $matches)) {
47             $bytestring = $matches[1];
48             $line = '';
49             foreach (explode(' ', $bytestring) as $strbyte) {
50                 $line .= chr(hexdec($strbyte));
51             }
52             $line = preg_replace(
53                 '/[^[:print:]]/',
54                 $this->white . '?' . $this->end,
55                 $line
56             );
57             echo $this->red . '     bytes' . $this->end . ': ' . $line . "\n";
58         }
59     }
60
61     protected function echoEDSS1($msg)
62     {
63         echo sprintf(
64             $this->purple . 'EDSS1_Message' . $this->end
65             . ' type 0x%02X '
66             . $this->purple . '%s' . $this->end
67             . ' SAPI %d, CR %d, TEI %d, call %d-%s'
68             . ', %d parameters',
69             $msg->type,
70             $msg->getTypeName(),
71             $msg->sapi,
72             $msg->callResponse,
73             $msg->tei,
74             $msg->callRef,
75             $msg->callRefType == 0 ? 'source' : 'target',
76             count($msg->parameters)
77         ) . "\n";
78
79         foreach ($msg->parameters as $param) {
80             echo sprintf(
81                 " Parameter type 0x%02X%s, %d bytes: %s\n",
82                 $param->type,
83                 $param->title
84                 ? ' ' . $this->purple . $param->title . $this->end
85                 : ' ' . $this->purple . $param->getTypeName() . $this->end,
86                 $param->length,
87                 preg_replace('/[^[:print:]]/', '?', $param->data)
88                 . (isset($param->number)
89                    ? ' ' . $this->red . $param->number . $this->end
90                    : ''
91                 )
92             );
93             if ($param instanceof EDSS1_Parameter_INumber) {
94                 echo sprintf(
95                     "    Number type: %s, plan: %s\n",
96                     EDSS1_Parameter_Names::$numberTypes[$param->numberType],
97                     EDSS1_Parameter_Names::$numberingPlans[$param->numberingPlan]
98                 );
99             }
100         }
101     }
102 }
103
104 ?>