Commits

Palmer, 2E0EOL committed e1285ff

Daybo Logic standard logging library

Comments (0)

Files changed (1)

+#!/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;
+