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(); } }

In reply to Threaded logging. Method change help. by glenn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.