Subversion Repositories DevTools

Rev

Rev 1044 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1038 dpurdie 1
package Logger;
2
 
3
use Shell qw(mv);
4
 
5
$| = 1;
6
 
7
sub new ($$) {
8
	my ($class, $conf) = @_;
9
	return bless { conf => $conf }, $class;
10
}
11
 
12
sub logmsg ($$) {
13
	my ($self, $msg) = @_;
14
	my $conf = $self->{conf};
15
	my $logfile = $conf->{'logfile'};
16
 
17
	open my $fh, ">>$logfile" or die "Can't write blat logfile $logfile: $!\n";
18
	print $fh localtime()." (PID $$): $msg\n";
19
    close $fh;
20
 
21
    unless ( $self->{RollingLogs} )
22
    {
23
        #
24
        #   Is it time to roll the log file
25
        #
26
        my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
27
           $atime,$mtime,$ctime,$blksize,$blocks) = stat($logfile);
28
 
29
        if ( $size > $conf->{'logfile.size'} )
30
        {
31
            rotatelog($self);
32
        }
33
    }
34
 
35
    return undef;
36
}
37
 
38
sub err ($$) {
39
	my ($self, $msg) = @_;
40
	$self->logmsg("ERROR: $msg");
41
    $SIG{__DIE__} = undef;
42
	die "ERROR: $msg\n";
43
}
44
 
45
sub warn ($$) {
46
	my ($self, $msg) = @_;
47
	$self->logmsg("WARNING: $msg");
48
    return undef;
49
}
50
 
51
sub verbose ($$) {
52
    my $self = shift;
53
	my $conf = $self->{conf};
54
	$self->logmsg("(V): @_") if ( $conf->{verbose} > 0 );
55
    return undef;
56
}
57
 
58
sub verbose2 ($$) {
59
    my $self = shift;
60
	my $conf = $self->{conf};
61
	$self->logmsg("(V): @_") if ( $conf->{verbose} > 1 );
62
    return undef;
63
}
64
 
65
sub verbose3 ($$) {
66
    my $self = shift;
67
	my $conf = $self->{conf};
68
	$self->logmsg("(V): @_") if ( $conf->{verbose} > 2 );
69
    return undef;
70
}
71
 
72
 
73
sub rotatelog ($) {
74
	my $self = shift;
75
	my $conf = $self->{conf};
76
	my $logfile = $conf->{'logfile'};
77
    $self->{RollingLogs} = 1;
78
 
79
	$self->logmsg('Rotating logfile');
80
    my $num = $conf->{'logfile.count'};
81
    unlink "$logfile.$num" if ( -f "$logfile.$num");;
82
    for (; $num > 1 ; $num--)
83
    {
84
        my $prev = $num - 1;
85
        mv ("$logfile.$prev", "$logfile.$num") if ( -f "$logfile.$prev");
86
    }
87
    mv ($logfile, "$logfile.$num");
88
	$self->logmsg('Rotating logfile complete');
89
    $self->{RollingLogs} = 0;
90
    return undef;
91
}
92
 
93
1;