PluginProbe
PDF for WPForms + Drag and Drop Template Builder / trunk
PDF for WPForms + Drag and Drop Template Builder vtrunk
7.2.0 7.1.0 trunk 2.5.0 3.0.0 6.2.1 6.3.1 6.5.0 6.5.1 6.5.2
pdf-for-wpforms / libs / Debug.php

Debug.php in PDF for WPForms + Drag and Drop Template Builder trunk, at libs/Debug.php

133 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace simplehtmldom;
4
5 if (! defined('ABSPATH')) exit; // Exit if accessed directly
6
7 /**
8 * Implements functions for debugging purposes. Debugging can be enabled and
9 * disabled on demand. Debug messages are send to error_log by default but it
10 * is also possible to register a custom debug handler.
11 */
12 class Debug
13 {
14
15 private static $enabled = false;
16 private static $debugHandler = null;
17 private static $callerLock = array();
18
19 /**
20 * Checks whether debug mode is enabled.
21 *
22 * @return bool True if debug mode is enabled, false otherwise.
23 */
24 public static function isEnabled()
25 {
26 return self::$enabled;
27 }
28
29 /**
30 * Enables debug mode
31 */
32 public static function enable()
33 {
34 self::$enabled = true;
35 self::log('Debug mode has been enabled');
36 }
37
38 /**
39 * Disables debug mode
40 */
41 public static function disable()
42 {
43 self::log('Debug mode has been disabled');
44 self::$enabled = false;
45 }
46
47 /**
48 * Sets the debug handler.
49 *
50 * `null`: error_log (default)
51 */
52 public static function setDebugHandler($function = null)
53 {
54 if ($function === self::$debugHandler) return;
55
56 self::log('New debug handler registered');
57 self::$debugHandler = $function;
58 }
59
60 /**
61 * This is the actual log function. It allows to set a custom backtrace to
62 * eliminate traces of this class.
63 */
64 private static function log_trace($message, $backtrace)
65 {
66 $idx = 0;
67 $debugmessage = '';
68
69 foreach ($backtrace as $caller) {
70 if (!isset($caller['file']) && !isset($caller['line'])) {
71 break; // Unknown caller
72 }
73
74 $debugmessage .= ' [' . $caller['file'] . ':' . $caller['line'];
75
76 if ($idx > 1) { // Do not include the call to Debug::log
77 $debugmessage .= ' '
78 . $caller['class']
79 . $caller['type']
80 . $caller['function']
81 . '()';
82 }
83
84 $debugmessage .= ']';
85
86 // Stop at the first caller that isn't part of simplehtmldom
87 if (!isset($caller['class']) || strpos($caller['class'], 'simplehtmldom\\') !== 0) {
88 break;
89 }
90 }
91
92 $output = '[DEBUG] ' . trim($debugmessage) . ' "' . $message . '"';
93
94 if (is_null(self::$debugHandler)) {
95 //error_log($output);
96 } else {
97 call_user_func_array(self::$debugHandler, array($output));
98 }
99 }
100
101 /**
102 * Adds a debug message to error_log if debug mode is enabled. Does nothing
103 * if debug mode is disabled.
104 *
105 * @param string $text The message to add to error_log
106 */
107 public static function log($message)
108 {
109 if (!self::isEnabled()) return;
110
111 $backtrace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT); //phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace
112 self::log_trace($message, $backtrace);
113 }
114
115 /**
116 * Adds a debug message to error_log if debug mode is enabled. Does nothing
117 * if debug mode is disabled. Each message is logged only once.
118 *
119 * @param string $text The message to add to error_log
120 */
121 public static function log_once($message)
122 {
123 if (!self::isEnabled()) return;
124
125 // Keep track of caller (file & line)
126 $backtrace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT); //phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace
127 if (in_array($backtrace[0], self::$callerLock, true)) return;
128
129 self::$callerLock[] = $backtrace[0];
130 self::log_trace($message, $backtrace);
131 }
132 }
133