O Monk kith,

A simple question I think, but examples are too big for C.B:

I am trying to modify an OO logging library to use 'format' and 'write' to keep timetamps in one column, and messages in another. I am having trouble figuring out how to name the format and then refer to it successfully. The log file works pretty well in the original form, but after making the changes shown below, I get this error:
syntax error at Logger.pm line 60, near "format $FH "
If I give format/filehandle as just 'FILEHANDLE' instead of '$FH' no text is added to the log file or to stdout, and the driver runs forever, then dies with:
Can't coerce UNKNOWN to string in formline at Logger.pm line 94.
subs with modified code are here:
sub lwrite{ my($self,$msg,$value)=@_; # args == object, message and + optional value my $FH=*{$$self{FILEHANDLE}}; # Make a filehandle string fr +om the blob my $tstamp; format $FH = ^<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $tstamp, $msg ~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $msg ~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $msg ~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $msg ~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $msg ~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $msg ~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $msg ~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $msg ~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $msg . # Get the local time into an array my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localti +me(time()); my @wdays = qw(Sun Mon Tue Wed Thu Fri Sat); # Make an array o +f weekday names my $weekday = $wdays[$wday]; # Get today's weekday name $year = $year + 1900; # Calculate the current year $mon = $mon + 1; # Calculate the current month # Now format the message with a time stamp in front of the message $tstamp = sprintf("[$weekday %4d-%02d-%02d %02d:%02d:%02d]", $year +, $mon, $mday, $hour, $min, $sec); if(! write $FH ){ # Log it if we cannot print to the file $self->log("Unable to write to $$self{LOG}: $!\n"); } print "\n$msg \n" if ($value ==1); # Print to monitor as w +ell if value == 1 } sub log{ my ($self,$error,$value)=@_; # Args == object, error messag +e and value $self->clear; # Clear last error from the object $$self{ERROR}=$error; # Add the error message to the ob +ject push @{$$self{HISTORY}},$error; # Push the error into the h +istory array carp "$error\n" if $$self{CARP}; # Carp if an error with th +is object $self->lwrite($error,$value); # Send the message and value +to "lwrite" sub above }
Original code is here:
###################################################################### +########## # NAME: Logger # PURPOSE: Single interface for log output # AUTHOR: Marty Gipson, Larry Stone # VERSION: 2006.03.07 11:00 ###################################################################### +########## package Logger; use strict; use Carp; use FileHandle; use Time::HiRes qw/usleep/; use vars qw /$VERSION $SEM $ms/; # Logger is intended to be a single interface for all logging action. +Each instance of Logger is intended to be an output for a particular +type of log; # Logger will save a txt file under c:\\Qagentlogs on windows side and + /root/Qagentlogs on linux # When calling logger, the user should pass filename , message and '1' + to print output to screen. sub new{ my($class,%args)=@_; my $self = bless{ LOG => "c:\\ShopTestHarnessLogs\\$args{LOG}" || croak "No l +ogfile!\n", # Need a name for the logfile or croak CARP => $args{CARP} || undef, FILEHANDLE => new FileHandle || croak "Unable to get filehandle\n" +, # Croak if unable to get filehandle ERROR => "", HISTORY => [], # Create an array for history },$class; if(! $self->open_log()){ die"Unable to open $self->{LOG}\n"; # Croak if we can't open th +e file } return $self; # Return the object } sub get_log{ my $self=shift; return $$self{LOG}; # Return the log } sub open_log{ my $self=shift; my $path="c:\\ShopTestHarnessLogs"; unless (opendir(DIR,$path)) { system("md c:\\ShopTestHarnessLogs") } if(! open($$self{FILEHANDLE},">>$$self{LOG}")){ $self->log("Unable to open logfile\n"); return 0; # On failure, return 0 } $$self{FILEHANDLE}->autoflush(1); return 1; # On success, return 1 } sub write{ my($self,$msg,$value)=@_; # args == object, message and + optional value my $FH=*{$$self{FILEHANDLE}}; # Make a filehandle string fr +om the blob # Get the local time into an array my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localti +me(time()); my @wdays = qw(Sun Mon Tue Wed Thu Fri Sat); # Make an array o +f weekday names my $weekday = $wdays[$wday]; # Get today's weekday name $year = $year + 1900; # Calculate the current year $mon = $mon + 1; # Calculate the current month # Now format the message with a time stamp in front of the message my $format = sprintf("[$weekday %4d-%02d-%02d %02d:%02d:%02d] $msg +", $year, $mon, $mday, $hour, $min, $sec); if(! print $FH "$format\n"){ # Log it if we cannot print to + the file $self->log("Unable to write to $$self{LOG}: $!\n"); } print "\n$msg \n" if ($value ==1); # Print to monitor as w +ell if value == 1 } sub clear{ # Clear the error (make it undef) my $self=shift; $$self{ERROR}=undef; } sub log{ my ($self,$error,$value)=@_; # Args == object, error messag +e and value $self->clear; # Clear last error from the object $$self{ERROR}=$error; # Add the error message to the ob +ject push @{$$self{HISTORY}},$error; # Push the error into the h +istory array carp "$error\n" if $$self{CARP}; # Carp if an error with th +is object $self->write($error,$value); # Send the message and value t +o "write" sub above } 1;
Here is a driver to test it.
use Logger; # Create the Logger object passing file name my $log=Logger->new(LOG=>"FILE.LOG",CARP=>1); # Call log method passing the message and 1 to print to screen or 0 n +ot to print to screen. $log->log("A",1); $log->log("qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwe +rtyuiopasdfghjklzxcvbnm ",1); $log->log("12345690123456789012345678990",1);

Edited by planetscape - added readmore tags

( keep:0 edit:14 reap:0 )


In reply to Format ignorance by sailortailorson

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.