[go: up one dir, main page]

Menu

[r1]: / errorlog.pm  Maximize  Restore  History

Download this file

156 lines (134 with data), 4.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package ErrorLog;
use strict;
use warnings;
use Time::Local;
use File::Copy;
# General Purpose Error Logging Module
# VERSION: 0.1; see version notes at bottom of file.
##################################################
## the object constructor (simplistic version) ##
##################################################
sub new {
my $self = {};
$self->{log_errors} = undef;
$self->{log_file} = undef;
$self->{loghandle} = undef;
$self->{error_file} = undef;
$self->{errorhandle} = undef;
$self->{std_out} = undef;
$self->{browser_out} = undef;
$self->{file_out} = undef;
$self->{backupage} = 1;
$self->{maxageinseconds} = 604800;
bless($self);
return $self;
}
sub startLogging {
my $self = shift;
$self->{log_errors} = 1;
}
sub stopLogging {
my $self = shift;
$self->{log_errors} = 0;
}
sub createLog {
# get parameters
my $self = shift;
my $enable = $_[0];
my $logfile = $_[1];
my $errorfile = $_[2];
my $outputtypes = $_[3];
# init error log values
$self->{log_errors} = $enable;
$self->{log_file} = $logfile;
$self->{error_file} = $errorfile;
if ($outputtypes =~ /s/) { $self->{std_out} = 1; } else {$self->{std_out} = 0; }
if ($outputtypes =~ /b/) { $self->{browser_out} = 1; } else {$self->{browser_out} = 0; }
if ($outputtypes =~ /f/) { $self->{file_out} = 1; } else {$self->{file_out} = 0; }
my $error;
# does log already exist?
my $exists = 0;
if ( (-e $logfile) ) { $exists = 1; }
#print "error log [" . $logfile . "] exists: $exists\n";
# check if old log should be moved to backup
# if ($exists) {
# # read the first line of the file (create date)
# open $self->{loghandle}, "<$logfile" or $error = "couldn't open $logfile: $!";
# my $fh = $self->{loghandle};
# my $timestamp = <$fh>;
# close($fh);
# hack for some case where timestamp is empty:
# unless (defined($timestamp)) {$timestamp = "20-2-2008 14:44:58"}
# check the age of the file
#print "logfile: $logfile\n";
#print "timestamp: $timestamp\n";
# chomp $timestamp;
# $timestamp =~ /(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)/;
# my $epochageinseconds = timelocal($6, $5, $4, $1, $2-1, $3-1900);
# my $ageinseconds = time - $epochageinseconds;
#print "check if $ageinseconds is greater than " . $self->{maxageinseconds} . "\n";
# if ($ageinseconds > $self->{maxageinseconds}) {
# # back up current log
# copy($logfile, "$logfile.bak");
# unlink $logfile;
# $exists = 0; # need to start a new log file
## }
# }
# if using a log file, open/create it and write out the time
if ($self->{file_out} == 1 && $enable == 1) {
# get the current time
my ($sec, $min, $hours, $mday, $month, $year) = localtime;
# open/create log file and print the date and time
open $self->{loghandle}, ">>$logfile" or $error = "couldn't open $logfile: $!";
#if ($error) { print "error: $error\n"; }
if ($error && length($error) > 0) {
# can't use log file; shut it down to prevent error messages
$self->{file_out} = 0;
} else {
my $fh = $self->{loghandle};
if ($exists) {
print $fh "\n\n$mday-".($month+1)."-".($year+1900)." $hours:$min:$sec\n";
} else {
# timestamp should be the first line of a new logfile.
print $fh "$mday-".($month+1)."-".($year+1900)." $hours:$min:$sec\n";
}
}
}
if ($self->{browser_out} == 1) {
print "Content-Type: text/html\n\n";
}
}
sub logMessage {
my $self = shift;
if ($self->{log_errors} == 1) {
my $message = $_[0];
my $logfile = $self->{log_file};
if ($self->{file_out} == 1) {
my $fh = $self->{loghandle};
print $fh $message;
}
if ($self->{browser_out} == 1) {
$message =~ s/\n/<br>/g;
print $message;
}
if ($self->{std_out} == 1) {
print $message;
}
}
}
sub reportError {
my $self = shift;
my $message = $_[0];
if (!$self->{errorhandle}) {
# get the current time
my ($sec, $min, $hours, $mday, $month, $year) = localtime;
# open/create error log file and print the date and time
my $error;
open $self->{errorhandle}, ">>$self->{error_file}" or $error = "couldn't open $self->{error_file}: $!";
my $fh = $self->{errorhandle};
print $fh "\n\n$mday-".($month+1)."-".($year+1900)." $hours:$min:$sec\n";
}
my $fh = $self->{errorhandle};
print $fh $message;
}
1; # so that the require or use succeeds