forked from simplehtmldom/simplehtmldom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Debug.php
149 lines (128 loc) · 3.38 KB
/
Debug.php
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php namespace simplehtmldom;
/**
* Website: http://sourceforge.net/projects/simplehtmldom/
* Acknowledge: Jose Solorzano (https://sourceforge.net/projects/php-html/)
*
* Licensed under The MIT License
* See the LICENSE file in the project root for more information.
*
* Authors:
* S.C. Chen
* John Schlick
* Rus Carroll
* logmanoriginal
*
* Contributors:
* Yousuke Kumakura
* Vadim Voituk
* Antcs
*
* Version $Rev$
*/
/**
* Implements functions for debugging purposes. Debugging can be enabled and
* disabled on demand. Debug messages are send to error_log by default but it
* is also possible to register a custom debug handler.
*/
class Debug {
private static $enabled = false;
private static $debugHandler = null;
private static $callerLock = array();
/**
* Checks whether debug mode is enabled.
*
* @return bool True if debug mode is enabled, false otherwise.
*/
public static function isEnabled()
{
return self::$enabled;
}
/**
* Enables debug mode
*/
public static function enable()
{
self::$enabled = true;
self::log('Debug mode has been enabled');
}
/**
* Disables debug mode
*/
public static function disable()
{
self::log('Debug mode has been disabled');
self::$enabled = false;
}
/**
* Sets the debug handler.
*
* `null`: error_log (default)
*/
public static function setDebugHandler($function = null)
{
if ($function === self::$debugHandler) return;
self::log('New debug handler registered');
self::$debugHandler = $function;
}
/**
* This is the actual log function. It allows to set a custom backtrace to
* eliminate traces of this class.
*/
private static function log_trace($message, $backtrace)
{
$idx = 0;
$debugmessage = '';
foreach($backtrace as $caller)
{
if (!isset($caller['file']) && !isset($caller['line'])) {
break; // Unknown caller
}
$debugmessage .= ' [' . $caller['file'] . ':' . $caller['line'];
if ($idx > 1) { // Do not include the call to Debug::log
$debugmessage .= ' '
. $caller['class']
. $caller['type']
. $caller['function']
. '()';
}
$debugmessage .= ']';
// Stop at the first caller that isn't part of simplehtmldom
if (!isset($caller['class']) || strpos($caller['class'], 'simplehtmldom\\') !== 0) {
break;
}
}
$output = '[DEBUG] ' . trim($debugmessage) . ' "' . $message . '"';
if (is_null(self::$debugHandler)) {
error_log($output);
} else {
call_user_func_array(self::$debugHandler, array($output));
}
}
/**
* Adds a debug message to error_log if debug mode is enabled. Does nothing
* if debug mode is disabled.
*
* @param string $text The message to add to error_log
*/
public static function log($message)
{
if (!self::isEnabled()) return;
$backtrace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT);
self::log_trace($message, $backtrace);
}
/**
* Adds a debug message to error_log if debug mode is enabled. Does nothing
* if debug mode is disabled. Each message is logged only once.
*
* @param string $text The message to add to error_log
*/
public static function log_once($message)
{
if (!self::isEnabled()) return;
// Keep track of caller (file & line)
$backtrace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT);
if (in_array($backtrace[0], self::$callerLock, true)) return;
self::$callerLock[] = $backtrace[0];
self::log_trace($message, $backtrace);
}
}