Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
namespace PhpXmlRpc\Helper;
4
 
5
/**
6
 * @todo implement an interface
7
 * @todo make constructor private to force users to go through `instance()` ?
8
 */
9
class Logger
10
{
11
    protected static $instance = null;
12
 
13
    /**
14
     * This class can be used as singleton, so that later we can move to DI patterns (ish...)
15
     *
16
     * @return Logger
17
     */
18
    public static function instance()
19
    {
20
        if (self::$instance === null) {
21
            self::$instance = new self();
22
        }
23
 
24
        return self::$instance;
25
    }
26
 
27
    // *** Implement the same interface as PSR/LOG, for the sake of interoperability ***
28
 
29
    /**
30
     * NB: unlike other "traditional" loggers, this one echoes to screen the debug messages instead of logging them.
31
     *
32
     * @param string $message
33
     * @param array $context known key: 'encoding'
34
     * @return void
35
     */
36
    public function debug($message, $context = array())
37
    {
38
        if (isset($context['encoding'])) {
39
            $this->debugMessage($message, $context['encoding']);
40
        } else {
41
            $this->debugMessage($message);
42
        }
43
    }
44
 
45
    /**
46
     * Following the general principle of 'never break stdout', the default behaviour
47
     *
48
     * @param string $message
49
     * @param $context
50
     * @return void
51
     */
52
    public function warning($message, $context = array())
53
    {
54
        $this->errorLog(preg_replace('/^XML-RPC :/', 'XML-RPC Warning: ', $message));
55
    }
56
 
57
    /**
58
     * Triggers the writing of a message to php's error log
59
     *
60
     * @param string $message
61
     * @param array $context
62
     * @return void
63
     */
64
    public function error($message, $context = array())
65
    {
66
        $this->errorLog(preg_replace('/^XML-RPC :/', 'XML-RPC Error: ', $message));
67
    }
68
 
69
    // BC interface
70
 
71
    /**
72
     * Echoes a debug message, taking care of escaping it when not in console mode.
73
     * NB: if the encoding of the message is not known or wrong, and we are working in web mode, there is no guarantee
74
     *     of 100% accuracy, which kind of defeats the purpose of debugging
75
     *
76
     * @param string $message
77
     * @param string $encoding deprecated
78
     * @return void
79
     *
80
     * @internal left in purely for BC
81
     */
82
    public function debugMessage($message, $encoding = null)
83
    {
84
        // US-ASCII is a warning for PHP and a fatal for HHVM
85
        if ($encoding == 'US-ASCII') {
86
            $encoding = 'UTF-8';
87
        }
88
 
89
        if (PHP_SAPI != 'cli') {
90
            $flags = ENT_COMPAT;
91
            // avoid warnings on php < 5.4...
92
            if (defined('ENT_HTML401')) {
93
                $flags =  $flags | ENT_HTML401;
94
            }
95
            if (defined('ENT_SUBSTITUTE')) {
96
                $flags =  $flags | ENT_SUBSTITUTE;
97
            }
98
            if ($encoding != null) {
99
                print "<PRE>\n".htmlentities($message, $flags, $encoding)."\n</PRE>";
100
            } else {
101
                print "<PRE>\n".htmlentities($message, $flags)."\n</PRE>";
102
            }
103
        } else {
104
            print "\n$message\n";
105
        }
106
 
107
        // let the user see this now in case there's a time-out later...
108
        flush();
109
    }
110
 
111
    /**
112
     * Writes a message to the error log.
113
     *
114
     * @param string $message
115
     * @return void
116
     *
117
     * @internal left in purely for BC
118
     */
119
    public function errorLog($message)
120
    {
121
        error_log($message);
122
    }
123
}