Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

many files one log file

by glenn (Scribe)
on Dec 03, 2013 at 22:53 UTC ( [id://1065504]=perlquestion: print w/replies, xml ) Need Help??

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

So I am updating this program and all was going well until I tried to make main plus all libraries write to the same log file for debugging. It used to work when everything ran sequential but i'm threading it now to add flexibility. So what i have tried to do is open the log file in MAIN then pass the handle to the libraries. To ensure that two write dont occur at the same time i'm also passing a semaphore. If you have a better way or can make this work please let me know. I think one problem is the libraries are loaded before the variables are made available but putting them in BEGIN breaks stuff.

MAIN:
use Thread::Semaphore; my $writelog = Thread::Semaphore->new(); #sequential log writing use diagConfig; #$sourcepath, %files, and helper functions open my $LOGFH ,">", $files{log}{file}; #disable write buffer my $stdout = select($LOGFH); $| = 1; select($stdout); use License (\$LOGFH, \$writelog);

LIB:
package License; use strict; no strict "refs"; my $LOGFH = undef; my $writelog = undef; use diagConfig; #$sourcepath, %files, trimwhitespace, cleanupWincuri, +$WINCURI, $PLINK, $CLI sub import { shift; $LOGFH = ${$_[0]}; $writelog = ${$_[1]}; if (defined $writelog) { $writelog->down(); $writelog->up(); } else { print "semaphore is not defined\n"; } if (defined $LOGFH) { print $LOGFH "Initiator loaded\n"; } else { print "logfile is not defined\n"; } }

Replies are listed 'Best First'.
Re: many files one log file
by BrowserUk (Patriarch) on Dec 04, 2013 at 00:37 UTC

    Ditch Thread::Semaphore and use a shared variable:

    my $logSem : shared; ... { lock $logSem; print LOGFH ...; }

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: many files one log file
by Eliya (Vicar) on Dec 04, 2013 at 00:49 UTC
    I think one problem is the libraries are loaded before the variables are made available but putting them in BEGIN breaks stuff.

    I wonder what exactly you have tried, and how BEGIN did break stuff. As you suspected, initialising the variables in a BEGIN block should work to get the values passed to the lib.

    A heavily simplified snippet might help ($foo represents $LOGFH etc. that you want to pass to the lib):

    #!/usr/bin/perl -w use strict; my $foo; BEGIN { $foo = "bar" } use License ($foo);

    with License.pm being

    package License; use strict; my $foo; sub import { shift; $foo = $_[0]; print "foo = $foo\n"; } 1;

    should print "foo = bar" when the main program is being run.

    Note that $foo has to be declared outside of the BEGIN { }, because the latter implies an extra block scope.

    Another way would be to use require instead of use and call the import method explicitly:

    my $foo = "bar"; require License; License->import($foo);

    In this case, the three statements would be executed at runtime in the sequence they appear, so $foo would be properly initialised before the import method is being called.

      Just tried it, like i posted, and it worked like a champ thank you so much.
      Lib template
      MAIN:
      sharedFunctions:
      Thank you. So it broke using the variables in main stating that it must be called main::$LOGFH.

      As for using require do you think this would work?
      Orignal:
      use diagConfig; #$sourcepath, %files, trimwhitespace, cleanupWincuri, +$WINCURI, $PLINK, $CLI open my $LOGFH ,">", $files{log}{file}; #disable write buffer my $stdout = select($LOGFH); $| = 1; select($stdout); use VolumeManager (\$LOGFH, \$writelog); use Initiator (\$LOGFH, \$writelog); my $vm = VolumeManager->new(); my $init = Initiator->new();

      Require:
      use diagConfig; #$sourcepath, %files, trimwhitespace, cleanupWincuri, +$WINCURI, $PLINK, $CLI open my $LOGFH ,">", $files{log}{file}; #disable write buffer my $stdout = select($LOGFH); $| = 1; select($stdout); require VolumeManager; VolumeManager->import(\$LOGFH, \$writelog); my $vm = VolumeManager->new(); require Initiator; Initiator->import(\$LOGFH, \$writelog); my $init = Initiator->new();

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (2)
As of 2024-03-19 07:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found