2014-05-20 10:17:01 +00:00
|
|
|
# File: SQLiteLogger
|
|
|
|
#
|
|
|
|
# Purpose: Logs SQLite trace messages to Logger.pm with profiling of elapsed
|
|
|
|
# time between messages.
|
|
|
|
|
2021-07-10 15:00:22 -07:00
|
|
|
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
|
|
|
|
# SPDX-License-Identifier: MIT
|
2017-03-05 21:33:31 +00:00
|
|
|
|
2021-07-23 19:22:25 -07:00
|
|
|
package PBot::Core::Utils::SQLiteLogger;
|
2014-05-20 10:17:01 +00:00
|
|
|
|
2021-06-18 21:23:34 -07:00
|
|
|
use PBot::Imports;
|
2019-07-10 18:40:53 -07:00
|
|
|
|
2014-05-20 10:17:01 +00:00
|
|
|
use Time::HiRes qw(gettimeofday);
|
|
|
|
|
2020-02-08 11:04:13 -08:00
|
|
|
sub new {
|
2021-06-18 21:23:34 -07:00
|
|
|
my ($class, %args) = @_;
|
|
|
|
|
|
|
|
my $self = {
|
|
|
|
pbot => $args{pbot},
|
|
|
|
buf => '',
|
|
|
|
timestamp => scalar gettimeofday,
|
|
|
|
};
|
|
|
|
|
2020-02-15 14:38:32 -08:00
|
|
|
return bless $self, $class;
|
2014-05-20 10:17:01 +00:00
|
|
|
}
|
|
|
|
|
2020-02-08 11:04:13 -08:00
|
|
|
sub log {
|
2020-02-15 14:38:32 -08:00
|
|
|
my $self = shift;
|
2021-06-18 21:23:34 -07:00
|
|
|
|
2020-02-15 14:38:32 -08:00
|
|
|
$self->{buf} .= shift;
|
|
|
|
|
|
|
|
# DBI feeds us pieces at a time, so accumulate a complete line
|
|
|
|
# before outputing
|
|
|
|
if ($self->{buf} =~ tr/\n//) {
|
|
|
|
$self->log_message;
|
|
|
|
$self->{buf} = '';
|
|
|
|
}
|
2014-05-20 10:17:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub log_message {
|
2021-06-18 21:23:34 -07:00
|
|
|
my ($self) = @_;
|
|
|
|
|
2020-02-15 14:38:32 -08:00
|
|
|
my $now = gettimeofday;
|
|
|
|
my $elapsed = $now - $self->{timestamp};
|
2021-06-18 21:23:34 -07:00
|
|
|
|
|
|
|
# log SQL statements that take more than 100ms since the last log
|
2020-02-15 14:38:32 -08:00
|
|
|
if ($elapsed >= 0.100) { $self->{pbot}->{logger}->log("^^^ SLOW SQL ^^^\n"); }
|
2021-06-18 21:23:34 -07:00
|
|
|
|
|
|
|
# log SQL statement and elapsed duration since last statement
|
2020-02-15 14:38:32 -08:00
|
|
|
$elapsed = sprintf '%10.3f', $elapsed;
|
|
|
|
$self->{pbot}->{logger}->log("$elapsed : $self->{buf}");
|
2021-06-18 21:23:34 -07:00
|
|
|
|
|
|
|
# update timestamp
|
2020-02-15 14:38:32 -08:00
|
|
|
$self->{timestamp} = $now;
|
2014-05-20 10:17:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub close {
|
2021-06-18 21:23:34 -07:00
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
# log anything left in buf when closing
|
2020-02-15 14:38:32 -08:00
|
|
|
if ($self->{buf}) {
|
|
|
|
$self->log_message;
|
|
|
|
$self->{buf} = '';
|
|
|
|
}
|
2014-05-20 10:17:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|