Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

filehandle for a log file with many packages 1

Status
Not open for further replies.

eve25

Programmer
Feb 9, 2004
32
US
Hi guys,
I would need a log file for my program but this one is composed with 5 different packages...and 1 program making the link to all the packages.
A filehandle is proper to a package and I know how to pass it from one prog/package to another one with aliases and using local... I can to that from the 'linking' program I got but it involves to pass the filehandle to each package and each time open and close rthe corresponding file in each package...

Would you have any other idea,? I bet I am not the only one needing a log files common to many packages...

Thank you,
Have a great nite!
 
eve,

I would use flock to ensure that no two (or more) processes wrote to the log file at anyone time, and wouldn't worry about passing handles.

Just open the file from each package - How much logging do you anticipate from each?

--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
I don't understand. If you are using packages you simply do a use <packagename> right?
 
The way to solve your problem is to write a separate LOG package /object that you can pass the object reference to your diverse functions within your packages. Since I can't seem to fall asleep, I'll write it for you :) to illustrate......

LOG.pm
----------------------------------------------------------
package LOG;

my $self = {};

sub new
{
my $class = shift;
my $filename = shift;
open LOG, ">>$filename" or die "$filename - $!";
$self{FILEHND} = \*LOG;
bless $self, $class;
return $self;
}

sub write
{
my $self = shift;
my $text = shift;
my $SAVEHND = select $self{FILEHND} ;
print $text, "\n";
select $SAVEHND;
}

DESTROY {
close $self{FILEHND} if exists $self{FILEHND};
}

1;
----------------------------------------------------------
MYPACK.pm
----------------------------------------------------------
package MYPACK; #Simple module

sub writesomething {
my $log = shift;
$log->write("I'm in writesomething");
}

sub writesomethingelse {
my $log = shift;
$log->write("I'm in writesomethingelse");
}

1;
----------------------------------------------------------
test program
----------------------------------------------------------

#!/usr/bin/perl

use LOG;
use MYPACK;

my $log = LOG->new("log.me");

MYPACK::writesomething($log);
MYPACK::writesomethingelse($log);
----------------------------------------------------------

Check the file log.me....

Cheers!
 
Thanks so much laserBeam, I am gonna try but I am sure it will work!
That's great and it made me go into the OOP features which is worse it!
Thanks a lot again, that is a great help and I am sure would be useful for many of us!!!

Have a wonderful week!
Eve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top