dlloglib / dlloglib.pl

#!/usr/bin/perl
#
# $Id: dlloglib.pl,v 0.0.3 2007/10/28 19:54:01 overlord Exp $
#
# Add a require into your Perl scripts, making sure that the directory this is
# stored in is in @INC and you will be able to use the routines defined here-in.
#

use constant DLLOGLIB_INFO => 0;
use constant DLLOGLIB_NOTICE => 1;
use constant DLLOGLIB_WARNING => 2;
use constant DLLOGLIB_ERROR => 3;
use constant DLLOGLIB_CRITICAL => 4;

my $dlloglib_log_open = 0;

# ---------------------------------------------------------------------------
# | dlloglib_Log($$)                                                        |
# | Logs to a file, previously opened by OpenLog(), or STDERR,              |
# | if the severity is DLLOGLIB_CRITICAL, the message is printed to STDOUT  |
# | as well.                                                                |
# |                                                                         |
# | Parameters: [0] Severity                                                |
# |             [1] Message                                                 |
# ---------------------------------------------------------------------------
sub dlloglib_Log($$);

# ---------------------------------------------------------------------------
# | dlloglib_OpenLog($)                                                     |
# | Opens a log file with a given name and maintains a handle to writing    |
# | to it via Log().                                                        |
# |                                                                         |
# | Parameters: [0] FileName                                                |
# ---------------------------------------------------------------------------
sub dlloglib_OpenLog($);

# ---------------------------------------------------------------------------
# | dlloglib_CloseLog()                                                     |
# | Closes a log file, previously opened with OpenLog().                    |
# |                                                                         |
# | Parameters: None                                                        |
# ---------------------------------------------------------------------------
sub dlloglib_CloseLog();

# ---------------------------------------------------------------------------
# | dlloglib_SeverityConvert()                                              |
# | Converts a severity mnemonic to an integer.                             |
# |                                                                         |
# | Parameters: [0] Mnemonic                                                |
# ---------------------------------------------------------------------------
sub dlloglib_SeverityConvert($);

#----------------------------------------------------------------------------
sub dlloglib_Log($$)
{
  my ( $severity, $msg ) = @_;
  my $hdle;
  my $dstamp;
  my $sevstr;
  my $str;
  my @severities = (
    'INFO',
    'NOTICE',
    'WARNING',
    'ERROR',
    'CRITICAL'
  );

  if ( $dlloglib_log_open ) {
    $hdle = DLLOGLIB_LOG_HANDLE;
  } else {
    $hdle = STDERR;
  }
  $dstamp = localtime(time());
  $severity = dlloglib_SeverityConvert($severity);
  $severity = int($severity);
  if ( $severity < 0 || $severity >= scalar(@severities) ) {
    $sevstr = 'UNKNOWN';
  } else {
    $sevstr = $severities[$severity];
  }

  $str = "$dstamp [$sevstr]: $msg\n";
  print $hdle $str;
  print $str if ( $severity == DLLOGLIB_CRITICAL );
  return 0;
}
#----------------------------------------------------------------------------
sub dlloglib_OpenLog($)
{
  my $logfile = $_[0];
  my $success = 0;
  if ( $dlloglib_log_open == 0 && $logfile ) {
    if ( open(DLLOGLIB_LOG_HANDLE, ">>$logfile") ) {
      $success = 1;
      $dlloglib_log_open = 1;
    }
  }
  return $success;
}
#----------------------------------------------------------------------------
sub dlloglib_CloseLog()
{
  if ( $dlloglib_log_open == 1 ) {
    close(DLLOGLIB_LOG_HANDLE);
    $dlloglib_log_open = 0;
  }
  return;
}
#----------------------------------------------------------------------------
sub dlloglib_SeverityConvert($)
{
  my $level = $_[0];

  return DLLOGLIB_INFO if ( $level eq 'DLLOGLIB_INFO' );
  return DLLOGLIB_NOTICE if ( $level eq 'DLLOGLIB_NOTICE' );
  return DLLOGLIB_WARNING if ( $level eq 'DLLOGLIB_WARNING' );
  return DLLOGLIB_ERROR if ( $level eq 'DLLOGLIB_ERROR' );
  return DLLOGLIB_CRITICAL if ( $level eq 'DLLOGLIB_CRITICAL' );

  return $level;
}
#----------------------------------------------------------------------------
# Please leave this at the end of the file
1;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.