| 1 |
<?php |
| 2 |
|
| 3 |
namespace RabbitLoader\SDK; |
| 4 |
|
| 5 |
class Exc extends \Exception |
| 6 |
{ |
| 7 |
private static $log; |
| 8 |
private static $debug = false; |
| 9 |
|
| 10 |
public static function setFile($rootDir, $debug) |
| 11 |
{ |
| 12 |
self::$log = new File(rtrim($rootDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'error.log'); |
| 13 |
self::$debug = $debug; |
| 14 |
} |
| 15 |
|
| 16 |
public static function catch($e, $data = [], $limit = 8) |
| 17 |
{ |
| 18 |
$msg = []; |
| 19 |
try { |
| 20 |
$msg['time'] = date("c"); |
| 21 |
|
| 22 |
if (function_exists('is_wp_error') && is_wp_error($e)) { |
| 23 |
$msg['msg'] = $e->get_error_message(); |
| 24 |
} else if ($e instanceof \Exception || $e instanceof \Throwable) { |
| 25 |
$msg['msg'] = $e->getMessage(); |
| 26 |
$msg['file'] = $e->getFile(); |
| 27 |
$msg['line'] = $e->getLine(); |
| 28 |
} else { |
| 29 |
$msg['msg'] = $e; |
| 30 |
} |
| 31 |
if ($limit > 8) { |
| 32 |
$limit = 8; |
| 33 |
} |
| 34 |
$msg['backtrace'] = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, $limit); |
| 35 |
$msg['server'] = $_SERVER; |
| 36 |
|
| 37 |
$msgStr = json_encode($msg); |
| 38 |
self::$log->fac($msgStr); |
| 39 |
if (self::$debug) { |
| 40 |
echo $msgStr; |
| 41 |
error_log($msgStr); |
| 42 |
} |
| 43 |
} catch (\Throwable $e) { |
| 44 |
if (self::$debug) { |
| 45 |
echo $e->getMessage(); |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
public static function &getAndClean() |
| 51 |
{ |
| 52 |
$data = self::$log->fgc(); |
| 53 |
self::$log->unlink(); |
| 54 |
return $data; |
| 55 |
} |
| 56 |
} |
| 57 |
|