libtblog_simple.pm 7.44 KB
Newer Older
1 2 3
#!/usr/bin/perl -w

#
4
# Copyright (c) 2005-2012 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#

#
# 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 
43
	      tbdie tblog_session 
44
	      TBLOG_EMERG TBLOG_ALERT TBLOG_CRIT TBLOG_ERR 
45 46 47
	      TBLOG_WARNING TBLOG_NOTICE TBLOG_INFO TBLOG_DEBUG
	      SEV_DEBUG SEV_NOTICE SEV_WARNING SEV_SECONDARY
	      SEV_ERROR SEV_ADDITIONAL SEV_IMMEDIATE);
48 49 50 51 52 53 54 55 56 57 58 59

# 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
60 61
	    %PRIORITY_MAP_TO_STR %PRIORITY_MAP_TO_NUM
	    *SOUT *SERR);
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

#
# 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;
}

108 109 110 111
#
# tbreport related constants
#

112 113 114 115 116 117 118
use constant SEV_DEBUG      =>   0;
use constant SEV_NOTICE     => 100;
use constant SEV_WARNING    => 300;
use constant SEV_SECONDARY  => 400;
use constant SEV_ERROR      => 500; # Threshold
use constant SEV_ADDITIONAL => 600;
use constant SEV_IMMEDIATE  => 900;
119

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
#
# 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});
}

140 141 142 143 144 145 146 147
#
#
#

sub tblog_session() {
    return $ENV{TBLOG_SESSION};
}

148

149 150 151 152 153 154 155 156 157 158
#
# 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;

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
#
# 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+$/;
    
189 190 191 192
    # XXX argh! Keep perl from complaining about unused SOUT var
    if (0) {
	print SOUT format_message(scriptname(), $priority, $mesg);
    }
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
    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('',@_);

219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
    if ($^S) {
	# Handle case when we are in an eval block special:
	#   1) downgrade error to warning as it may not be fatal
	#   2) Call tblog (not just dblog) to print the error since 
        #      the message may never actually appear. 
	#   3) Don't stop capturing as we are not trully dying
	#   4) Don't format message as it may be modified latter
	tblog($WARNING, $parms, $mesg);
	$mesg .= "\n" unless $mesg =~ /\n$/;
	die $mesg;
    } else {
	dblog($ERR, $parms, $mesg);
	tblog_stop_capture() if exists $INC{'libtblog.pm'};
	die format_message(scriptname(), $ERR, $mesg);
    }
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
}

#
# 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";
        }
    }
}