| 1 | package RWDE::Logger; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Error qw(:try); | ||
| 7 | use Sys::Syslog qw(:standard :extended :macros); | ||
| 8 | |||
| 9 | use RWDE::Configuration; | ||
| 10 | use RWDE::Exceptions; | ||
| 11 | |||
| 12 | our ($debug, $syslog_socket); | ||
| 13 | |||
| 14 | use vars qw($VERSION); | ||
| 15 | $VERSION = sprintf "%d", q$Revision: 507 $ =~ /(\d+)/; | ||
| 16 | |||
| 17 | =pod | ||
| 18 | |||
| 19 | =head1 RWDE::Logger | ||
| 20 | |||
| 21 | Provides methods to log via syslog | ||
| 22 | |||
| 23 | =cut | ||
| 24 | |||
| 25 | =head2 set_debug() | ||
| 26 | |||
| 27 | Method to enable debug mode | ||
| 28 | |||
| 29 | =cut | ||
| 30 | |||
| 31 | sub set_debug { | ||
| 32 | $debug = 1; | ||
| 33 | return; | ||
| 34 | } | ||
| 35 | |||
| 36 | =head2 toggle_debug() | ||
| 37 | |||
| 38 | Method to toggle debug mode | ||
| 39 | |||
| 40 | =cut | ||
| 41 | |||
| 42 | sub toggle_debug { | ||
| 43 | $debug = ($debug ? 0 : 1); | ||
| 44 | return; | ||
| 45 | } | ||
| 46 | |||
| 47 | =head2 is_debug() | ||
| 48 | |||
| 49 | Determine if debug mode is currently set | ||
| 50 | |||
| 51 | =cut | ||
| 52 | |||
| 53 | sub is_debug { | ||
| 54 | return $debug; | ||
| 55 | } | ||
| 56 | |||
| 57 | =head2 _init_syslog() | ||
| 58 | |||
| 59 | Private Method | ||
| 60 | |||
| 61 | Open the syslog connection defined within the Configuration file | ||
| 62 | |||
| 63 | =cut | ||
| 64 | |||
| 65 | sub _init_syslog { | ||
| 66 | |||
| 67 | # open syslog connection | ||
| 68 | my $result = setlogsock 'unix'; | ||
| 69 | |||
| 70 | if (not defined $result) { | ||
| 71 | throw RWDE::DevelException({ info => 'Could not connect to syslog facility' }); | ||
| 72 | } | ||
| 73 | |||
| 74 | $syslog_socket = $result; | ||
| 75 | |||
| 76 | my $log_filename = lc(RWDE::Configuration->ServiceName); | ||
| 77 | openlog($log_filename, 'cons,pid', LOG_LOCAL0); | ||
| 78 | |||
| 79 | return (); | ||
| 80 | } | ||
| 81 | |||
| 82 | =head2 syslog_msg() | ||
| 83 | |||
| 84 | Log a message to syslog via the established syslog connection | ||
| 85 | |||
| 86 | A type and info are required | ||
| 87 | |||
| 88 | type is one of debug, info, notice, warning, err, crit, alert, emerg | ||
| 89 | info is the desired log message | ||
| 90 | |||
| 91 | =cut | ||
| 92 | |||
| 93 | sub syslog_msg { | ||
| 94 | my ($self, $type, $info) = @_; | ||
| 95 | |||
| 96 | if (!($syslog_socket)) { | ||
| 97 | _init_syslog(); | ||
| 98 | } | ||
| 99 | |||
| 100 | my ($package, $filename, $line, $subroutine, $hasargs, $wantarray, $evaltext, $is_require, $hints, $bitmask) = caller(1); | ||
| 101 | |||
| 102 | my %valid_level = ( | ||
| 103 | debug => 1, | ||
| 104 | info => 1, | ||
| 105 | notice => 1, | ||
| 106 | warning => 1, | ||
| 107 | err => 1, | ||
| 108 | crit => 1, | ||
| 109 | alert => 1, | ||
| 110 | emerg => 1 | ||
| 111 | ); | ||
| 112 | |||
| 113 | if (not defined $valid_level{$type}) { | ||
| 114 | $type = 'info'; | ||
| 115 | } | ||
| 116 | |||
| 117 | if (not defined $info) { | ||
| 118 | my ($package, $filename, $line) = caller(1); | ||
| 119 | $info = "No message sent to syslog from $filename Line: $line!"; | ||
| 120 | } | ||
| 121 | |||
| 122 | if (defined($package) && defined($subroutine)) { | ||
| 123 | $info = "$package=>$subroutine ($info)"; | ||
| 124 | } | ||
| 125 | |||
| 126 | syslog($type, '%s', $info); | ||
| 127 | |||
| 128 | debug_info($type, $info); | ||
| 129 | |||
| 130 | return; | ||
| 131 | } | ||
| 132 | |||
| 133 | =head2 debug_info() | ||
| 134 | |||
| 135 | Take the type and the human readable message and print it to STDERR if debug is on | ||
| 136 | |||
| 137 | =cut | ||
| 138 | |||
| 139 | sub debug_info { | ||
| 140 | my ($type, $info) = @_; | ||
| 141 | |||
| 142 | if ($debug) { | ||
| 143 | my $d = scalar localtime; | ||
| 144 | print "$d: $type -- $info\n"; | ||
| 145 | } | ||
| 146 | |||
| 147 | return (); | ||
| 148 | } | ||
| 149 | |||
| 150 | 1; |