From 69502140d2e229a314bb1eb192d31b9a2c14f981 Mon Sep 17 00:00:00 2001 From: Guilhem Moulin Date: Fri, 31 Jul 2015 22:40:28 +0200 Subject: [PATCH] Log debug messages to STDERR unless 'logfile' is set. However don't include timestamps to STDERR, that's the job of the syslog. --- imapsync | 10 ++++++++-- lib/Net/IMAP/Sync.pm | 7 +++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/imapsync b/imapsync index b0226c3..30982af 100755 --- a/imapsync +++ b/imapsync @@ -96,6 +96,9 @@ my ($DBFILE, $LOCKFILE, $LOGGER_FD); or die "Can't open $CONF->{_}->{logfile}: $!\n"; $LOGGER_FD->autoflush(1); } + elsif ($CONFIG{debug}) { + $LOGGER_FD = \*STDERR; + } } my $DBH; @@ -196,14 +199,17 @@ $DBH->do('PRAGMA foreign_keys = ON'); sub msg($@) { my $name = shift; return unless @_; - logger($name, @_) if defined $LOGGER_FD and $LOGGER_FD ne \*STDERR; + logger($name, @_) if defined $LOGGER_FD and $LOGGER_FD->fileno != fileno STDERR; my $prefix = defined $name ? "$name: " : ''; print STDERR $prefix, @_, "\n"; } sub logger($@) { my $name = shift; return unless @_ and defined $LOGGER_FD; - my $prefix = strftime "%b %e %H:%M:%S ", localtime; + my $prefix = ''; + if ($LOGGER_FD->fileno != fileno STDERR) { + $prefix = strftime "%b %e %H:%M:%S ", localtime; + } $prefix .= "$name: " if defined $name; $LOGGER_FD->say($prefix, @_); } diff --git a/lib/Net/IMAP/Sync.pm b/lib/Net/IMAP/Sync.pm index 47c6258..b44f0a6 100644 --- a/lib/Net/IMAP/Sync.pm +++ b/lib/Net/IMAP/Sync.pm @@ -392,7 +392,7 @@ sub DESTROY($) { sub log($@) { my $self = shift; return unless @_; - $self->logger(@_) if defined $self->{'logger-fd'} and $self->{'logger-fd'} ne \*STDERR; + $self->logger(@_) if defined $self->{'logger-fd'} and $self->{'logger-fd'}->fileno != fileno STDERR; my $prefix = defined $self->{name} ? $self->{name} : ''; $prefix .= "($self->{_SELECTED})" if $self->{_STATE} eq 'SELECTED'; print STDERR $prefix, ': ', @_, "\n"; @@ -400,7 +400,10 @@ sub log($@) { sub logger($@) { my $self = shift; return unless @_ and defined $self->{'logger-fd'}; - my $prefix = strftime "%b %e %H:%M:%S ", localtime; + my $prefix = ''; + if ($self->{'logger-fd'}->fileno != fileno STDERR) { + $prefix = strftime "%b %e %H:%M:%S ", localtime; + } $prefix .= defined "$self->{name}" ? $self->{name} : ''; $prefix .= "($self->{_SELECTED})" if $self->{_STATE} eq 'SELECTED'; $self->{'logger-fd'}->say($prefix, ': ', @_); -- 2.39.2