forked from Zodiac1978/tl-normalizer
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdebug.php
More file actions
192 lines (168 loc) · 5.95 KB
/
debug.php
File metadata and controls
192 lines (168 loc) · 5.95 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
/**
* Some debugging helpers.
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) exit;
if ( defined( 'UNFC_DEBUG' ) && UNFC_DEBUG ) :
if ( ! defined( 'UNFC_DEBUG_PRINT_LIMIT' ) ) define( 'UNFC_DEBUG_PRINT_LIMIT', 256 );
if ( ! defined( 'WP_CONTENT_DIR' ) ) define( 'WP_CONTENT_DIR', '' );
/**
* Error log helper. Prints arguments, prefixing file, line and function called from.
*/
function unfc_error_log() {
$func_get_args = func_get_args();
$ret = unfc_debug_trace( debug_backtrace(), $func_get_args );
$ret[0] = 'ERROR: ' . $ret[0] . "\n\t";
$ret = implode( '', $ret );
error_log( $ret );
return $ret;
}
/**
* Debug log helper. Same as unfc_error_log() except it prints nothing unless UNFC_DEBUG set, and doesn't prefix with "ERROR:".
*/
function unfc_debug_log() {
if ( ! UNFC_DEBUG ) return '';
if ( defined( 'DOING_AJAX' ) && DOING_AJAX && isset( $_REQUEST['action'] ) && is_string( $_REQUEST['action'] ) && 'heartbeat' === $_REQUEST['action'] ) return '';
$func_get_args = func_get_args();
$ret = unfc_debug_trace( debug_backtrace(), $func_get_args );
$ret[0] = $ret[0] . "\n\t";
$ret = implode( '', $ret );
error_log( $ret );
return $ret;
}
/**
* Common routine for unfc_error_log() and unfc_debug_log().
*/
function unfc_debug_trace( $trace, $func_get_args ) {
$file = str_replace( array( WP_CONTENT_DIR, ABSPATH ), array( '../..', '../../../' ), isset( $trace[0]['file'] ) ? $trace[0]['file'] : '' ); // Assuming in wp-content/themes/mytheme.
$line = isset( $trace[0]['line'] ) ? $trace[0]['line'] : '';
$function_args = '';
if ( ( $function = isset( $trace[1]['function'] ) ? $trace[1]['function'] : '' ) && ! empty( $trace[1]['args'] ) ) {
$function_args = array_reduce( $trace[1]['args'], 'unfc_debug_trace_array_reduce_cb' );
}
$ret[] = $file . ':' . $line . ' ' . $function . '(' . unfc_print_r( $function_args ) . ') '; // Limit $function_args length.
foreach ( $func_get_args as $func_get_arg ) $ret[] = is_array( $func_get_arg ) || is_object( $func_get_arg ) ? print_r( $func_get_arg, true ) : $func_get_arg;
return $ret;
}
/**
* Callback for array_reduce() in unfc_debug_trace().
*/
function unfc_debug_trace_array_reduce_cb( $carry, $item ) {
return ( $carry === null ? '' : $carry . ', ' )
. ( is_array( $item ) ? 'Array' : ( is_object( $item ) ? 'Object' : ( is_null( $item ) ? 'null' : str_replace( "\n", '', print_r( $item, true ) ) ) ) );
}
/**
* Backtrace formatter for debugging.
*/
function unfc_backtrace( $offset = 0, $length = null ) {
if ( ! UNFC_DEBUG ) return '';
$ret = array();
$backs = debug_backtrace();
$i = count( $backs );
foreach ( $backs as $back ) {
$entry = "\t" . $i . '. ';
$entry .= isset($back['class']) ? "{$back['class']}::" : '';
$entry .= isset($back['function']) ? "{$back['function']} " : '';
$entry .= isset($back['file']) ? "{$back['file']}:" : '';
$entry .= isset($back['line']) ? "{$back['line']} " : '';
$ret[] = $entry;
$i--;
}
if ( $length === null ) $length = 20;
return "\n" . implode( "\n", array_reverse( array_slice( $ret, $offset + 1, $length ) ) );
}
/**
* Wrapper around PHP print_r() to limit length dumped.
*/
function unfc_print_r( $var ) {
if ( is_array( $var ) ) {
return print_r( array_map( 'unfc_print_r', $var ), true );
}
return unfc_print_r_limit_cb( print_r( $var, true ) );
}
/**
* Callback for unfc_print_r().
*/
function unfc_print_r_limit_cb( $str ) {
if ( strlen( $str ) > UNFC_DEBUG_PRINT_LIMIT ) {
return substr( $str, 0, UNFC_DEBUG_PRINT_LIMIT ) . '...';
}
return $str;
}
/**
* Hex dump version of unfc_print_r().
*/
function unfc_print_r_hex( $var ) {
if ( is_array( $var ) ) {
return trim( print_r( array_map( 'unfc_print_r_hex', $var ), true ) );
}
return unfc_print_r_limit_cb( unfc_bin2hex( $var ) );
}
/**
* Dump a variable as a string.
*/
function unfc_dump( $var, $format = false ) {
if ( ! UNFC_DEBUG ) return '';
ob_start();
debug_zval_dump( $var );
$ret = ob_get_clean();
if ( $format ) {
$ret = preg_replace( '/ refcount\(\d+\)$/m', '', $ret );
$ret = preg_replace( '/ refcount\(\d+\)/m', ' ', $ret );
$ret = preg_replace( '/=>[ \n]+/', '=> ', $ret );
}
return $ret;
}
/**
* Hex dump a variable if string.
*/
function unfc_bin2hex( $var ) {
$ret = '';
if ( ! isset( $var ) || is_null( $var ) ) {
$ret .= '(null)';
} elseif ( is_array( $var ) || is_object( $var ) ) {
$ret .= '(' . gettype( $var ) . ')';
foreach ( (array) $var as $k => $v ) {
$ret .= ';' . $k . '=' . ( is_scalar( $v ) ? unfc_bin2hex( $v ) : print_r( $v, true ) );
}
} elseif ( is_string( $var ) ) {
$ret .= bin2hex( $var );
} elseif ( is_bool( $var ) ) {
$ret .= '(' . gettype( $var ) . ')' . ( $var ? 'true' : 'false' );
} elseif ( is_int( $var ) ) {
$ret .= '(' . gettype( $var) . ')' . $var;
} elseif ( is_float( $var ) ) {
$ret .= '(' . gettype( $var ) . ')' . $var;
} elseif ( is_resource( $var ) ) {
$ret .= '(' . get_resource_type( $var ) . ')' . $var;
} else {
$ret .= '(' . gettype( $var ) . ')';
}
return $ret;
}
/**
* Format bytes in human-friendly manner.
* From http://stackoverflow.com/a/2510459
*/
function unfc_format_bytes( $bytes, $precision = 2 ) {
$units = array( 'B', 'KB', 'MB', 'GB', 'TB' );
$bytes = max( $bytes, 0 );
$pow = floor( ( $bytes ? log( $bytes ) : 0 ) / log( 1024 ) );
$pow = min( $pow, count( $units ) - 1 );
// Uncomment one of the following alternatives
// $bytes /= pow( 1024, $pow );
$bytes /= ( 1 << ( 10 * $pow ) );
return round( $bytes, $precision ) . ' ' . $units[$pow];
}
else :
function unfc_error_log() { return ''; }
function unfc_debug_log() { return ''; }
function unfc_backtrace( $offset = 0, $length = null ) { return ''; }
function unfc_print_r( $var ) { return ''; }
function unfc_print_r_limit_cb( $str ) { return ''; }
function unfc_print_r_hex( $var ) { return ''; }
function unfc_dump( $var, $format = false ) { return ''; }
function unfc_bin2hex( $var ) { return ''; }
function unfc_format_bytes( $bytes, $precision = 2 ) { return ''; }
endif;