2014-05-20 12:17:01 +02:00
|
|
|
# File: SQLiteLogger
|
|
|
|
# Author: pragma_
|
|
|
|
#
|
|
|
|
# Purpose: Logs SQLite trace messages to Logger.pm with profiling of elapsed
|
|
|
|
# time between messages.
|
|
|
|
|
2017-03-05 22:33:31 +01:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2014-05-20 12:17:01 +02:00
|
|
|
package PBot::SQLiteLogger;
|
|
|
|
|
2020-02-08 20:04:13 +01:00
|
|
|
use strict; use warnings;
|
2019-07-11 03:40:53 +02:00
|
|
|
use feature 'unicode_strings';
|
|
|
|
|
2014-05-20 12:17:01 +02:00
|
|
|
use Time::HiRes qw(gettimeofday);
|
|
|
|
|
2020-02-08 20:04:13 +01:00
|
|
|
sub new {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($class, %conf) = @_;
|
|
|
|
my $self = {};
|
|
|
|
$self->{buf} = '';
|
|
|
|
$self->{timestamp} = gettimeofday;
|
2020-02-20 00:09:41 +01:00
|
|
|
$self->{pbot} = $conf{pbot};
|
2020-02-15 23:38:32 +01:00
|
|
|
return bless $self, $class;
|
2014-05-20 12:17:01 +02:00
|
|
|
}
|
|
|
|
|
2020-02-08 20:04:13 +01:00
|
|
|
sub log {
|
2020-02-15 23:38:32 +01:00
|
|
|
my $self = shift;
|
|
|
|
$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 12:17:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub log_message {
|
2020-02-15 23:38:32 +01:00
|
|
|
my $self = shift;
|
|
|
|
my $now = gettimeofday;
|
|
|
|
my $elapsed = $now - $self->{timestamp};
|
|
|
|
if ($elapsed >= 0.100) { $self->{pbot}->{logger}->log("^^^ SLOW SQL ^^^\n"); }
|
|
|
|
$elapsed = sprintf '%10.3f', $elapsed;
|
|
|
|
$self->{pbot}->{logger}->log("$elapsed : $self->{buf}");
|
|
|
|
$self->{timestamp} = $now;
|
2014-05-20 12:17:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub close {
|
2020-02-15 23:38:32 +01:00
|
|
|
my $self = shift;
|
|
|
|
if ($self->{buf}) {
|
|
|
|
$self->log_message;
|
|
|
|
$self->{buf} = '';
|
|
|
|
}
|
2014-05-20 12:17:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|