libtblog_simple.pm 5.68 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
#!/usr/bin/perl -w

#
# EMULAB-COPYRIGHT
# Copyright (c) 2005 University of Utah and the Flux Group.
# All rights reserved.
#

#
# libtblog-simple: Logging library for testbed
#
# This version ...
#

package libtblog_simple;

sub import {
    'libtblog'->export_to_level (1, @_);
}

package libtblog;
use Exporter;

@ISA = "Exporter";
@EXPORT = qw (tblog tberror tberr tbwarn tbwarning tbnotice tbinfo tbdebug 
	      tbdie
	      TBLOG_EMERG TBLOG_ALERT TBLOG_CRIT TBLOG_ERR 
	      TBLOG_WARNING TBLOG_NOTICE TBLOG_INFO TBLOG_DEBUG);

# After package decl.
# DO NOT USE "use English" in this module
use File::Basename;
use IO::Handle;
use Text::Wrap;
use Carp;

use strict;

use vars qw($SCRIPTNAME 
	    $EMERG $ALRET $CRIT $ERR $WARNING $NOTICE $INFO $DEBUG
	    %PRIORITY_MAP_TO_STR %PRIORITY_MAP_TO_NUM);

#
# Duplicate STDOUT and STDERR to SOUT and SERR respectfully, since
# tblog_capture() will redirect the real STDOUT and STDERR
#

open SOUT ,">&=STDOUT"; # Must be "&=" not "&" to avoid creating a
                        # new low level file descriper as this
                        # interacts strangly with the fork in swapexp.
autoflush SOUT 1;
open SERR ,">&=STDERR"; # Ditto
autoflush SERR 1;
						       
#
#
#

$SCRIPTNAME = basename($0);

#
# Make constants for the error level, the sub, prefixed with TBLOG_,
# are exported, the non-prefixed variables are used internally
#

sub TBLOG_EMERG   {000} $EMERG   = 000;
sub TBLOG_ALERT   {100} $ALRET   = 100;
sub TBLOG_CRIT    {200} $CRIT    = 200;
sub TBLOG_ERR     {300} $ERR     = 300;
sub TBLOG_WARNING {400} $WARNING = 400;
sub TBLOG_NOTICE  {500} $NOTICE  = 500;
sub TBLOG_INFO    {600} $INFO    = 600;
sub TBLOG_DEBUG   {700} $DEBUG   = 700;

%PRIORITY_MAP_TO_STR = (000 => 'EMERG',
	                100 => 'ALRET',
               	        200 => 'CRIT',
           	        300 => 'ERR',
			400 => 'WARNING',
			500 => 'NOTICE',
			600 => 'INFO',
			700 => 'DEBUG');
while (my ($n,$v) = each %PRIORITY_MAP_TO_STR) {
  $PRIORITY_MAP_TO_NUM{uc $v} = $n;
  $PRIORITY_MAP_TO_NUM{lc $v} = $n;
}

#
# Utility functions
#

sub if_defined ($$) {
    return $_[0] if defined $_[0];
    return $_[1] if defined $_[1];
    return '';
}

sub oneof ($@) {
    my ($to_find) = shift;
    my @res = grep {$to_find eq $_} @_;
    return @res > 0;
}

sub scriptname() {
  if_defined($SCRIPTNAME, $ENV{TBLOG_SCRIPTNAME});
}

#
# Dummy dblog, does nothing in this module
# Once the real "libtblog.pm" is used than this will be replaced
# with the real function which logs to the database
#
sub dblog_dummy( $$@ ) {
  return 1;
}
*dblog = \&dblog_dummy;

#
# tblog(priority, mesg, ...)
# tblog(priority, {parm=>value,...}, mesg, ...)
#
#   The main log function.  Logs a message to the database and print
#   the message to STDERR with an approate prefix depending on the
#   severity of the error.  If more than one string is given for the
#   message than they will concatenated.  If the env. var. TBLOG_OFF
#   is set to a true value than nothing will be written to the
#   database, but the message will still be written to STDOUT.  
#   Useful parms: sublevel, cause, type
#
sub tblog( $@ ) {
    my ($priority) = shift;
    my $parms = {};
    $parms = shift if ref $_[0] eq 'HASH';
    my $mesg = join('',@_);

    if (exists $PRIORITY_MAP_TO_STR{$priority}) {
      # $priority already a valid priority number
    } elsif (exists $PRIORITY_MAP_TO_NUM{$priority}) {
      # $priority a priority string, convert to num
      $priority = $PRIORITY_MAP_TO_NUM{$priority}
    } else {
      croak "Unknown priority \"$priority\" in call to tblog";
    }

    my $res = dblog($priority, $parms, $mesg) unless $mesg =~ /^\s+$/;
    
    print SERR format_message(scriptname(), $priority, $mesg);

    return $res;
}


# Useful alias functions

sub tberror( @ ) {&tblog($ERR, @_)}
sub tberr( @ ) {&tblog($ERR, @_)}
sub tbwarn( @ ) {&tblog($WARNING, @_)}
sub tbwarning( @ ) {&tblog($WARNING, @_)}
sub tbnotice( @ ) {&tblog($NOTICE, @_)}
sub tbinfo( @ ) {&tblog($INFO, @_)}
sub tbdebug( @ ) {&tblog($DEBUG, @_)}

#
# Log the message to the database as an error and than die.  An
# optional set of paramaters may be specified as the first paramater.
# Not exactly like die as the message bust be specified.
#
sub tbdie( @ ) {
    my $parms = {};
    $parms = shift if ref $_[0] eq 'HASH';
    my $mesg = join('',@_);

    dblog($ERR, $parms, $mesg);
    tblog_stop_capture() if exists $INC{'libtblog.pm'};
    die format_message(scriptname(), $ERR, $mesg);
}

#
# Format the message based on $priority
#
sub format_message ( $$$ ) {

    my ($scriptname, $priority, $mesg) = @_;

    $mesg =~ s/\s+$//;

    my $header;

    if ($mesg =~ /\s*\*\*\*/) {
        # do nothing
    } elsif ($priority <= $ERR ) {
        $header = "ERROR: $scriptname";
    } elsif ($priority == $WARNING) {
        $header = "WARNING: $scriptname";
    } elsif ($priority == $NOTICE) {
        $header = "$scriptname";
    }
    my $text;

    my @mesg = split /\n/, $mesg;
    if (@mesg == 1) {
        $mesg[0] =~ s/^\s+//;
        $mesg = $mesg[0];
    }
    if ($header) {
        my $line = "*** $header: $mesg[0]";
        if (@mesg > 1 || length($line) > $Text::Wrap::columns) {
            $line = "*** $header:\n";
            if (@mesg == 1) { # NOTE: $mesg[0] eq $mesg
                $mesg =~ s/^\s+//;
                $line .= wrap('***   ','***   ', $mesg, "\n");
            } else {
                foreach (@mesg) {
                    s/\s+$//;
                    $line .= "***   $_\n";
                }
            }
            return $line;
        } else {
            return "$line\n";
        }
    } else {
        if (@mesg == 1) {
            return wrap ('', '    ', $mesg, "\n");
        } else {
            return "$mesg\n";
        }
    }
}