diff options
author | Yorhel <git@yorhel.nl> | 2009-11-04 20:42:57 +0100 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2009-11-04 20:42:57 +0100 |
commit | e9fb4c410ef5f20f09feed3a5bb3c5f437530745 (patch) | |
tree | 816149a19e852302b6595787d28cccc51d9af6d6 /lib/Multi | |
parent | 79e15541ff49211663729e597ffa68241b26c171 (diff) |
Multi::Core: Improved logging of warnings and errors
Rather than forwarding STDERR and STDOUT to /dev/null, it is now written
to the log file. This also means that exceptions are now finally logged
somewhere, making debugging a lot easier.
I've also looked at how we should handle exceptions, but the default
action of POE is to let the process die() as any normal script (assuming
a session doesn't sig_handled() the DIE signal), and I couldn't think of
anything better than this default - so let's code with this in mind:
die() when something goes horribly wrong, otherwise just warn() and
handle the error.
Diffstat (limited to 'lib/Multi')
-rw-r--r-- | lib/Multi/Core.pm | 31 |
1 files changed, 22 insertions, 9 deletions
diff --git a/lib/Multi/Core.pm b/lib/Multi/Core.pm index 06ade2f1..5c668b7c 100644 --- a/lib/Multi/Core.pm +++ b/lib/Multi/Core.pm @@ -1,6 +1,6 @@ # -# Multi::Core - handles logging and the main command queue +# Multi::Core - handles spawning and logging # package Multi::Core; @@ -78,24 +78,29 @@ sub _start { setsid(); chdir '/'; umask 0022; - $SIG{__WARN__} = sub {(local$_=shift)=~s/\r?\n//;$poe_kernel->call(core=>log=>'__WARN__: '.$_)}; open STDIN, '/dev/null'; - open STDOUT, '>/dev/null'; - open STDERR, '>/dev/null'; + tie *STDOUT, 'Multi::Core::STDIO', 'STDOUT'; + tie *STDERR, 'Multi::Core::STDIO', 'STDERR'; } -sub log { # level, msg - (my $p = eval { $_[SENDER][2]{$_[CALLER_STATE]}[0] } || '') =~ s/^Multi:://; - my $msg = sprintf '%s::%s: %s', $p, $_[CALLER_STATE], - $_[ARG1] ? sprintf($_[ARG0], @_[ARG1..$#_]) : $_[ARG0]; - +# subroutine, not supposed to be called as a POE event +sub log_msg { # msg + (my $msg = shift) =~ s/\n+$//; open(my $F, '>>', $VNDB::M{log_dir}.'/multi.log'); printf $F "[%s] %s\n", scalar localtime, $msg; close $F; } +# the POE event +sub log { # level, msg + (my $p = eval { $_[SENDER][2]{$_[CALLER_STATE]}[0] } || '') =~ s/^Multi:://; + log_msg sprintf '%s::%s: %s', $p, $_[CALLER_STATE], + $_[ARG1] ? sprintf($_[ARG0], @_[ARG1..$#_]) : $_[ARG0]; +} + + sub pg_error { # ARG: command, errmsg, [ query, params, orig_session, event-args ] my $s = $_[ARG2] ? sprintf ' (Session: %s, Query: "%s", Params: %s, Args: %s)', join(', ', $_[KERNEL]->alias_list($_[ARG4])), $_[ARG2], @@ -120,5 +125,13 @@ sub shutdown { } +# Tiny class for forwarding output for STDERR/STDOUT to the log file using tie(). +package Multi::Core::STDIO; + +use base 'Tie::Handle'; +sub TIEHANDLE { return bless \"$_[1]", $_[0] } +sub WRITE { Multi::Core::log_msg(${$_[0]}.': '.$_[1]) } + + 1; |