DBG.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. Lib::loadClass('User');
  3. class DBG {
  4. public static function isActive($mode = null) {
  5. return (isset($_SESSION)) ? (true == V::get('_DBG_ON', false, $_SESSION)) : false;
  6. }
  7. public static function activate($mode = null) {// @used in User::auth()
  8. if (isset($_SESSION)) $_SESSION['_DBG_ON'] = true;
  9. }
  10. public static function deactivate($mode = 'all') {
  11. if (isset($_SESSION)) $_SESSION['_DBG_ON'] = false;
  12. }
  13. /**
  14. * @param $reqKqy - key in $_REQUEST array (if true then always show)
  15. * @param $reqValueExpr - expression to compare req value
  16. * true - always visible - only for fast DBG without $reqKey in REQUEST
  17. * '>*', '>=*', '<*', '<=*' - compare $reqValue
  18. * examples:
  19. * - show when $_REQUEST['DBG_SCH'] == '1'
  20. * DBG::_('DBG_SCH', '1', "fieldsConfig({$idTable})", $fieldsConfig, __CLASS__, __FUNCTION__, __LINE__);
  21. * - show when $_REQUEST['DBG_SCH'] > '1'
  22. * DBG::_('DBG_SCH', '>1', "fieldsConfig({$idTable})", $fieldsConfig, __CLASS__, __FUNCTION__, __LINE__);
  23. * - show when any value: strlen($_REQUEST['DBG_SCH']) > 0
  24. * DBG::_('DBG_SCH', true, "fieldsConfig({$idTable})", $fieldsConfig, __CLASS__, __FUNCTION__, __LINE__);
  25. * - always show
  26. * DBG::_(true, true, "fieldsConfig({$idTable})", $fieldsConfig, __CLASS__, __FUNCTION__, __LINE__);
  27. */
  28. public static function _($reqKey, $reqValueExpr, $label, $variable, $className, $functionName, $lineNumber, $borderColor = 'red') {
  29. if (!DBG::isActive()) return;
  30. $showDBG = false;
  31. $reqValue = (true === $reqKey || !$reqKey)? '' : V::get($reqKey, '', $_GET);
  32. if (true === $reqKey) {
  33. $showDBG = true;
  34. }
  35. else if (strlen($reqValue) == 0) {
  36. return;
  37. }
  38. else if ($reqValue == $reqValueExpr) {
  39. $showDBG = true;
  40. }
  41. else if (true === $reqValueExpr) {
  42. $showDBG = true;
  43. }
  44. else {
  45. if ('>=' == substr($reqValueExpr, 0, 2)) {
  46. if ($reqValue >= substr($reqValueExpr, 2)) {
  47. $showDBG = true;
  48. }
  49. }
  50. else if ('>' == substr($reqValueExpr, 0, 1)) {
  51. if ($reqValue > substr($reqValueExpr, 1)) {
  52. $showDBG = true;
  53. }
  54. }
  55. else if ('<=' == substr($reqValueExpr, 0, 2)) {
  56. if ($reqValue <= substr($reqValueExpr, 2)) {
  57. $showDBG = true;
  58. }
  59. }
  60. else if ('<' == substr($reqValueExpr, 0, 1)) {
  61. if ($reqValue < substr($reqValueExpr, 1)) {
  62. $showDBG = true;
  63. }
  64. }
  65. else {
  66. if ($reqValue = $reqValueExpr) {
  67. $showDBG = true;
  68. }
  69. }
  70. }
  71. if ($showDBG) {
  72. ?>
  73. <pre style="max-height:200px;max-width:800px;overflow:auto;border:1px solid <?php echo $borderColor; ?>;text-align:left;"
  74. ><?php echo "{$label} ({$className}::{$functionName}:{$lineNumber}):\n"; ?>
  75. <?php print_r($variable);?>
  76. </pre>
  77. <?php
  78. }
  79. }
  80. public static function table($label, $rows, $className, $functionName, $lineNumber) {
  81. $params = array();
  82. $params['caption'] = "{$label} ({$className}::{$functionName}:{$lineNumber}):";
  83. $params['rows'] = $rows;
  84. UI::table($params);
  85. }
  86. public static function nicePrint($variable, $varName) {
  87. $col = ['green'=>'#96c178', 'red'=>'#de6b74', 'blue'=>'#55b5c1', 'bg-dark'=>'#282c34', 'white'=>'#abb2bf', 'orange'=>'#d19a66', 'violet'=>'#c476db'];
  88. $cnt = '';
  89. ob_start();
  90. print_r($variable);
  91. $cnt = ob_get_contents();
  92. ob_end_clean();
  93. $outLines = array();
  94. $lines = explode("\n", $cnt);
  95. foreach ($lines as $line) {
  96. if ('(' == trim($line)) continue;
  97. if (')' == trim($line)) continue;
  98. if ('' == trim($line)) continue;
  99. if ('Array' == substr($line, -5) || 'stdClass' == substr($line, -5)) {
  100. $line = str_replace('Array', '<span style="color:'.$col['blue'].'">Array</span>', $line);
  101. $line = str_replace('stdClass', '<span style="color:'.$col['blue'].'">stdClass</span>', $line);
  102. $line .= ':';
  103. }
  104. if (($firstBracket = strpos($line, '[')) > 0) {
  105. $line = str_replace("\t", ' ', $line);
  106. $splitPos = ($firstBracket > 4)? ($firstBracket - 4) / 2 + 4 : 4;
  107. $line = substr($line, $firstBracket - $splitPos);
  108. $line = preg_replace('/\[(\w+)\]/', '[<span style="color:'.$col['green'].'">\1</span>]', $line);
  109. $line = preg_replace('/\] \=\> (.+)$/', '] => <span style="color:'.$col['orange'].'">\1</span>', $line);
  110. }
  111. $outLines[] = $line;
  112. }
  113. if ($varName) $outLines[0] = '<b style="color:'.$col['red'].'">' . $varName . "</b> => {$outLines[0]}";
  114. $outLines = implode("\n", $outLines);
  115. echo '<pre style="background-color:'.$col['bg-dark'].'; color:'.$col['white'].'; font-size:x-small">' . $outLines . '</pre>' . "\n";
  116. }
  117. /**
  118. * @param $mixedArg string or Exception
  119. */
  120. public static function log($mixedArg) {
  121. if (!self::isActive()) return;
  122. // * TODO: debug to file based on session_id (/tmp/se-debug-{$date("Y-m-d")}-{$login}_{$ip}_{$session_id}.log)
  123. $logInfo = [
  124. 'date' => date("Y-m-d H:i:s"),
  125. 'type' => 'unknown',
  126. 'msg' => '',
  127. 'log' => '',
  128. 'trace' => '',
  129. ];
  130. if ($mixedArg instanceof Exception) {
  131. $logInfo['type'] = 'Exception';
  132. $logInfo['msg'] = $mixedArg->getMessage();
  133. $logInfo['log'] = [
  134. 'code' => $mixedArg->getCode(),
  135. 'line' => $mixedArg->getLine(),
  136. 'file' => str_replace(APP_PATH_ROOT, 'SE', $mixedArg->getFile()),
  137. ];
  138. $logInfo['trace'] = $mixedArg->getTraceAsString();// getTrace
  139. } else if (is_string($mixedArg)) {
  140. $logInfo['type'] = 'string';
  141. $logInfo['msg'] = $mixedArg;
  142. $logInfo['log'] = array();
  143. ob_start();
  144. debug_print_backtrace();
  145. $logInfo['trace'] = ob_get_clean();
  146. } else if (is_array($mixedArg)) {
  147. $logInfo['type'] = 'array';
  148. $logInfo['msg'] = V::get('msg', '', $mixedArg);
  149. $logInfo['log'] = $mixedArg;
  150. ob_start();
  151. debug_print_backtrace();
  152. $logInfo['trace'] = ob_get_clean();
  153. }
  154. $logInfo['trace'] = str_replace(APP_PATH_ROOT, 'SE', $logInfo['trace']);
  155. $logInfo['trace'] .= (("\n" == substr($logInfo['trace'], -1)) ? '' : "\n") . "URI: " . V::get('REQUEST_URI', '', $_SERVER);
  156. error_log(
  157. json_encode($logInfo) . "\n"
  158. , 3
  159. , '/tmp/se-debug-' . implode('-', [
  160. date("Y-m-d"),
  161. User::getLogin(),
  162. Request::getUserIp(),
  163. substr(session_id(), 0, 6),
  164. V::get('REQUEST_TIME', '', $_SERVER)// [REQUEST_TIME] => 1485770466
  165. ]) . '.log'
  166. );
  167. }
  168. }