From 6d9db83789e2fa9850080ea21347025aafbe9fff Mon Sep 17 00:00:00 2001 From: kkigomi Date: Sun, 27 Nov 2022 01:46:26 +0900 Subject: [PATCH] =?UTF-8?q?debugbar=20=EC=BF=BC=EB=A6=AC=20=EC=98=A4?= =?UTF-8?q?=EB=A5=98=20=ED=91=9C=EC=8B=9C=20=EC=B6=94=EA=B0=80=20=EB=B0=8F?= =?UTF-8?q?=20G5=5FCOLLECT=5FQUERY=20=EC=84=A4=EC=A0=95=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 210 이슈에서 제안했던 개선안 - debugbar에 오류 등으로 실패한 쿼리가 나오지 않는 문제 수정 - 실패한 쿼리에 오류 메시지 출력 - 쿼리가 실행된 파일, 라인 함수 표시 - G5_COLLECT_QUERY 상수 설정 추가로 디버그 모드(G5_DEBUG)를 켜지 않아도 서드파티 플러그인에서 쿼리 목록을 수집할 수 있도록 개선 --- config.php | 1 + lib/common.lib.php | 62 ++++++++++++++++++++++++++++++++---- plugin/debugbar/debugbar.php | 22 ++++++++++++- plugin/debugbar/style.css | 6 ++-- 4 files changed, 82 insertions(+), 9 deletions(-) diff --git a/config.php b/config.php index 6c2267f9b..b636b0338 100644 --- a/config.php +++ b/config.php @@ -28,6 +28,7 @@ define('G5_HTTPS_DOMAIN', ''); // 그누보드 디버그바 설정입니다, 실제 서버운영시 false 로 설정해 주세요. define('G5_DEBUG', false); +define('G5_COLLECT_QUERY', false); // Set Database table default engine is Database default_storage_engine, If you want to use MyISAM or InnoDB, change to MyISAM or InnoDB. // DB에 테이블 생성 시 테이블의 기본 스토리지 엔진을 설정할 수 있습니다. diff --git a/lib/common.lib.php b/lib/common.lib.php index 6644016da..68e4975f6 100644 --- a/lib/common.lib.php +++ b/lib/common.lib.php @@ -1623,7 +1623,7 @@ function sql_query($sql, $error=G5_DISPLAY_SQL_ERROR, $link=null) $is_debug = get_permission_debug_show(); - $start_time = $is_debug ? get_microtime() : 0; + $start_time = ($is_debug || G5_COLLECT_QUERY) ? get_microtime() : 0; if(function_exists('mysqli_query') && G5_MYSQLI_USE) { if ($error) { @@ -1643,18 +1643,68 @@ function sql_query($sql, $error=G5_DISPLAY_SQL_ERROR, $link=null) } } - $end_time = $is_debug ? get_microtime() : 0; + $end_time = ($is_debug || G5_COLLECT_QUERY) ? get_microtime() : 0; + + $error = null; + if ($is_debug || G5_COLLECT_QUERY) { + if(function_exists('mysqli_error') && G5_MYSQLI_USE) { + $error = array( + 'error_code' => mysqli_errno($g5['connect_db']), + 'error_message' => mysqli_error($g5['connect_db']), + ); + } else { + $error = array( + 'error_code' => mysql_errno($g5['connect_db']), + 'error_message' => mysql_error($g5['connect_db']), + ); + } + + $stack = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); + $source = array(); + $found = false; + + foreach ($stack as $index => $trace) { + if ($trace['function'] === 'sql_query') { + $found = true; + } + if (isset($stack[$index + 1]) && $stack[$index + 1]['function'] === 'sql_fetch') { + continue; + } + + if ($found) { + $trace['file'] = str_replace($_SERVER['DOCUMENT_ROOT'], '', $trace['file']); + $source['file'] = $trace['file']; + $source['line'] = $trace['line']; + + $parent = (isset($stack[$index + 1])) ? $stack[$index + 1] : array(); + if (isset($parent['function'])) { + if (in_array($trace['function'], array('sql_query', 'sql_fetch')) && (isset($parent['function']) && !in_array($parent['function'], array('sql_fetch', 'include', 'include_once', 'require', 'require_once')))) { + if (isset($parent['class']) && $parent['class']) { + $source['class'] = $parent['class']; + $source['function'] = $parent['function']; + $source['type'] = $parent['type']; + } else { + $source['function'] = $parent['function']; + } + } + } + break; + } + } - if($result && $is_debug) { - // 여기에 실행한 sql문을 화면에 표시하는 로직 넣기 $g5_debug['sql'][] = array( 'sql' => $sql, + 'result' => $result, + 'success' => !!$result, + 'source' => $source, + 'error_code' => $error['error_code'], + 'error_message' => $error['error_message'], 'start_time' => $start_time, 'end_time' => $end_time, - ); + ); } - run_event('sql_query_after', $result, $sql, $start_time, $end_time); + run_event('sql_query_after', $result, $sql, $start_time, $end_time, $error); return $result; } diff --git a/plugin/debugbar/debugbar.php b/plugin/debugbar/debugbar.php index fce0c8aad..25c918ca3 100644 --- a/plugin/debugbar/debugbar.php +++ b/plugin/debugbar/debugbar.php @@ -54,7 +54,27 @@ add_stylesheet(' - + + +

+
+ {$function}
"; } ?> +

+ {$query['sql']}

"; + if(!$query['success']) { + echo '

오류: [' . $query['error_code'] . '] ' . $query['error_message'] . '

'; + } + ?> + diff --git a/plugin/debugbar/style.css b/plugin/debugbar/style.css index 88f2a2a27..8b55fec7b 100644 --- a/plugin/debugbar/style.css +++ b/plugin/debugbar/style.css @@ -23,12 +23,14 @@ .debug_table{border:1px solid #ccc;border-collapse:collapse;margin:0;padding:0;width:100%} .debug_table caption{display:none} .debug_table tr{background:#f8f8f8;border:1px solid #ddd;padding:.35em} -.debug_table th,.debug_table td{padding:.625em;text-align:center} +.debug_table th,.debug_table td{padding:.625em;text-align:center;white-space:nowrap;} .debug_table tbody tr:hover td{background:#c7d4dd!important} .debug_table tbody tr:nth-child(even){background-color:#fff} -.debug_table td.left{text-align:left} +.debug_table td.left{text-align:left;font-family:monospace;white-space:initial} .debug_table th{font-size:.85em;letter-spacing:.1em;text-transform:uppercase} .debug_table td img{text-align:center} +.debug_table .query_sql {font-weight:bold;margin:.5em 0} +.debug_table .query_error_message {color:red;margin:.5em 0} .debug_table.hook_table th, .debug_table.hook_table td {text-align:left;border:1px solid #ddd;} .hook_table .hook_count{margin-left:3px;font-size:0.9em;color:#7cbc0a}