Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Threaded logging. Method change help.

by glenn (Scribe)
on Jan 23, 2014 at 16:50 UTC ( [id://1071782]=perlquestion: print w/replies, xml ) Need Help??

glenn has asked for the wisdom of the Perl Monks concerning the following question:

From below you should be able to see that logLine is repeated in every library and I would like to move it to common. So the two questions I have are how will 'caller' be affected and how would i correctly provide the FH and SEM?

MAIN.PL
#semaphores my $writelog = Thread::Semaphore->new(); #sequential log writing #LIBS: use common; open my $LOGFH ,">", $files{log}{file}; #from common #disable write buffer my $stdout = select($LOGFH); $| = 1; select($stdout); require other; other->import(\$LOGFH, \$writelog); #FH, SEM logLine("TEST"); sub logLine { my ($text) =@_; chomp($text); my ($package, $filename, $line) = caller; my $FunctionName = (caller(1))[3]; if ($FunctionName =~ m/::(.+)/) { $FunctionName = $1; } my $time = localtime time; #do sprintf left align truncate until sprintf supports if ($files{log}{fileNameLen} =~ m/-\d+\.(\d+)/) { $filename = substr($filename, 0, $1); } if ($files{log}{funNameLen} =~ m/-\d+\.(\d+)/) { $FunctionName = substr($FunctionName, 0, $1); } my $header = sprintf("%*s, %*s, %*s, %*s:", $files{log}{timeLen}, +$time, $files{log}{fileNameLen}, $filename, $files{log}{funNameLen}, +$FunctionName, $files{log}{lineNumLen}, $line); print "$header $text\n"; #only for testing if (defined $writelog and defined $LOGFH) { $writelog->down(); print $LOGFH "$header $text\n"; $writelog->up(); } }
COMMON.PM
package other; use Exporter; our @ISA = qw(Exporter); our @EXPORT = qw($sourcepath %files); our $sourcepath = dirname(Cwd::abs_path(__FILE__)); #Where am I locate +d at? unless ($sourcepath =~ m/\\$/) { $sourcepath .= "\\"; } our %files = ( log => { file => $sourcepath."file.log", fileNameLen => -22.22, funNameLen => -21.21, lineNumLen => 5, timeLen => 24.24, }, );
OTHER.PM
package other.pm use common; sub import { shift; $LOGFH = ${$_[0]}; $writelog = ${$_[1]}; } logLine("TEST"); sub logLine { my ($text) =@_; chomp($text); my ($package, $filename, $line) = caller; my $FunctionName = (caller(1))[3]; if ($FunctionName =~ m/::(.+)/) { $FunctionName = $1; } my $time = localtime time; #do sprintf left align truncate until sprintf supports if ($files{log}{fileNameLen} =~ m/-\d+\.(\d+)/) { $filename = substr($filename, 0, $1); } if ($files{log}{funNameLen} =~ m/-\d+\.(\d+)/) { $FunctionName = substr($FunctionName, 0, $1); } my $header = sprintf("%*s, %*s, %*s, %*s:", $files{log}{timeLen}, +$time, $files{log}{fileNameLen}, $filename, $files{log}{funNameLen}, +$FunctionName, $files{log}{lineNumLen}, $line); print "$header $text\n"; #only for testing if (defined $writelog and defined $LOGFH) { $writelog->down(); print $LOGFH "$header $text\n"; $writelog->up(); } }
the other sub to move to common:
sub debugLogFunctionNameLineNum { #debugLogFunctionNameLineNum((caller(0))[2],(caller(1))[3], @_); my ($callerline, $caller, @input) = @_; my $input_msg = ""; for (my $i = 0; $i < @input; $i++) { unless (defined $input[$i]) { $input[$i] = "undef"; } $input_msg .= "$i=>$input[$i]"; if ($i < $#input) { $input_msg .= ", "; } } my ($package, $filename, $line) = caller; unless ($caller) { $caller = $filename; } if ($caller =~ m/eval/) { $caller = "Tk call"; $callerline = "Unknown"; } else { if ($callerline =~ m/::(.+)/) { $callerline = $1; } } my $FunctionName = (caller(1))[3]; if ($FunctionName =~ m/::(.+)/) { $FunctionName = $1; } my $time = localtime time; unless ($FunctionName) { $FunctionName = "MAIN"; } #do sprintf left align truncate until sprintf supports if ($files{log}{fileNameLen} =~ m/-\d+\.(\d+)/) { $filename = substr($filename, 0, $1); } if ($files{log}{funNameLen} =~ m/-\d+\.(\d+)/) { $FunctionName = substr($FunctionName, 0, $1); } my $header = sprintf("%*s, %*s, %*s, %*s:", $files{log}{timeLen}, +$time, $files{log}{fileNameLen}, $filename, $files{log}{funNameLen}, +$FunctionName, $files{log}{lineNumLen}, $line); print "$header Called from [$caller] at [$callerline] with options + [$input_msg]\n"; #only for testing if (defined $writelog and defined $LOGFH) { $writelog->down(); print $LOGFH "$header Called from [$caller] at [$callerline] w +ith options [$input_msg]\n"; $writelog->up(); } }

Replies are listed 'Best First'.
Re: Threaded logging. Method change help.
by Laurent_R (Canon) on Jan 23, 2014 at 17:52 UTC
    Your filehandle is a lexical FH, so you can pass it as an argument, just as other variables. As for SEM, I don't really see what you are talking about. Check the caller EXPR syntax, I don't remember the exact details, but you can specify the number of stack frames to go back.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1071782]
Approved by boftx
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-03-19 09:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found