Merge pull request #168 from jaredj/warnings

Turn warnings into $qpsmtpd->log() calls
This commit is contained in:
Matt Simerson 2014-12-18 11:21:19 -08:00
commit 7d4c93ed47
7 changed files with 66 additions and 2 deletions

View File

@ -67,6 +67,22 @@ run the 'run' files in the ./ and ./log directories. Any log entries
emitted will get handled per the instructions in log/run. The default
location specified in log/run is log/main/current.
## logging via 'warn'
Any warnings emitted are sent to logging plugins with a deafult log level of
LOGWARN. Warnings can be sent to plugins with a different log level using a
prefix. For example:
`warn 'NOTICE: Danger Will Robinson!'`
Emitting this warning in a plugin is equivalent to using log():
`$self->log(LOGNOTICE, 'Danger Will Robinson!')`
Notably, using 'warn' in a plugin will always result in that warning being
emitted to STDERR when the test suite is executed, wheras messages sent with
log() are not output to the terminal when running the test suite.
## plugin loglevel
Most plugins support a loglevel argument after their config/plugins entry.

View File

@ -93,6 +93,25 @@ sub log {
$self->varlog($trace, join(" ", @log));
}
sub warn_handler {
my $self = shift;
$self->log( $self->warn_level(@_) );
}
sub warn_level {
my ( $self, @warnings ) = @_;
my @levels = ( keys %Qpsmtpd::Constants::log_levels,
qw[ LOGWARNING LOGCRITICAL LOGEMERGENCY ] );
my $levels = join '|', map { s/^LOG//; $_ } @levels;
my $prefix;
$prefix = $1 if $warnings[0] =~ s/^($levels):\s*//;
$prefix = 'WARN' if ! $prefix;
$prefix = 'WARN' if $prefix eq 'WARNING';
$prefix = 'CRIT' if $prefix eq 'CRITICAL';
$prefix = 'EMERG' if $prefix eq 'EMERGENCY';
return log_level("LOG$prefix"), @warnings;
}
sub varlog {
my ($self, $trace) = (shift, shift);
my ($hook, $plugin, @log);

View File

@ -3,7 +3,7 @@ use strict;
require Exporter;
# log levels
my %log_levels = (
our %log_levels = (
LOGDEBUG => 7,
LOGINFO => 6,
LOGNOTICE => 5,
@ -54,7 +54,7 @@ sub return_code {
sub log_level {
my $test = shift;
if ($test =~ /^\d+$/) { # need to return the textural form
foreach (keys %log_levels) {
foreach (sort keys %log_levels) {
return $_ if $log_levels{$_} =~ /$test/;
}
}

View File

@ -17,6 +17,7 @@ delete $ENV{ENV};
$ENV{PATH} = '/bin:/usr/bin:/var/qmail/bin';
my $qpsmtpd = Qpsmtpd::TcpServer->new();
$SIG{__WARN__} = sub { $qpsmtpd->warn_handler(@_) };
$qpsmtpd->load_plugins();
$qpsmtpd->start_connection();
$qpsmtpd->run(\*STDIN); # pass the "socket" like -prefork/-forkserver

View File

@ -180,6 +180,7 @@ if ($PID_FILE) {
# Load plugins here
my $qpsmtpd = Qpsmtpd::TcpServer->new();
$SIG{__WARN__} = sub { $qpsmtpd->warn_handler(@_) };
# Drop privileges
my (undef, undef, $quid, $qgid) = getpwnam $USER

View File

@ -290,6 +290,7 @@ sub run {
# setup qpsmtpd_instance
$qpsmtpd = qpsmtpd_instance();
$SIG{__WARN__} = sub { $qpsmtpd->warn_handler(@_) };
if ($detach) {
open STDIN, '/dev/null' or die "/dev/null: $!";

View File

@ -45,6 +45,8 @@ __load_logging();
__config_dir();
__config();
__warn_level();
done_testing();
sub __run_hooks {
@ -348,6 +350,30 @@ sub __config {
}
}
sub __warn_level {
my ( $level, $msg ) = $qp->warn_level('qwerty');
is( log_level($level), 'LOGWARN', 'Correct log level with no prefix' );
is( $msg, 'qwerty', 'Correct message with no prefix' );
my $s = 'abc123';
$s =~ /(abc)/;
( $level, $msg ) = $qp->warn_level('dvorak');
is( log_level($level), 'LOGWARN', 'Correct level after $1 assignment' );
is( $msg, 'dvorak', 'Correct message with no prefix after $1 assignment' );
( $level, $msg ) = $qp->warn_level('NOTICE: asdf');
is( log_level($level), 'LOGNOTICE', 'Correct level with NOTICE prefix' );
is( $msg, 'asdf', 'Correct message with NOTICE prefix' );
( $level, $msg ) = $qp->warn_level('EMERGENCY:foo');
is( log_level($level), 'LOGEMERG', 'Correct level with EMERGENCY prefix' );
is( $msg, 'foo', 'Correct message with EMERGENCY prefix' );
( $level, $msg ) = $qp->warn_level('NOTICE != LOGNOTICE');
is( log_level($level), 'LOGWARN', 'Correct level with no colon' );
is( $msg, 'NOTICE != LOGNOTICE', 'Correct message with no colon' );
}
1;
package FakeAddress;