bf86abadb59c420dfd7bcae0e1cd61b0029b27aa
[plcapi.git] / src / Helper / Logger.php
1 <?php
2 /**
3  * Created by PhpStorm.
4  * User: gg
5  * Date: 12/04/2015
6  * Time: 12:11
7  */
8
9 namespace PhpXmlRpc\Helper;
10
11
12 class Logger
13 {
14     protected static $instance = null;
15
16     /**
17      * This class is singleton, so that later we can move to DI patterns.
18      *
19      * @return Logger
20      */
21     public static function instance()
22     {
23         if (self::$instance === null) {
24             self::$instance = new self();
25         }
26
27         return self::$instance;
28     }
29
30     /**
31      * Echoes a debug message, taking care of escaping it when not in console mode.
32      * NB: if the encoding of the message is not known or wrong, and we are working in web mode, there is no guarantee
33      *     of 100% accuracy, which kind of defeats the purpose of debugging
34      *
35      * @param string $message
36      * @param string $encoding
37      */
38     public function debugMessage($message, $encoding=null)
39     {
40         // US-ASCII is a warning for PHP and a fatal for HHVM
41         if ($encoding == 'US-ASCII') {
42             $encoding = 'UTF-8';
43         }
44
45         if (PHP_SAPI != 'cli') {
46             $flags = ENT_COMPAT | ENT_HTML401 | ENT_SUBSTITUTE;
47             if ($encoding != null) {
48                 print "<PRE>\n".htmlentities($message, $flags, $encoding)."\n</PRE>";
49             } else {
50                 print "<PRE>\n".htmlentities($message, $flags)."\n</PRE>";
51             }
52         } else {
53             print "\n$message\n";
54         }
55
56         // let the user see this now in case there's a time out later...
57         flush();
58     }
59 }