| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Core\Util; |
| 4 |
|
| 5 |
final class Log |
| 6 |
{ |
| 7 |
public static function get_backtrace_info() |
| 8 |
{ |
| 9 |
$trace = debug_backtrace(); |
| 10 |
|
| 11 |
$file = $trace[1]['file']; |
| 12 |
$class = $trace[2]['class']; |
| 13 |
$func = $trace[2]['function']; |
| 14 |
$type = $trace[2]['type']; |
| 15 |
$line = $trace[1]['line']; |
| 16 |
|
| 17 |
$output = sprintf( |
| 18 |
'%s%s%s() at %s on line %s', |
| 19 |
$class, |
| 20 |
$type, |
| 21 |
$func, |
| 22 |
$file, |
| 23 |
$line |
| 24 |
); |
| 25 |
|
| 26 |
return $output; |
| 27 |
} |
| 28 |
|
| 29 |
public static function debug_log($log) |
| 30 |
{ |
| 31 |
$trace_info = self::get_backtrace_info(); |
| 32 |
if (WP_DEBUG === true) { |
| 33 |
if (is_array($log) || is_object($log)) { |
| 34 |
if (is_array($log)) { |
| 35 |
$log['trace'] = $trace_info; |
| 36 |
} else { |
| 37 |
$log->trace = $trace_info; |
| 38 |
} |
| 39 |
$log_msg = print_r($log, true); |
| 40 |
} else { |
| 41 |
$log_msg = $log . ' in ' . $trace_info; |
| 42 |
} |
| 43 |
|
| 44 |
error_log($log_msg); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
public static function print($log, $path = '', $fileName = 'print.log', $fileOpenMode = 'a') |
| 49 |
{ |
| 50 |
$rootDir = BITFORMS_CONTENT_DIR; |
| 51 |
$path = trim($path, '/'); |
| 52 |
$pathArr = explode('/', $path); // like "fieldname/user => [Fieldname, user] |
| 53 |
foreach ($pathArr as $d) { |
| 54 |
$rootDir .= $d . DIRECTORY_SEPARATOR; |
| 55 |
if (!realpath($rootDir)) { |
| 56 |
mkdir($rootDir); |
| 57 |
} |
| 58 |
} |
| 59 |
$fullPath = $rootDir . $fileName; |
| 60 |
$file = fopen($fullPath, $fileOpenMode); |
| 61 |
fwrite($file, $log); |
| 62 |
fclose($file); |
| 63 |
return true; |
| 64 |
} |
| 65 |
} |
| 66 |
|