Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorErich Eckner <git@eckner.net>2018-12-06 09:50:11 +0100
committerErich Eckner <git@eckner.net>2018-12-06 09:50:11 +0100
commitd91e95ba62eb2e7f96571b6b9ec0c7e58148a0a0 (patch)
treeea04c3c15c47e86e660de5d9645fa53680deafa9 /lib
parentc1e427c364784576e37fc792e59d21ee38655c09 (diff)
lib/mysql.php: log queries and their duration
Diffstat (limited to 'lib')
-rw-r--r--lib/mysql.php26
1 files changed, 26 insertions, 0 deletions
diff --git a/lib/mysql.php b/lib/mysql.php
index ec0a1ba..cd21eaa 100644
--- a/lib/mysql.php
+++ b/lib/mysql.php
@@ -13,17 +13,43 @@ if ( $mysql -> connect_error ) {
die_500( "Connection failed: " . $mysql -> connect_error );
}
+function print_important_trace_components($call) {
+ return substr($call['file'], strlen(BASE)+1) . '(' . $call['line'] . ')';
+}
+
+function mysql_log_duration_and_trace($start) {
+ $start = round((microtime(true) - $start) * 1000000);
+ $trace = debug_backtrace();
+ array_shift($trace);
+ // silently fail if logfile is unavailable
+ if (($fp = fopen(BASE . '/log', 'a')) !== false) {
+ flock($fp, LOCK_EX);
+ fwrite($fp,
+ date('Y-m-d H:i:s') . " " .
+ $start . " " .
+ implode(' ', array_map('print_important_trace_components', $trace)) . " - " .
+ $trace[0]['args'][0] . "\n"
+ );
+ flock($fp, LOCK_UN);
+ fclose($fp);
+ }
+}
+
function mysql_run_query($query) {
global $mysql;
+ $start = microtime(true);
if ( ! $result = $mysql -> query($query) )
die_500( "Query failed: " . $mysql -> error );
+ mysql_log_duration_and_trace($start);
return $result;
}
function mysql_prepare_query($query) {
global $mysql;
+ $start = microtime(true);
if ( ! $result = $mysql -> prepare($query) )
die_500( "Prepare failed: " . $mysql -> error );
+ mysql_log_duration_and_trace($start);
return $result;
}