Subject: | Patch - add noTimestamp option to CGI::Carp |
Sometimes it is useful for CGI::Carp not to add a timestamp to error
messages. The web server may add its own timestamp anyway, and you may
prefer that any error output from the script should be identical from
one run to the next. This patch adds a new 'noTimestamp' option or
$NO_TIMESTAMP package variable. Please could you consider this feature
for inclusion in the next version?
Subject: | CGI.pm-3.63-noTimestamp.diff |
diff -ru Changes Changes
--- Changes 2012-11-14 23:43:13.000000000 +0000
+++ Changes 2013-01-02 13:47:52.543627766 +0000
@@ -1,3 +1,9 @@
+Version 3.64? Jan 2, 2013
+
+ [NEW FEATURES]
+ - If you specify the new option noTimestamp, CGI::Carp no longer adds a timestamp
+ to each line of error messages.
+
Version 3.63 Nov 12, 2012
[SECURITY]
diff -ru lib/CGI/Carp.pm lib/CGI/Carp.pm
--- lib/CGI/Carp.pm 2012-11-03 00:33:07.000000000 +0000
+++ lib/CGI/Carp.pm 2013-01-02 13:52:37.480195214 +0000
@@ -265,9 +265,24 @@
Note that this override doesn't happen until after the program has
compiled, so any compile-time errors will still show up with the
non-overridden program name
-
+
+=head1 TURNING OFF TIMESTAMPS
+
+If your web server automatically adds a timestamp to each log line,
+you may not need CGI::Carp to add its own. You can disable timestamping
+by importing "noTimestamp":
+
+ use CGI::Carp qw(noTimestamp);
+
+Alternatively you can set C<$CGI::Carp::NO_TIMESTAMP> to 1.
+
+Note that the name of the program is still automatically included in
+the message.
+
=head1 CHANGE LOG
+3.54? Added noTimestamp option
+
3.51 Added $CGI::Carp::TO_BROWSER
1.29 Patch from Peter Whaite to fix the unfixable problem of CGI::Carp
@@ -340,7 +355,7 @@
@ISA = qw(Exporter);
@EXPORT = qw(confess croak carp);
-@EXPORT_OK = qw(carpout fatalsToBrowser warningsToBrowser wrap set_message set_die_handler set_progname cluck ^name= die);
+@EXPORT_OK = qw(carpout fatalsToBrowser warningsToBrowser wrap noTimestamp set_message set_die_handler set_progname cluck ^name= die);
$main::SIG{__WARN__}=\&CGI::Carp::warn;
@@ -348,7 +363,7 @@
$CGI::Carp::CUSTOM_MSG = undef;
$CGI::Carp::DIE_HANDLER = undef;
$CGI::Carp::TO_BROWSER = 1;
-
+$CGI::Carp::NO_TIMESTAMP = 0;
# fancy import routine detects and handles 'errorWrap' specially.
sub import {
@@ -370,6 +385,7 @@
Exporter::import($pkg,keys %routines);
$Exporter::ExportLevel = $oldlevel;
$main::SIG{__DIE__} =\&CGI::Carp::die if $routines{'fatalsToBrowser'};
+ $CGI::Carp::NO_TIMESTAMP = 1 if $routines{'noTimestamp'};
# $pkg->export('CORE::GLOBAL','die');
}
@@ -385,7 +401,6 @@
}
sub stamp {
- my $time = scalar(localtime);
my $frame = 0;
my ($id,$pack,$file,$dev,$dirs);
if (defined($CGI::Carp::PROGNAME)) {
@@ -397,6 +412,8 @@
} until !$file;
}
($dev,$dirs,$id) = File::Spec->splitpath($id);
+ return "$id: " if $CGI::Carp::NO_TIMESTAMP;
+ my $time = scalar(localtime);
return "[$time] $id: ";
}
diff -ru t/carp.t t/carp.t
--- t/carp.t 2011-01-24 03:20:03.000000000 +0000
+++ t/carp.t 2013-01-02 13:57:27.941730756 +0000
@@ -3,7 +3,7 @@
use strict;
-use Test::More tests => 61;
+use Test::More tests => 62;
use IO::Handle;
use CGI::Carp;
@@ -85,6 +85,15 @@
"/] $id: There is a problem at $q_file line $expect_l.".'$/',
"CGI::Carp::warn builds correct message");
+# Test $NO_TIMESTAMP
+{
+ local $CGI::Carp::NO_TIMESTAMP = 1;
+ $expect_l = __LINE__ + 1;
+ like(CGI::Carp::warn("There is a problem"),
+ qr/\A\Q$id: There is a problem at $file line $expect_l.\E\s*\z/,
+ "noTimestamp");
+}
+
# Test that _warn is called at the correct time
$CGI::Carp::WARN = 1;