From bcf9465dc01fe2fdf32384272efb80fba8461a03 Mon Sep 17 00:00:00 2001 From: Roman Kelesidis Date: Mon, 20 Mar 2023 23:28:10 +0700 Subject: [PATCH] Refactored Dev class (#649) --- common.php | 69 +--------- library/includes/page_footer_dev.php | 2 +- src/Dev.php | 177 ++++++++++++++++++++++++++ src/Legacy/Ajax.php | 2 + src/Legacy/Cache/Common.php | 4 +- src/Legacy/Cache/File.php | 4 +- src/Legacy/Cache/Memcache.php | 4 +- src/Legacy/Cache/Redis.php | 4 +- src/Legacy/Cache/SqliteCommon.php | 3 +- src/Legacy/Datastore/Common.php | 4 +- src/Legacy/Datastore/File.php | 4 +- src/Legacy/Datastore/Memcache.php | 4 +- src/Legacy/Datastore/Redis.php | 4 +- src/Legacy/Datastore/SqliteCommon.php | 3 +- src/Legacy/Dev.php | 84 ------------ src/Legacy/SqlDb.php | 7 +- 16 files changed, 218 insertions(+), 161 deletions(-) create mode 100644 src/Dev.php delete mode 100644 src/Legacy/Dev.php diff --git a/common.php b/common.php index f65d6fb75..db5a0f77f 100644 --- a/common.php +++ b/common.php @@ -52,6 +52,12 @@ if (!file_exists(__DIR__ . '/vendor/autoload.php')) { } require_once __DIR__ . '/vendor/autoload.php'; +/** + * Progressive error reporting + */ +define('DBG_USER', (isset($_COOKIE[COOKIE_DBG]))); +\TorrentPier\Dev::debug_init(); + /** * Gets the value of an environment variable. * @@ -80,9 +86,6 @@ define('FORUM_PATH', $bb_cfg['script_path']); define('FULL_URL', $server_protocol . $bb_cfg['server_name'] . $server_port . $bb_cfg['script_path']); unset($server_protocol, $server_port); -// Debug options -define('DBG_USER', (isset($_COOKIE[COOKIE_DBG]))); - // Board / tracker shared constants and functions define('BB_BT_TORRENTS', 'bb_bt_torrents'); define('BB_BT_TRACKER', 'bb_bt_tracker'); @@ -108,47 +111,6 @@ define('TOR_TYPE_SILVER', 2); define('GUEST_UID', -1); define('BOT_UID', -746); -/** - * Progressive error reporting - */ -if ($bb_cfg['bugsnag']['enabled']) { - if (env('APP_ENV', 'production') !== 'local') { - /** @var Bugsnag\Handler $bugsnag */ - $bugsnag = Bugsnag\Client::make($bb_cfg['bugsnag']['api_key']); - Bugsnag\Handler::register($bugsnag); - } -} else { - if (DBG_USER) { - /** @var Whoops\Run $whoops */ - $whoops = new \Whoops\Run; - - /** - * Show errors on page - */ - $whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler); - - /** - * Show log in browser console - */ - $loggingInConsole = new \Whoops\Handler\PlainTextHandler(); - $loggingInConsole->loggerOnly(true); - $loggingInConsole->setLogger((new \Monolog\Logger(getenv('APP_NAME', 'TorrentPier'), [(new \Monolog\Handler\BrowserConsoleHandler())->setFormatter((new \Monolog\Formatter\LineFormatter(null, null, true)))]))); - $whoops->pushHandler($loggingInConsole); - - /** - * Log errors in file - */ - if (ini_get('log_errors') == 1) { - $loggingInFile = new \Whoops\Handler\PlainTextHandler(); - $loggingInFile->loggerOnly(true); - $loggingInFile->setLogger((new \Monolog\Logger(getenv('APP_NAME', 'TorrentPier'), [(new \Monolog\Handler\StreamHandler(bb_log('', WHOOPS_LOG_FILE, true)))->setFormatter((new \Monolog\Formatter\LineFormatter(null, null, true)))]))); - $whoops->pushHandler($loggingInFile); - } - - $whoops->register(); - } -} - /** * Database */ @@ -217,25 +179,6 @@ if (CHECK_REQIREMENTS['status'] && !CACHE('bb_cache')->get('system_req')) { CACHE('bb_cache')->set('system_req', true); } -function sql_dbg_enabled() -{ - return (SQL_DEBUG && DBG_USER && !empty($_COOKIE['sql_log'])); -} - -function short_query($sql, $esc_html = false) -{ - $max_len = 100; - $sql = str_compact($sql); - - if (!empty($_COOKIE['sql_log_full'])) { - if (mb_strlen($sql, 'UTF-8') > $max_len) { - $sql = mb_substr($sql, 0, 50) . ' [...cut...] ' . mb_substr($sql, -50); - } - } - - return $esc_html ? htmlCHR($sql, true) : $sql; -} - // Functions function utime() { diff --git a/library/includes/page_footer_dev.php b/library/includes/page_footer_dev.php index 06c29c31b..090c24433 100644 --- a/library/includes/page_footer_dev.php +++ b/library/includes/page_footer_dev.php @@ -71,7 +71,7 @@ if (!empty($_COOKIE['explain'])) { } } -$sql_log = !empty($_COOKIE['sql_log']) ? \TorrentPier\Legacy\Dev::get_sql_log() : ''; +$sql_log = !empty($_COOKIE['sql_log']) ? \TorrentPier\Dev::get_sql_log() : ''; if ($sql_log) { echo '
' . ($sql_log ?: '') . '

'; diff --git a/src/Dev.php b/src/Dev.php new file mode 100644 index 000000000..87922f899 --- /dev/null +++ b/src/Dev.php @@ -0,0 +1,177 @@ +pushHandler(new PrettyPageHandler); + + /** + * Show log in browser console + */ + $loggingInConsole = new PlainTextHandler(); + $loggingInConsole->loggerOnly(true); + $loggingInConsole->setLogger((new Logger(getenv('APP_NAME', 'TorrentPier'), [(new BrowserConsoleHandler())->setFormatter((new LineFormatter(null, null, true)))]))); + $whoops->pushHandler($loggingInConsole); + + /** + * Log errors in file + */ + if (ini_get('log_errors') == 1) { + $loggingInFile = new PlainTextHandler(); + $loggingInFile->loggerOnly(true); + $loggingInFile->setLogger((new Logger(getenv('APP_NAME', 'TorrentPier'), [(new StreamHandler(bb_log('', WHOOPS_LOG_FILE, true)))->setFormatter((new LineFormatter(null, null, true)))]))); + $whoops->pushHandler($loggingInFile); + } + + $whoops->register(); + } + } + } + + /** + * Get SQL debug log + * + * @return string + * @throws Exception + */ + public static function get_sql_log(): string + { + global $DBS, $CACHES, $datastore; + + $log = ''; + + foreach ($DBS->srv as $srv_name => $db_obj) { + $log .= !empty($db_obj) ? self::get_sql_log_html($db_obj, "$srv_name [MySQL]") : ''; + } + + foreach ($CACHES->obj as $cache_name => $cache_obj) { + if (!empty($cache_obj->db)) { + $log .= self::get_sql_log_html($cache_obj->db, "cache: $cache_name [{$cache_obj->db->engine}]"); + } elseif (!empty($cache_obj->engine)) { + $log .= self::get_sql_log_html($cache_obj, "cache: $cache_name [{$cache_obj->engine}]"); + } + } + + if (!empty($datastore->db->dbg)) { + $log .= self::get_sql_log_html($datastore->db, 'cache: datastore [' . $datastore->engine . ']'); + } elseif (!empty($datastore->dbg)) { + $log .= self::get_sql_log_html($datastore, 'cache: datastore [' . $datastore->engine . ']'); + } + + return $log; + } + + /** + * Sql debug status + * + * @return bool + */ + public static function sql_dbg_enabled(): bool + { + return (SQL_DEBUG && DBG_USER && !empty($_COOKIE['sql_log'])); + } + + /** + * Short query + * + * @param string $sql + * @param bool $esc_html + * @return string + */ + public static function short_query(string $sql, bool $esc_html = false): string + { + $max_len = 100; + $sql = str_compact($sql); + + if (!empty($_COOKIE['sql_log_full'])) { + if (mb_strlen($sql, 'UTF-8') > $max_len) { + $sql = mb_substr($sql, 0, 50) . ' [...cut...] ' . mb_substr($sql, -50); + } + } + + return $esc_html ? htmlCHR($sql, true) : $sql; + } + + /** + * Get SQL query html log + * + * @param object $db_obj + * @param string $log_name + * + * @return string + * @throws Exception + */ + private static function get_sql_log_html(object $db_obj, string $log_name): string + { + $log = ''; + + foreach ($db_obj->dbg as $i => $dbg) { + $id = "sql_{$i}_" . random_int(0, mt_getrandmax()); + $sql = self::short_query($dbg['sql'], true); + $time = sprintf('%.4f', $dbg['time']); + $perc = @sprintf('[%2d]', $dbg['time'] * 100 / $db_obj->sql_timetotal); + $info = !empty($dbg['info']) ? $dbg['info'] . ' [' . $dbg['src'] . ']' : $dbg['src']; + + $log .= '' + . '
' + . '' . $time . ' ' + . '' . $perc . '' + . ' ' + . '' . $sql . '' + . ' # ' . $info . ' ' + . '
' + . "\n"; + } + return ' +
' . $log_name . '
+ ' . $log . ' + '; + } +} diff --git a/src/Legacy/Ajax.php b/src/Legacy/Ajax.php index dcec7d568..c68a7ae70 100644 --- a/src/Legacy/Ajax.php +++ b/src/Legacy/Ajax.php @@ -9,6 +9,8 @@ namespace TorrentPier\Legacy; +use TorrentPier\Dev; + /** * Class Ajax * @package TorrentPier\Legacy diff --git a/src/Legacy/Cache/Common.php b/src/Legacy/Cache/Common.php index b34124c68..3465c4e71 100644 --- a/src/Legacy/Cache/Common.php +++ b/src/Legacy/Cache/Common.php @@ -9,6 +9,8 @@ namespace TorrentPier\Legacy\Cache; +use TorrentPier\Dev; + /** * Class Common * @package TorrentPier\Legacy\Cache @@ -67,7 +69,7 @@ class Common if ($mode == 'start') { $this->sql_starttime = utime(); - $dbg['sql'] = isset($cur_query) ? short_query($cur_query) : short_query($this->cur_query); + $dbg['sql'] = Dev::short_query($cur_query ?? $this->cur_query); $dbg['src'] = $this->debug_find_source(); $dbg['file'] = $this->debug_find_source('file'); $dbg['line'] = $this->debug_find_source('line'); diff --git a/src/Legacy/Cache/File.php b/src/Legacy/Cache/File.php index 08458fe61..a52045095 100644 --- a/src/Legacy/Cache/File.php +++ b/src/Legacy/Cache/File.php @@ -9,6 +9,8 @@ namespace TorrentPier\Legacy\Cache; +use TorrentPier\Dev; + /** * Class File * @package TorrentPier\Legacy\Cache @@ -24,7 +26,7 @@ class File extends Common { $this->dir = $dir; $this->prefix = $prefix; - $this->dbg_enabled = sql_dbg_enabled(); + $this->dbg_enabled = Dev::sql_dbg_enabled(); } public function get($name, $get_miss_key_callback = '', $ttl = 0) diff --git a/src/Legacy/Cache/Memcache.php b/src/Legacy/Cache/Memcache.php index b01d0eb36..16573aad5 100644 --- a/src/Legacy/Cache/Memcache.php +++ b/src/Legacy/Cache/Memcache.php @@ -9,6 +9,8 @@ namespace TorrentPier\Legacy\Cache; +use TorrentPier\Dev; + /** * Class Memcache * @package TorrentPier\Legacy\Cache @@ -31,7 +33,7 @@ class Memcache extends Common $this->cfg = $cfg; $this->prefix = $prefix; $this->memcache = new \Memcache(); - $this->dbg_enabled = sql_dbg_enabled(); + $this->dbg_enabled = Dev::sql_dbg_enabled(); } public function connect() diff --git a/src/Legacy/Cache/Redis.php b/src/Legacy/Cache/Redis.php index 60714a554..6e6e37c16 100644 --- a/src/Legacy/Cache/Redis.php +++ b/src/Legacy/Cache/Redis.php @@ -9,6 +9,8 @@ namespace TorrentPier\Legacy\Cache; +use TorrentPier\Dev; + /** * Class Redis * @package TorrentPier\Legacy\Cache @@ -31,7 +33,7 @@ class Redis extends Common $this->cfg = $cfg; $this->prefix = $prefix; $this->redis = new \Redis(); - $this->dbg_enabled = sql_dbg_enabled(); + $this->dbg_enabled = Dev::sql_dbg_enabled(); } public function connect() diff --git a/src/Legacy/Cache/SqliteCommon.php b/src/Legacy/Cache/SqliteCommon.php index 4e4e8140e..91f3ad0e6 100644 --- a/src/Legacy/Cache/SqliteCommon.php +++ b/src/Legacy/Cache/SqliteCommon.php @@ -10,6 +10,7 @@ namespace TorrentPier\Legacy\Cache; use SQLite3; +use TorrentPier\Dev; /** * Class SqliteCommon @@ -37,7 +38,7 @@ class SqliteCommon extends Common public function __construct($cfg) { $this->cfg = array_merge($this->cfg, $cfg); - $this->dbg_enabled = sql_dbg_enabled(); + $this->dbg_enabled = Dev::sql_dbg_enabled(); } public function connect() diff --git a/src/Legacy/Datastore/Common.php b/src/Legacy/Datastore/Common.php index 8e257bf78..91b2738f7 100644 --- a/src/Legacy/Datastore/Common.php +++ b/src/Legacy/Datastore/Common.php @@ -9,6 +9,8 @@ namespace TorrentPier\Legacy\Datastore; +use TorrentPier\Dev; + /** * Class Common * @package TorrentPier\Legacy\Datastore @@ -142,7 +144,7 @@ class Common if ($mode == 'start') { $this->sql_starttime = utime(); - $dbg['sql'] = isset($cur_query) ? short_query($cur_query) : short_query($this->cur_query); + $dbg['sql'] = Dev::short_query($cur_query ?? $this->cur_query); $dbg['src'] = $this->debug_find_source(); $dbg['file'] = $this->debug_find_source('file'); $dbg['line'] = $this->debug_find_source('line'); diff --git a/src/Legacy/Datastore/File.php b/src/Legacy/Datastore/File.php index 92d2177d1..0dcb1bac1 100644 --- a/src/Legacy/Datastore/File.php +++ b/src/Legacy/Datastore/File.php @@ -9,6 +9,8 @@ namespace TorrentPier\Legacy\Datastore; +use TorrentPier\Dev; + /** * Class File * @package TorrentPier\Legacy\Datastore @@ -23,7 +25,7 @@ class File extends Common { $this->prefix = $prefix; $this->dir = $dir; - $this->dbg_enabled = sql_dbg_enabled(); + $this->dbg_enabled = Dev::sql_dbg_enabled(); } public function store($title, $var) diff --git a/src/Legacy/Datastore/Memcache.php b/src/Legacy/Datastore/Memcache.php index ad19c8c02..3214089e0 100644 --- a/src/Legacy/Datastore/Memcache.php +++ b/src/Legacy/Datastore/Memcache.php @@ -9,6 +9,8 @@ namespace TorrentPier\Legacy\Datastore; +use TorrentPier\Dev; + /** * Class Memcache * @package TorrentPier\Legacy\Datastore @@ -30,7 +32,7 @@ class Memcache extends Common $this->cfg = $cfg; $this->prefix = $prefix; $this->memcache = new \Memcache(); - $this->dbg_enabled = sql_dbg_enabled(); + $this->dbg_enabled = Dev::sql_dbg_enabled(); } public function connect() diff --git a/src/Legacy/Datastore/Redis.php b/src/Legacy/Datastore/Redis.php index 230cd1f43..c67c57d76 100644 --- a/src/Legacy/Datastore/Redis.php +++ b/src/Legacy/Datastore/Redis.php @@ -9,6 +9,8 @@ namespace TorrentPier\Legacy\Datastore; +use TorrentPier\Dev; + /** * Class Redis * @package TorrentPier\Legacy\Datastore @@ -29,7 +31,7 @@ class Redis extends Common $this->cfg = $cfg; $this->redis = new \Redis(); - $this->dbg_enabled = sql_dbg_enabled(); + $this->dbg_enabled = Dev::sql_dbg_enabled(); $this->prefix = $prefix; } diff --git a/src/Legacy/Datastore/SqliteCommon.php b/src/Legacy/Datastore/SqliteCommon.php index fd4433540..7ce6c6bb6 100644 --- a/src/Legacy/Datastore/SqliteCommon.php +++ b/src/Legacy/Datastore/SqliteCommon.php @@ -10,6 +10,7 @@ namespace TorrentPier\Legacy\Datastore; use SQLite3; +use TorrentPier\Dev; /** * Class SqliteCommon @@ -37,7 +38,7 @@ class SqliteCommon extends Common public function __construct($cfg) { $this->cfg = array_merge($this->cfg, $cfg); - $this->dbg_enabled = sql_dbg_enabled(); + $this->dbg_enabled = Dev::sql_dbg_enabled(); } public function connect() diff --git a/src/Legacy/Dev.php b/src/Legacy/Dev.php deleted file mode 100644 index 8addc53dd..000000000 --- a/src/Legacy/Dev.php +++ /dev/null @@ -1,84 +0,0 @@ -srv as $srv_name => $db_obj) { - $log .= !empty($db_obj) ? self::get_sql_log_html($db_obj, "$srv_name [MySQL]") : ''; - } - - foreach ($CACHES->obj as $cache_name => $cache_obj) { - if (!empty($cache_obj->db)) { - $log .= self::get_sql_log_html($cache_obj->db, "cache: $cache_name [{$cache_obj->db->engine}]"); - } elseif (!empty($cache_obj->engine)) { - $log .= self::get_sql_log_html($cache_obj, "cache: $cache_name [{$cache_obj->engine}]"); - } - } - - if (!empty($datastore->db->dbg)) { - $log .= self::get_sql_log_html($datastore->db, 'cache: datastore [' . $datastore->engine . ']'); - } elseif (!empty($datastore->dbg)) { - $log .= self::get_sql_log_html($datastore, 'cache: datastore [' . $datastore->engine . ']'); - } - - return $log; - } - - /** - * Get SQL query html log - * - * @param object $db_obj - * @param string $log_name - * - * @return string - */ - private static function get_sql_log_html($db_obj, $log_name) - { - $log = ''; - - foreach ($db_obj->dbg as $i => $dbg) { - $id = "sql_{$i}_" . random_int(0, mt_getrandmax()); - $sql = short_query($dbg['sql'], true); - $time = sprintf('%.4f', $dbg['time']); - $perc = @sprintf('[%2d]', $dbg['time'] * 100 / $db_obj->sql_timetotal); - $info = !empty($dbg['info']) ? $dbg['info'] . ' [' . $dbg['src'] . ']' : $dbg['src']; - - $log .= '' - . '
' - . '' . $time . ' ' - . '' . $perc . '' - . ' ' - . '' . $sql . '' - . ' # ' . $info . ' ' - . '
' - . "\n"; - } - return ' -
' . $log_name . '
- ' . $log . ' - '; - } -} diff --git a/src/Legacy/SqlDb.php b/src/Legacy/SqlDb.php index 4deaf60e5..35043a61a 100644 --- a/src/Legacy/SqlDb.php +++ b/src/Legacy/SqlDb.php @@ -11,6 +11,7 @@ namespace TorrentPier\Legacy; use mysqli_result; use TorrentPier\Common\Http; +use TorrentPier\Dev; /** * Class SqlDb @@ -58,7 +59,7 @@ class SqlDb global $DBS; $this->cfg = array_combine($this->cfg_keys, $cfg_values); - $this->dbg_enabled = (sql_dbg_enabled() || !empty($_COOKIE['explain'])); + $this->dbg_enabled = (Dev::sql_dbg_enabled() || !empty($_COOKIE['explain'])); $this->do_explain = ($this->dbg_enabled && !empty($_COOKIE['explain'])); $this->slow_time = SQL_SLOW_QUERY_TIME; @@ -896,7 +897,7 @@ class SqlDb $msg[] = sprintf('%-4s', round(sys('la'), 1)); $msg[] = sprintf('%05d', getmypid()); $msg[] = $this->db_server; - $msg[] = short_query($this->cur_query); + $msg[] = Dev::short_query($this->cur_query); $msg = implode(LOG_SEPR, $msg); $msg .= ($info = $this->query_info()) ? ' # ' . $info : ''; $msg .= ' # ' . $this->debug_find_source() . ' '; @@ -1001,7 +1002,7 @@ class SqlDb ' . $this->explain_hold . ' -
' . short_query($dbg['sql'], true) . '  
+
' . Dev::short_query($dbg['sql'], true) . '  

'; break;