| 1 | # ===================================================================== | ||
| 2 | # Copyright (c) 2002,2003 IBM Corporation | ||
| 3 | # All rights reserved. This program and the accompanying materials | ||
| 4 | # are made available under the terms of the Common Public License v1.0 | ||
| 5 | # which accompanies this distribution, and is available at | ||
| 6 | # http://www.opensource.org/licenses/cpl.php | ||
| 7 | # | ||
| 8 | # ===================================================================== | ||
| 9 | package LS::Cache::Logger; | ||
| 10 | |||
| 11 | use strict; | ||
| 12 | use warnings; | ||
| 13 | |||
| 14 | use Fcntl ':flock'; | ||
| 15 | use Carp qw(:DEFAULT); | ||
| 16 | |||
| 17 | sub new { | ||
| 18 | |||
| 19 | my ($self, %params) = @_; | ||
| 20 | |||
| 21 | unless (ref $self) { | ||
| 22 | |||
| 23 | $self = bless { | ||
| 24 | |||
| 25 | 'log-file'=> $params{'log-file'}, | ||
| 26 | }, $self; | ||
| 27 | } | ||
| 28 | |||
| 29 | return $self; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub log_message { | ||
| 33 | |||
| 34 | my ($self, $message) = @_; | ||
| 35 | |||
| 36 | my $filename = ">>" . $self->{'log-file'}; | ||
| 37 | |||
| 38 | unless(open(LOG_FILE, $filename)) { | ||
| 39 | |||
| 40 | print STDERR Carp::longmess("Unable to open cache log file: $filename"); | ||
| 41 | |||
| 42 | return undef; | ||
| 43 | } | ||
| 44 | |||
| 45 | flock(LOG_FILE, LOCK_EX); | ||
| 46 | |||
| 47 | my $now_string = localtime; | ||
| 48 | |||
| 49 | print LOG_FILE "[ $now_string ] $message\n"; | ||
| 50 | |||
| 51 | flock(LOG_FILE, LOCK_UN); | ||
| 52 | |||
| 53 | close(LOG_FILE); | ||
| 54 | |||
| 55 | return 1; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub clear_log { | ||
| 59 | |||
| 60 | my $self = shift; | ||
| 61 | |||
| 62 | unless(unlink($self->{'log-file'})) { | ||
| 63 | |||
| 64 | print STDERR Carp::longmess("Unable to clear log file"); | ||
| 65 | return undef; | ||
| 66 | } | ||
| 67 | |||
| 68 | return 1; | ||
| 69 | } | ||
| 70 | |||
| 71 | 1; | ||
| 72 | |||
| 73 | __END__ |