Logger - An object to write some traces to a log file in a structured way
This object generates a time stamp computed to be easily manageable (with sort and other unix commands). Because of the last number (iteration), you are sure that the generated stamp is UNIQUE to your program.
#04#11#09#13#16#27#01# means 9 nov 2004, 13h 16mn 27s iteration 01. (N.B. The nb of iterations is limited to 100)
Please note it is very easy to sort the time stamps: the sort utility, without any parameter, sorts the stamps from oldest to newest.
The timer stamp represents an duration; it thus has a different meaning from a time stamp, but the format is nearly the same:
#00#00#00#13#16#27#36# means the elapsed time was 13hours, 16 minutes, 27.36 seconds
If the module Time::HiRes is not available (not installed on system), the timer resolution will be 1s and the last number will be always 00 and the resolution will be limited to 1 s
New my $log = New Logger ($logfile,$log_level,$options);
CalcStamp my $stamp = $log->CalcStamp()
Trace $log->Trace($msg,$stamp,$log_type);
SetLogFile $log->SetLogFile($logfile)
StartTimer $log->StartTimer("timer")
StopTimer my $elapsed = $log->StopTimer("timer")
CalcTimerStamp() my $timer_stamp = $log->CalcStamp("timer")
Usage: my $log = New Logger ($logfile,$log_level,$options); Procedure: Initialize an object, opening the logfile specified. Return: the created object Args: $logfile, the log file name "file.log" opens in write mode ">file.log" opens in write mode, too ">>file.log" opens in append mode $log_level = a string representing a log level (cf. Trace) $options = one or several words describing some options 'COMPRESSION' -> gzip the logfile 'NOCOMPRESSION' -> normal (default option) 'NEWQUIET' -> do not log anything during constructor Ex: 'NOCOMPRESSION NEWQUIET' is a legal options
Usage: my $log->SetLogFile($logfile); Function: Close the current logfile and open a new file Return: nothing Args: $logfile, the new log file name "file.log" opens in write mode ">file.log" opens in write mode, too ">>file.log" opens in append mode
Title: StartTimer Usage: $log->StartTimer('timer') Function: reset and start the timer called 'timer' Call gmtime() and keep the number returned in a private member. Args: The timer name On error: No error
Usage: $log->StopTimer('timer') Function: "stop" the timer called 'timer' Call gmtime() and returns the alapsed time since the number sotred by StartTimer Args: The timer name Return: The elapsed time On error: No error
Usage: $log->CalcStamp("timer") Function: compute a timer stamp Return: the stamp Args: The timer name On error: croak a message
Usage: $log->CalcStamp() Function: compute an unique stamp Return: the stamp Args: none static: stamp_old_time, stamp_seq
Usage: $log->Trace($msg,$stamp,$log_type) Function: write $msg to LOG only if $log_type matches with __log_level If $stamp is "", only $msg is printed If $stamp is ".", print a standard stamp If $stamp is "*", call CalcStamp If $stamp is a name but not a stamp ("timer"), call CalcTimerStamp("timer") If $stamp is a correct stamp (#\d\d# etc.), print this stamp The output is formatted as follows: $stamp $log_type $msg (space is the separator) NO \n is written, so you must provide \n in $msg if needed. You may thus trace a line in two steps: $Trace("first part","#.....#","D"); ... $Trace("second part\n","","D"); $log_type is a regex, a match is done as follows: if ($log_level =~ /$log_type/) { display the line } TIPS ==> If you supply '.', the line is ALWAYS printed.
Return: none Args: $msg = the message to trace $stamp = the stamp to print $log_type = type of log, compare with __log_level