| 1 | =pod | ||
| 2 | |||
| 3 | =begin classdoc | ||
| 4 | |||
| 5 | Interface specification for an apartment threaded Logger component. Opens the logfile. | ||
| 6 | Accepts log messages, prefixes them with | ||
| 7 | timestamps, and writes them to the logfile. Also inspects the size and | ||
| 8 | lifetime of the current logfile at regular intervals; when either size or | ||
| 9 | lifetime exceeds the defined maximums (or at the direction of an external | ||
| 10 | control command), closes and renames the current logfile, and then opens | ||
| 11 | a new logfile. | ||
| 12 | <p> | ||
| 13 | Classes which use a Logger object should inherit the HTTP::Daemon::Threaded::Logable | ||
| 14 | class. While this class provides a default implementation, applications should | ||
| 15 | implement their own subclasses, and create instances to be passed to HTTP::Daemon::Threaded | ||
| 16 | as either the EventLogger and/or WebLogger parameters. | ||
| 17 | <p> | ||
| 18 | Copyright© 2006-2008, Dean Arnold, Presicient Corp., USA<br> | ||
| 19 | All rights reserved. | ||
| 20 | <p> | ||
| 21 | Licensed under the Academic Free License version 3.0, as specified at | ||
| 22 | <a href='http://www.opensource.org/licenses/afl-3.0.php'>OpenSource.org</a>. | ||
| 23 | |||
| 24 | @author D. Arnold | ||
| 25 | @since 2005-08-21 | ||
| 26 | @self $self | ||
| 27 | |||
| 28 | |||
| 29 | |||
| 30 | =end classdoc | ||
| 31 | |||
| 32 | =cut | ||
| 33 | package HTTP::Daemon::Threaded::Logger; | ||
| 34 | |||
| 35 | use Thread::Apartment::Server; | ||
| 36 | use Time::Local; | ||
| 37 | |||
| 38 | use base qw(Thread::Apartment::Server); | ||
| 39 | |||
| 40 | use strict; | ||
| 41 | use warnings; | ||
| 42 | |||
| 43 | our $VERSION = '0.91'; | ||
| 44 | |||
| 45 | =pod | ||
| 46 | |||
| 47 | =begin classdoc | ||
| 48 | |||
| 49 | Constructor. Opens the logfile. If the logfile is too old or big, | ||
| 50 | it is truncated (i.e., closed, and renamed with the current timestamp | ||
| 51 | as its suffix, and a new file is created). An initial startup log | ||
| 52 | message is written. | ||
| 53 | |||
| 54 | @param AptTimeout maximum Thread::Apartment proxy method call timeout | ||
| 55 | @param Path pathname of logfile | ||
| 56 | @param MaxSize maximum size of logfile, in megabytes | ||
| 57 | @param Lifetime maximum lifetime of logfile, in hours | ||
| 58 | |||
| 59 | @return HTTP::Daemon::Threaded::Logger object | ||
| 60 | |||
| 61 | |||
| 62 | =end classdoc | ||
| 63 | |||
| 64 | =cut | ||
| 65 | sub new { | ||
| 66 | my ($class, %args) = @_; | ||
| 67 | # | ||
| 68 | # make sure we can open the logfile | ||
| 69 | # | ||
| 70 | my $self = { %args }; | ||
| 71 | bless $self, $class; | ||
| 72 | $self->set_client(delete $self->{AptTAC}) | ||
| 73 | if $self->{AptTAC}; | ||
| 74 | |||
| 75 | my $logfd; | ||
| 76 | $@ = "Can't open logfile: $!", | ||
| 77 | return undef | ||
| 78 | unless open($logfd, ">>$self->{Path}"); | ||
| 79 | |||
| 80 | my $old_fh = select($logfd); | ||
| 81 | $| = 1; | ||
| 82 | select($old_fh); | ||
| 83 | $self->{_fd} = $logfd; | ||
| 84 | # | ||
| 85 | # get current logfile size and last modify time | ||
| 86 | # if bigger than maxsize, or older than lifetime, truncate it | ||
| 87 | # | ||
| 88 | my @logstats = stat $logfd; | ||
| 89 | |||
| 90 | $self->{_logsize} = $logstats[7]; | ||
| 91 | $self->{_logtime} = $logstats[9]; | ||
| 92 | $self->truncate() | ||
| 93 | if (($args{MaxSize} && ($self->{_logsize} > $args{MaxSize})) || | ||
| 94 | ($args{Lifetime} && ((time() - $self->{_logtime}) > $args{Lifetime}))); | ||
| 95 | |||
| 96 | $self->log("**************************************"); | ||
| 97 | $self->log('Logger: HTTP::Daemon::Threaded started.'); | ||
| 98 | |||
| 99 | return $self; | ||
| 100 | } | ||
| 101 | =pod | ||
| 102 | |||
| 103 | =begin classdoc | ||
| 104 | |||
| 105 | Overrides Thread::Apartment::Server::get_simplex_methods() | ||
| 106 | |||
| 107 | @return hashref of simplex method names | ||
| 108 | |||
| 109 | |||
| 110 | =end classdoc | ||
| 111 | |||
| 112 | =cut | ||
| 113 | sub get_simplex_methods { | ||
| 114 | return { | ||
| 115 | 'log' => 1, | ||
| 116 | 'close' => 1, | ||
| 117 | 'truncate' => 1, | ||
| 118 | 'handleExpiration' => 1, | ||
| 119 | 'updateLifetime' => 1, | ||
| 120 | 'updateMaxSize' => 1 | ||
| 121 | }; | ||
| 122 | } | ||
| 123 | |||
| 124 | =pod | ||
| 125 | |||
| 126 | =begin classdoc | ||
| 127 | |||
| 128 | Append a log message to the logfile. The current timestamp | ||
| 129 | is prepended to the message before it is logged. | ||
| 130 | |||
| 131 | @simplex | ||
| 132 | @param $msg log message text | ||
| 133 | |||
| 134 | @return none | ||
| 135 | |||
| 136 | |||
| 137 | =end classdoc | ||
| 138 | |||
| 139 | =cut | ||
| 140 | sub log { | ||
| 141 | my ($self, $msg) = @_; | ||
| 142 | |||
| 143 | my $fd = $self->{_fd}; | ||
| 144 | return undef | ||
| 145 | unless $fd; | ||
| 146 | $msg = scalar localtime() . ": $msg\n"; | ||
| 147 | $self->{_logsize} += length($msg); | ||
| 148 | print $fd $msg; | ||
| 149 | return $self; | ||
| 150 | } | ||
| 151 | |||
| 152 | =pod | ||
| 153 | |||
| 154 | =begin classdoc | ||
| 155 | |||
| 156 | Close the logfile. | ||
| 157 | |||
| 158 | @simplex | ||
| 159 | @return none | ||
| 160 | |||
| 161 | |||
| 162 | =end classdoc | ||
| 163 | |||
| 164 | =cut | ||
| 165 | sub close { | ||
| 166 | my $self = shift; | ||
| 167 | my $fd = delete $self->{_fd}; | ||
| 168 | close($fd) | ||
| 169 | if $fd; | ||
| 170 | |||
| 171 | return $self; | ||
| 172 | } | ||
| 173 | |||
| 174 | =pod | ||
| 175 | |||
| 176 | =begin classdoc | ||
| 177 | |||
| 178 | Truncate the logfile. Close the current file, rename it | ||
| 179 | with the current timestamp appended to its name, and create | ||
| 180 | a new logfile. Writes an introductory message to the log. | ||
| 181 | |||
| 182 | @simplex | ||
| 183 | @param $newpath optional new logfile path | ||
| 184 | |||
| 185 | @return none | ||
| 186 | |||
| 187 | |||
| 188 | =end classdoc | ||
| 189 | |||
| 190 | =cut | ||
| 191 | sub truncate { | ||
| 192 | my ($self, $newpath) = @_; | ||
| 193 | # | ||
| 194 | # close existing file | ||
| 195 | # rename it with timestamp | ||
| 196 | # open new file | ||
| 197 | # reset size/time | ||
| 198 | # | ||
| 199 | $newpath = $self->{Path} | ||
| 200 | unless $newpath; | ||
| 201 | |||
| 202 | my $fd = delete $self->{_fd}; | ||
| 203 | CORE::close($fd) | ||
| 204 | if $fd; | ||
| 205 | |||
| 206 | my @ts = split(/\s+/, scalar localtime()); | ||
| 207 | $ts[3]=~tr/:/_/; | ||
| 208 | my $sfx = join('', '.', $ts[4], $ts[1], $ts[2], '_', $ts[3]); | ||
| 209 | print STDERR "HTTP::Daemon::Threaded::Logger::truncate: Can't rename logfile: $!" and | ||
| 210 | return undef | ||
| 211 | unless rename $self->{Path}, $self->{Path} . $sfx; | ||
| 212 | |||
| 213 | $self->{_logsize} = 0; | ||
| 214 | $self->{_logtime} = time(); | ||
| 215 | |||
| 216 | print STDERR "HTTP::Daemon::Threaded::Logger::truncate: Can't open logfile: $!" and | ||
| 217 | return undef | ||
| 218 | unless open($fd, ">$newpath"); | ||
| 219 | |||
| 220 | my $old_fh = select($fd); | ||
| 221 | $| = 1; | ||
| 222 | select($old_fh); | ||
| 223 | $self->{_fd} = $fd; | ||
| 224 | |||
| 225 | $self->log("**************************************"); | ||
| 226 | $self->log('Logger: Logfile truncated.'); | ||
| 227 | } | ||
| 228 | =pod | ||
| 229 | |||
| 230 | =begin classdoc | ||
| 231 | |||
| 232 | Update logfile lifetime. Called from WebClient. | ||
| 233 | |||
| 234 | @simplex | ||
| 235 | @param $lifetime lifetime in hours. | ||
| 236 | |||
| 237 | @return none | ||
| 238 | |||
| 239 | |||
| 240 | =end classdoc | ||
| 241 | |||
| 242 | =cut | ||
| 243 | sub updateLifetime { | ||
| 244 | $_[0]->{Lifetime} = $_[1]; | ||
| 245 | } | ||
| 246 | =pod | ||
| 247 | |||
| 248 | =begin classdoc | ||
| 249 | |||
| 250 | Update logfile maximum size. Called from WebClient. | ||
| 251 | |||
| 252 | @simplex | ||
| 253 | @param $maxsize Maximum size in megabytes. | ||
| 254 | |||
| 255 | @return none | ||
| 256 | |||
| 257 | |||
| 258 | =end classdoc | ||
| 259 | |||
| 260 | =cut | ||
| 261 | sub updateMaxSize { | ||
| 262 | $_[0]->{MaxSize} = $_[1]; | ||
| 263 | } | ||
| 264 | =pod | ||
| 265 | |||
| 266 | =begin classdoc | ||
| 267 | |||
| 268 | Update logfile name. Called from WebClient. | ||
| 269 | Truncates the existing logfile. | ||
| 270 | |||
| 271 | @simplex | ||
| 272 | @param $path new logfile name | ||
| 273 | |||
| 274 | @return none | ||
| 275 | |||
| 276 | |||
| 277 | =end classdoc | ||
| 278 | |||
| 279 | =cut | ||
| 280 | sub updatePath { | ||
| 281 | my ($self, $newpath) = @_; | ||
| 282 | |||
| 283 | $self->truncate($newpath); | ||
| 284 | $self->{Path} = $newpath; | ||
| 285 | } | ||
| 286 | |||
| 287 | 1; |