NAME
Finance::Shares::Log - Keep track of activity in Finance::Share modules
SYNOPSIS
This module houses three groups of functions. The main object handles Log files. Exported functions are provided that assist with date and file handling.
use Finance::Shares::Log qw(:date :file);
use Finance::Shares::Log qw(
today_as_days
today_as_string
days_from_time
time_from_days
string_from_days
days_from_string
ymd_from_days
days_from_ymd
ymd_from_string
string_from_ymd
expand_tilde
check_file
fetch_line
);
Log methods
The simplest usage sends messages to a specified file. Sending a message with level 0 is equivalent to the builtin die
.
my $lf = Finance::Shares::Log->new("logfile.txt");
$lf->log(1, $message);
$lf->log(0, $fatal_message);
This can be expanded by adding level()
and file()
.
my $lf = Finance::Shares::Log->new();
$lf->file("myprog.log", "~/logs");
$lf->level(5);
$lf->log(5, "This message gets through");
$lf->log(6, "While this is ignored");
Although the main uses are given above, lower level access is available.
my $lf = Finance::Shares::Log->new();
$lf->open("~/myprog.log");
$lf->print("Own level monitoring") unless ($lf->level() > 2);
my using_file = $lf->is_file();
my open_file = $lf->is_open();
$lf->close();
Date functions
$days = today_as_days();
$date = today_as_string();
$days = days_from_time( $time );
$time = time_from_days( $days );
$date = string_from_days( $days );
$days = days_from_string( $date );
($year, $month, $day) = ymd_from_days( $days );
$days = days_from_ymd( $year, $month, $day );
($year, $month, $day) = ymd_from_string( $date );
$date = string_from_ymd( $year, $month, $day );
File functions
$abs_file = check_file( $rel_file );
$abs_file = check_file( $abs_file );
$abs_file = check_file( $file, $rel_dir );
$abs_file = check_file( $file, $abs_dir );
$abs_file = check_file( $rel_file, $abs_dir );
$abs_file = expand_tilde( $shell_name );
$line = fetch_line( FILE_HANDLE );
DESCRIPTION
A centralized logging system is used to keep track of which stock prices are fetched from the internet and what processing has been done on them.
CONSTRUCTOR
new( [file, [dir, [level]]] )
file
-
An optional fully qualified path-and-file, a simple file name, or "" for null device.
dir
-
An optional directory. If present (and
file
is not already an absolute path), it is prepended tofile
. level
-
The logging threshold. Messages with numbers greater than this will be ignored. (Default: 2)
Create a new log object. If filename
already exists, logging will just be appended. Any leading '~' is expanded, otherwise any arguments are handled using File::Spec so should be portable.
Logged data will be sent to stdout if no file is given. If file
is given the value "", all logged data is suppressed.
OBJECT METHODS
log( priority, string_or_array )
priority
-
This is compared with the value passed to
level()
to determine whether the data gets logged or ignored. Higher numbers should be used for greater detail; lower numbers for messages that are more important. A value of '0' signifies a fatal error message - the method callsdie
. string_or_array
-
The data to be sent to the log. A timestamp and a trailing "\n" will be added.
The main logging function. This opens the file, send the data to it and closes it again (if necessary). Use the lower level print()
method if this proves inappropriate.
ACCESS METHODS
file( [file [, dir]] )
file
-
An optional fully qualified path-and-file, a simple file name, or "" for null device.
dir
-
An optional directory. If present (and
file
is not already an absolute path), it is prepended tofile
.
Specify the file to use. If it doesn't already exist, it is created. With no arguments, this redirects output to STDERR, while "" is interpreted as the NULL device.
Returns the name of the log file.
level( [level] )
level
should be a number 0 or greater. Data sent to log()
with a number less than or equal to this will be output. Messages with levels greater than this will be suppressed.
A level of 0 supresses all except fatal messages.
Returns the priority threshold.
SUPPORT METHODS
open( [file, [dir]] )
file
-
An optional fully qualified path-and-file, a simple file name, or "" for null device.
dir
-
An optional directory
dir
. If present (andfile
is not already an absolute path), it is prepended tofile
.
If a file has been given, an attempt is made to open it for appending data. The special name "" sends all logged data to /dev/null. Alternatively, if the file is a handle such as STDOOUT
, the data will be sent there. The method either dies (if no file has been specified or it cannot be opened) or data may be written.
is_open()
Return true
if the log file is open.
is_file()
Return true
if the log file is an actual file.
close()
Closes the log file.
print( string_or_array )
Unlike the builtin print
, a timestamp is output before the parameter(s) and a "\n" is added on the end.
open()
must have been called beforehand, and close()
should be called after the printing is done.
DATE FUNCTIONS
There are three types of dates here. A 'days' value is the number of days from some arbitrary day zero. A 'date' is a string in YYYY-MM-DD format while 'ymd' refers to an array holding a year, month and day such as (2002, 12, 31). See SYNOPSIS for all the functions.
All the work is done by David Eisenberg's Date::Pcalc module. These function just provide an interface convenient for the Finance::Shares modules.
FILE FUNCTIONS
check_file( file, [dir] )
file
-
An optional fully qualified path-and-file, a simple file name, or "".
dir
-
An optional directory
dir
. If present (andfile
is not already an absolute path), it is prepended tofile
.
If no directory is given either way, the current directory is assumed. Any leading '~' is expanded to the users home directory, and the file is created (with any intervening directories) if it doesn't exist.
If file
is given "" the special file File::Spec->devnull() is returned.
File::Spec is used throughout so file access should be portable.
expand_tilde( dir )
Expands any leading '~' to the home directory.
fetch_line( FILE_HANDLE )
Return the next line of data from an open file. Blank lines and '#' comments are skipped and leading and trailing space is removed.
BUGS
Please report those you find to the author.
AUTHOR
Chris Willmot, chris@willmot.org.uk