| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- <?php
- Lib::loadClass('User');
- class DBG {
- public static function isActive($mode = null) {
- return (isset($_SESSION)) ? (true == V::get('_DBG_ON', false, $_SESSION)) : false;
- }
- public static function activate($mode = null) {// @used in User::auth()
- if (isset($_SESSION)) $_SESSION['_DBG_ON'] = true;
- }
- public static function deactivate($mode = 'all') {
- if (isset($_SESSION)) $_SESSION['_DBG_ON'] = false;
- }
- /**
- * @param $reqKqy - key in $_REQUEST array (if true then always show)
- * @param $reqValueExpr - expression to compare req value
- * true - always visible - only for fast DBG without $reqKey in REQUEST
- * '>*', '>=*', '<*', '<=*' - compare $reqValue
- * examples:
- * - show when $_REQUEST['DBG_SCH'] == '1'
- * DBG::_('DBG_SCH', '1', "fieldsConfig({$idTable})", $fieldsConfig, __CLASS__, __FUNCTION__, __LINE__);
- * - show when $_REQUEST['DBG_SCH'] > '1'
- * DBG::_('DBG_SCH', '>1', "fieldsConfig({$idTable})", $fieldsConfig, __CLASS__, __FUNCTION__, __LINE__);
- * - show when any value: strlen($_REQUEST['DBG_SCH']) > 0
- * DBG::_('DBG_SCH', true, "fieldsConfig({$idTable})", $fieldsConfig, __CLASS__, __FUNCTION__, __LINE__);
- * - always show
- * DBG::_(true, true, "fieldsConfig({$idTable})", $fieldsConfig, __CLASS__, __FUNCTION__, __LINE__);
- */
- public static function _($reqKey, $reqValueExpr, $label, $variable, $className, $functionName, $lineNumber, $borderColor = 'red') {
- if (!DBG::isActive()) return;
- $showDBG = false;
- $reqValue = (true === $reqKey || !$reqKey)? '' : V::get($reqKey, '', $_GET);
- if (true === $reqKey) {
- $showDBG = true;
- }
- else if (strlen($reqValue) == 0) {
- return;
- }
- else if ($reqValue == $reqValueExpr) {
- $showDBG = true;
- }
- else if (true === $reqValueExpr) {
- $showDBG = true;
- }
- else {
- if ('>=' == substr($reqValueExpr, 0, 2)) {
- if ($reqValue >= substr($reqValueExpr, 2)) {
- $showDBG = true;
- }
- }
- else if ('>' == substr($reqValueExpr, 0, 1)) {
- if ($reqValue > substr($reqValueExpr, 1)) {
- $showDBG = true;
- }
- }
- else if ('<=' == substr($reqValueExpr, 0, 2)) {
- if ($reqValue <= substr($reqValueExpr, 2)) {
- $showDBG = true;
- }
- }
- else if ('<' == substr($reqValueExpr, 0, 1)) {
- if ($reqValue < substr($reqValueExpr, 1)) {
- $showDBG = true;
- }
- }
- else {
- if ($reqValue = $reqValueExpr) {
- $showDBG = true;
- }
- }
- }
- if ($showDBG) {
- ?>
- <pre style="max-height:200px;max-width:800px;overflow:auto;border:1px solid <?php echo $borderColor; ?>;text-align:left;"
- ><?php echo "{$label} ({$className}::{$functionName}:{$lineNumber}):\n"; ?>
- <?php print_r($variable);?>
- </pre>
- <?php
- }
- }
- public static function table($label, $rows, $className, $functionName, $lineNumber) {
- $params = array();
- $params['caption'] = "{$label} ({$className}::{$functionName}:{$lineNumber}):";
- $params['rows'] = $rows;
- UI::table($params);
- }
- public static function nicePrint($variable, $varName) {
- $col = ['green'=>'#96c178', 'red'=>'#de6b74', 'blue'=>'#55b5c1', 'bg-dark'=>'#282c34', 'white'=>'#abb2bf', 'orange'=>'#d19a66', 'violet'=>'#c476db'];
- $cnt = '';
- ob_start();
- print_r($variable);
- $cnt = ob_get_contents();
- ob_end_clean();
- $outLines = array();
- $lines = explode("\n", $cnt);
- foreach ($lines as $line) {
- if ('(' == trim($line)) continue;
- if (')' == trim($line)) continue;
- if ('' == trim($line)) continue;
- if ('Array' == substr($line, -5) || 'stdClass' == substr($line, -5)) {
- $line = str_replace('Array', '<span style="color:'.$col['blue'].'">Array</span>', $line);
- $line = str_replace('stdClass', '<span style="color:'.$col['blue'].'">stdClass</span>', $line);
- $line .= ':';
- }
- if (($firstBracket = strpos($line, '[')) > 0) {
- $line = str_replace("\t", ' ', $line);
- $splitPos = ($firstBracket > 4)? ($firstBracket - 4) / 2 + 4 : 4;
- $line = substr($line, $firstBracket - $splitPos);
- $line = preg_replace('/\[(\w+)\]/', '[<span style="color:'.$col['green'].'">\1</span>]', $line);
- $line = preg_replace('/\] \=\> (.+)$/', '] => <span style="color:'.$col['orange'].'">\1</span>', $line);
- }
- $outLines[] = $line;
- }
- if ($varName) $outLines[0] = '<b style="color:'.$col['red'].'">' . $varName . "</b> => {$outLines[0]}";
- $outLines = implode("\n", $outLines);
- echo '<pre style="background-color:'.$col['bg-dark'].'; color:'.$col['white'].'; font-size:x-small">' . $outLines . '</pre>' . "\n";
- }
- /**
- * @param $mixedArg string or Exception
- */
- public static function log($mixedArg) {
- if (!self::isActive()) return;
- // * TODO: debug to file based on session_id (/tmp/se-debug-{$date("Y-m-d")}-{$login}_{$ip}_{$session_id}.log)
- $logInfo = [
- 'date' => date("Y-m-d H:i:s"),
- 'type' => 'unknown',
- 'msg' => '',
- 'log' => '',
- 'trace' => '',
- ];
- if ($mixedArg instanceof Exception) {
- $logInfo['type'] = 'Exception';
- $logInfo['msg'] = $mixedArg->getMessage();
- $logInfo['log'] = [
- 'code' => $mixedArg->getCode(),
- 'line' => $mixedArg->getLine(),
- 'file' => str_replace(APP_PATH_ROOT, 'SE', $mixedArg->getFile()),
- ];
- $logInfo['trace'] = $mixedArg->getTraceAsString();// getTrace
- } else if (is_string($mixedArg)) {
- $logInfo['type'] = 'string';
- $logInfo['msg'] = $mixedArg;
- $logInfo['log'] = array();
- ob_start();
- debug_print_backtrace();
- $logInfo['trace'] = ob_get_clean();
- } else if (is_array($mixedArg)) {
- $logInfo['type'] = 'array';
- $logInfo['msg'] = V::get('msg', '', $mixedArg);
- $logInfo['log'] = $mixedArg;
- ob_start();
- debug_print_backtrace();
- $logInfo['trace'] = ob_get_clean();
- }
- $logInfo['trace'] = str_replace(APP_PATH_ROOT, 'SE', $logInfo['trace']);
- $logInfo['trace'] .= (("\n" == substr($logInfo['trace'], -1)) ? '' : "\n") . "URI: " . V::get('REQUEST_URI', '', $_SERVER);
- error_log(
- json_encode($logInfo) . "\n"
- , 3
- , '/tmp/se-debug-' . implode('-', [
- date("Y-m-d"),
- User::getLogin(),
- Request::getUserIp(),
- substr(session_id(), 0, 6),
- V::get('REQUEST_TIME', '', $_SERVER)// [REQUEST_TIME] => 1485770466
- ]) . '.log'
- );
- }
- }
|