Skip to content

Commit

Permalink
DB/Logs
Browse files Browse the repository at this point in the history
 * set up some query profiling
  • Loading branch information
Sarjuuk committed May 28, 2020
1 parent c290f84 commit d22e90c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
31 changes: 31 additions & 0 deletions includes/database.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class DB
private static $optionsCache = [];
private static $connectionCache = [];

private static $logs = [];

private static function createConnectSyntax(&$options)
{
return 'mysqli://'.$options['user'].':'.$options['pass'].'@'.$options['host'].'/'.$options['db'];
Expand Down Expand Up @@ -58,6 +60,35 @@ public static function errorHandler($message, $data)
exit;
}

public static function logger($self, $query, $trace)
{
if ($trace) // actual query
self::$logs[] = [substr(str_replace("\n", ' ', $query), 0, 200)];
else // the statistics
{
end(self::$logs);
self::$logs[key(self::$logs)][] = substr(explode(';', $query)[0], 5);
}
}

public static function getLogs()
{
$out = '<pre><table style="font-size:12;"><tr><th></th><th>Time</th><th>Query</th></tr>';
foreach (self::$logs as $i => [$l, $t])
{
$c = 'inherit';
preg_match('/(\d+)/', $t, $m);
if ($m[1] > 100)
$c = '#FFA0A0';
else if ($m[1] > 20)
$c = '#FFFFA0';

$out .= '<tr><td>'.$i.'.</td><td style="background-color:'.$c.';">'.$t.'</td><td>'.$l.'</td></tr>';
}

return Util::jsEscape($out).'</table></pre>';
}

public static function getDB($idx)
{
return self::$interfaceCache[$idx];
Expand Down
14 changes: 14 additions & 0 deletions includes/kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,20 @@
if (!empty($AoWoWconf['aowow']) && User::init())
User::save(); // save user-variables in session

// set up some logging (~10 queries will execute before we init the user and load the config)
if (CFG_DEBUG && User::isInGroup(U_GROUP_DEV | U_GROUP_ADMIN))
{
DB::Aowow()->setLogger(['DB', 'logger']);
DB::World()->setLogger(['DB', 'logger']);
if (DB::isConnected(DB_AUTH))
DB::Auth()->setLogger(['DB', 'logger']);

if (!empty($AoWoWconf['characters']))
foreach ($AoWoWconf['characters'] as $idx => $__)
if (DB::isConnected(DB_CHARACTERS . $idx))
DB::Characters($idx)->setLogger(['DB', 'logger']);
}

// hard-override locale for this call (should this be here..?)
// all strings attached..
if (!empty($AoWoWconf['aowow']))
Expand Down
9 changes: 9 additions & 0 deletions template/bricks/footer.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,14 @@
</noscript>

<script type="text/javascript">DomContentLoaded.now()</script>
<?php
if (CFG_DEBUG && User::isInGroup(U_GROUP_DEV | U_GROUP_ADMIN)):
?>
<script type="text/javascript">
window.open("/", "SqlLog", "width=1800,height=200,top=100,left=100,status=no,location=no,toolbar=no,menubar=no").document.write('<?=DB::getLogs();?>');
</script>
<?php
endif;
?>
</body>
</html>

0 comments on commit d22e90c

Please sign in to comment.