File Coverage

blib/lib/Log/Simplest.pm
Criterion Covered Total %
statement 31 36 86.1
branch 3 6 50.0
condition 2 6 33.3
subroutine 9 12 75.0
pod 4 4 100.0
total 49 64 76.5


line stmt bran cond sub pod time code
1             # $Id: Simplest.pm 2468 2009-03-21 02:25:35Z dk $
2             #
3             # This program is free software; you can redistribute it and/or
4             # modify it under the terms of the GNU General Public License
5             # as published by the Free Software Foundation; either version 2
6             # of the License, or (at your option) any later version.
7             #
8             # This program is distributed in the hope that it will be useful,
9             # but WITHOUT ANY WARRANTY; without even the implied warranty of
10             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11             # GNU General Public License for more details.
12             #
13             # You should have received a copy of the GNU General Public License
14             # along with this program; if not, write to the Free Software
15             # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16             #
17             package Log::Simplest;
18 1     1   23933 use vars qw /$VERSION/;
  1         3  
  1         60  
19 1     1   6 use strict;
  1         2  
  1         63  
20              
21             $VERSION = "1.0";
22              
23             # Dmytro Kovalov. 2004-2009
24             # $Author: dk $
25             # $Date: 2009-03-21 11:25:35 +0900 (Sat, 21 Mar 2009) $
26             # $URL: svn://svn/dmytro/Development/perl/modules/Log-Simplest/trunk/lib/Log/Simplest.pm $
27              
28             =head1 NAME
29              
30             Log::Simplest - Simple log module. Writes log messages to file and/or STDERR.
31              
32             You should use this module when you want to have very simple
33             programming interface to log your messages. If you want some
34             flexibility of message format and log file names, you should use some
35             of others Log::* modules available at CPAN.
36              
37             This module gives the least flexibility among existing Log
38             modules. However it requires the least programmer's effort to write log
39             messages to file.
40              
41             =cut
42              
43             =head1 USAGE
44              
45             use Log::Simplest;
46             &Log("Informative log message");
47             &Fatal("I am dying...");
48              
49             Log files opened and closed by module initialization/end routines and
50             does not require programmer's attention. When your main script
51             contains only L directive and does not have any calls
52             to L and L functions, Log::Simplest will:
53              
54             * open log file;
55             * write start time and date of the script;
56             * write end time and date of the script;
57             * close log file.
58              
59             1)
60              
61             Log::Simplest creates log file during module initialization (i.e. load
62             of the module). On file opening module writes time and date of main
63             script start time.
64              
65             Log::Simplest uses environment variable L<${LOG_DIR}> for log file
66             location. Log file is created in directory defined by L<${LOG_DIR}>,
67             if it is defined, or in L if environment variable is not defined
68             or empty.
69              
70             2)
71              
72             Log file name format is fixed and consists of:
73              
74             * name of main script (without extension .pl);
75             * time and date when script started;
76             * PID of main script;
77             * extension .log.
78              
79             3)
80              
81             Log message format is fixed too. Each row contains log message passed
82             to one of the functions L<&Log()> or L<&Fatal()> with pre-pended
83             time-stamp.
84              
85             4)
86              
87             Log file is closed automatically when module unloads with message
88             containing time-stamp when script ended.
89              
90             =cut
91              
92             =head1 DEPENDENCIES
93              
94             This module requires these other modules and libraries:
95              
96             FileHandle
97             POSIX
98             =cut
99              
100 1     1   935 use FileHandle;
  1         13667  
  1         6  
101 1     1   1685 use POSIX qw(strftime);
  1         7674  
  1         7  
102 1     1   1333 use Exporter;
  1         3  
  1         37  
103 1     1   167 use vars qw(@ISA @EXPORT @EXPORT_OK);
  1         2  
  1         110  
104             @ISA = qw(Exporter);
105              
106             =head2 EXPORTED FUNCTIONS
107              
108             See below for description and/or usage.
109              
110             =head3 &Log()
111              
112             =head3 &Fatal()
113              
114             =head3 &NOFILE &NOSTDERR
115              
116              
117              
118             =cut
119              
120             @EXPORT = qw(&Log &Fatal &NOFILE &NOSTDERR &STOP_LOG_STDERR &START_LOG_STDERR);
121              
122             @EXPORT_OK = qw ($Log_file $StartTime);
123              
124             =head2 OK to export variables:
125              
126             =head3 $Log_file
127              
128             Log file handler
129              
130             =head3 $StartTime
131              
132             Time-stamp of script start (same as used in the file name of Log file).
133              
134             =cut
135              
136 1     1   5 use vars qw(@ENV $logToSTDERR $StartTime $Log_file );
  1         3  
  1         575  
137              
138             $Log_file = new FileHandle;
139              
140             $logToSTDERR = 1; # set default value for $logToSTDERR
141             $StartTime = strftime "%y%m%d:%H:%M:%S", localtime;
142             chomp(my $MyName = `basename main::$0`);
143             $MyName =~ s/\.pl$//;
144             $ENV{'LOG_DIR'} = "/tmp" if $ENV{'LOG_DIR'} eq "" or ! defined $ENV{'LOG_DIR'};
145             my $LogFileName = sprintf "%s/%s.%s.%s.%s", $ENV{'LOG_DIR'}, $MyName,$StartTime, $$,"log";
146             die "Cannot open Log File $LogFileName: $!\n" unless $Log_file->open("> $LogFileName");
147             #
148             #
149             #
150             &Log (" *** " . $MyName . " *** starting *** ");
151             &Log ("Log file: $LogFileName");
152              
153             1;
154             # --------------------------------------------------------------------------------
155             =head2 FUNCTIONS
156             =head3 &Log ("log message")
157              
158             Prints message with time stamp to log file.
159              
160             If variable $logToSTDERR is not 0 than, message is also printed to
161             STDERR (this is default).
162              
163             =cut
164             # --------------------------------------------------------------------------------
165             sub Log {
166 4     4 1 7 my $msg = shift;
167 4         7 my $output = shift;
168 4 50       10 $output = 0 unless defined $output;
169 4         475 my $now = strftime "%y/%m/%d %H:%M:%S", localtime;
170 4 50       144 $Log_file->print(sprintf "%s: %s\n", $now, $msg) if $output != &NOFILE;
171 4 50 33     416 printf STDERR "%s: %s\n", $now, $msg if ( $logToSTDERR
      33        
172             and defined $output
173             and $output != &NOSTDERR);
174 4         10 return 0
175             };
176              
177              
178             # --------------------------------------------------------------------------------
179             =head3 &Fatal ("error message");
180              
181             Same as &Log(), dies with an error after printing log message. Internally calls &Log().
182              
183             =cut
184             # --------------------------------------------------------------------------------
185             sub Fatal ($) {
186 0     0 1   my $msg = shift;
187 0           &Log ("FATAL ERROR: " . $msg);
188 0           die "FATAL : ". $msg;
189             };
190              
191             # --------------------------------------------------------------------------------
192             =head3 NOFILE, NOSTDERR
193              
194             Can be passed as additional parameters to &Log(). Do not print log to
195             the corresponding media even if log level is high enough
196              
197             =cut
198             # --------------------------------------------------------------------------------
199             sub NOFILE () {1};
200             sub NOSTDERR () {2};
201              
202              
203             =head3 STOP_LOG_STDERR START_LOG_STDERR
204              
205             Disable/enable logging to STDERR. Both functions change value of
206             $logToSTDERR variable. At the module initialization it is set to 1,
207             i.e. enable printing log messages to STDERR.
208              
209             Please note that, very first message about start of main script will
210             always print to STDERR because it happens before you can call any of
211             these two functions.
212              
213             =cut
214 0     0 1   sub STOP_LOG_STDERR () {$logToSTDERR = 0};
215 0     0 1   sub START_LOG_STDERR () {$logToSTDERR = 1};
216              
217             # ============================================================
218             # close cleanly file on module exit
219             #
220             END {
221 1     1   7 &Log (" *** Closing file $LogFileName *** ");
222 1         6 &Log (" *** " . $MyName . " *** Completed *** ");
223 1         106 close $Log_file;
224             }
225             __END__