"Fossies" - the Fresh Open Source Software Archive

Member "absence-v2.1/cgi-bin/AbsenceLog.pm" (20 Oct 2013, 3876 Bytes) of package /linux/www/web-absence-2.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "AbsenceLog.pm" see the Fossies "Dox" file reference documentation.

    1 #======================================================================
    2 #    This file is part of Absence.
    3 #
    4 #    Absence is free software: you can redistribute it and/or modify
    5 #    it under the terms of the GNU General Public License as published by
    6 #    the Free Software Foundation, either version 3 of the License, or
    7 #    (at your option) any later version.
    8 #
    9 #    Absence is distributed in the hope that it will be useful,
   10 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
   11 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12 #    GNU General Public License for more details.
   13 #
   14 #    You should have received a copy of the GNU General Public License
   15 #    along with Absence.  If not, see <http://www.gnu.org/licenses/>.
   16 #======================================================================
   17 
   18 # copyright Robert Urban
   19 
   20 package AbsenceLog;
   21 
   22 use Exporter;
   23 
   24 @ISA = qw(Exporter);
   25 @EXPORT = qw(abslog);
   26 
   27 use Fcntl (':flock'); # import LOCK_* constants
   28 use FileHandle;
   29 
   30 use AbsenceConfig;
   31 
   32 our $LOGFILE;
   33 my $VERSION = '2.0.1';
   34 
   35 my $AUTH    = AbsenceConfig::fetch('authentication');
   36 my $IPADDR  = $ENV{'REMOTE_ADDR'};
   37 my $AUTH_USER;
   38 my $LOG_METHOD = '';
   39 
   40 sub SEEK_END() { 2; }   # not portable, but what the hell
   41 
   42 sub import
   43 {
   44     shift;  # get rid of $class
   45 
   46     #print "IMPORT...\n";
   47     my $word;
   48     my @args;
   49     while ($word = shift) {
   50         #print "word=[$word]\n";
   51         if ($word eq 'LOGFILE') {
   52             #print "parm = [$_[0]], LOGFILE=[$LOGFILE]\n";
   53             $LOGFILE = shift unless (defined($LOGFILE));
   54             #print "LOGFILE = [$LOGFILE]\n";
   55         } elsif ($word eq 'LOG_STDOUT') {
   56             $LOG_METHOD = 'stdout';
   57         } else {
   58             push(@args, $word);
   59         }
   60     }
   61     AbsenceLog->export_to_level(1, @args);
   62 
   63     if (!defined($LOGFILE)) {
   64         if (exists($ENV{ABSENCE_TEST})) {
   65             $LOGFILE    = '/tmp/absence.log';
   66         } else {
   67             #print "getting logfile path from AbsenceConfig...\n";
   68             $LOGFILE    = AbsenceConfig::fetch('log_file') ||
   69                 die "no logfile configured.\n";
   70         }
   71     }
   72 }
   73 
   74 sub setAuthUser
   75 {
   76     $AUTH_USER = ' '.shift;
   77 }
   78 
   79 sub abslog
   80 {
   81     my $do_ts = 1;
   82     if ($_[0] eq '-nots') {
   83         $do_ts = 0;
   84         shift;
   85     }
   86         
   87     my $msg = shift;
   88 
   89     my $ts = timestamp();
   90 
   91     my $openstr = "+<$LOGFILE";
   92 
   93     if (! -e $LOGFILE) {
   94         $openstr = ">$LOGFILE";
   95     }
   96 
   97     if ($LOG_METHOD eq 'stdout') {
   98         print "LOG: $msg\n";
   99         return;
  100     }
  101 
  102     my $fh = FileHandle->new($openstr);
  103     defined($fh) || die "open of [$LOGFILE] failed";
  104     flock($fh, LOCK_EX) || die "flock lock_ex on $LOGFILE ($!)";
  105     # position file-pointer at EOF
  106     seek($fh, 0, SEEK_END);
  107     if (ref($msg)) {
  108         print $fh "$ts $IPADDR$AUTH_USER\n";
  109         foreach (@$msg) {
  110             print $fh "\t$_\n";
  111         }
  112     } else {
  113         if ($msg =~ /^debug/i) {
  114             print $fh "$msg\n";
  115         } elsif (!$do_ts) {
  116             print $fh "$msg\n";
  117         } else {
  118             print $fh "$ts $IPADDR$AUTH_USER $msg\n";
  119         }
  120     }
  121     flock($fh, LOCK_UN) || die "flock lock_un on $LOGFILE ($!)";
  122     $fh->close;
  123 }
  124 
  125 sub timestamp
  126 {
  127     my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
  128     my ($sec,$min,$hour,$mday,$mon,$year,@junk) = localtime(time);
  129 
  130     return sprintf("%d-%s-%d %02d:%02d:%02d",
  131         $mday, $months[$mon], $year+1900, $hour, $min, $sec);
  132 }
  133 
  134 1;
  135 
  136 __END__
  137 
  138 =head1 NAME
  139 
  140 AbsenceLog.pm - primitive logging facility for absence
  141 
  142 =head1 SYNOPSIS
  143 
  144   use AbsenceLog;
  145 
  146   abslog('text to be logged, not CR-terminated');
  147 
  148 =head1 DESCRIPTION
  149 
  150 AbsenceLog.pm provides a simple logging facility.  Messages are
  151 written to the logfile specified in by 'log_file' in the configuration.
  152 There is no provision for not having a logfile, i.e., a C<use AbsenceLog>
  153 will fail if 'log_file' is not configured.  The log file must also exist.
  154 
  155 =head1 FUNCTIONS
  156 
  157 =over 4
  158 
  159 =item abslog()
  160 
  161 Takes a single parameter consisting of text to be logged.  The text
  162 should not be carriage-return terminated (that is done automatically).
  163 B<abslog> is exported by default for convenience.
  164 
  165 =back
  166 
  167 =head1 AUTHOR
  168 
  169 Robert Urban <urban@tru64.org>
  170 
  171 Copyright (C) Robert Urban 2003
  172 
  173 =cut