package VCP::Logger; =head1 NAME VCP::Logger - Update message, bug, and Log file management =head1 SYNOPSIS use VCP::Logger qw( shell_quote ); =head1 DESCRIPTION Does not throw exceptions or use the debug module, so this is safe to use with both. Load this as the very first module in your program. The log file name defaults to "vcp.log", set the environment variable VCPLOGFILE to change it. Here's how to do this in your program: BEGIN { $ENV{VCPLOGFILE} = "foo.bar" unless defined $ENV{VCPLOGFILE} || length $ENV{VCPLOGFILE}; } =cut @EXPORT_OK = qw( BUG lg lg_fh pr program_name log_file_name ); @ISA = qw( Exporter ); use Exporter; use strict ; use Carp; use File::Basename qw( basename ); use constant program_name => basename $0; use constant log_file_name => defined $ENV{VCPLOGFILE} && length $ENV{VCPLOGFILE} ? $ENV{VCPLOGFILE} : "vcp.log"; my $quiet_mode = 0; =head1 FUNCTIONS =over =item lg Prints a timestamped message to the log. Adds a trailing newline if need be. The first word of the message should not be capitalized unless it's a name or acronym; this makes grepping a bit easier (same for all error messages). "lg" is "log" abbreviated so as not to conflict with Perl's builtin log(). The timestamps are in integer seconds since this module was compiled unless you have Time::HiRes install in which case they are in floating point seconds. Should not throw an exception or alter $@ in the normal course of events (does not call any routines that should do so). =cut my $start_time; { my $s1; BEGIN { $s1 = program_name . ": " } sub _msg { my $msg = join "", map defined $_ ? $_ : "(((UNDEF)))", @_; 1 while chomp $msg; $msg =~ s/^$s1//o; ## TODO: go 'round and get rid of all the vcp: prefixes join $msg, $s1, "\n"; } my $log_failure_warned; sub _lg { print LOG ( sprintf( "%f ", gettimeofday() - $start_time ), @_ ) or $log_failure_warned++ or warn "$! writing ", program_name, " log file ", log_file_name, "\n"; } } sub lg { _lg &_msg; } =item lg_fh Returns a reference to the log filehandle (*LOG{IO}) so you can emit to the log directly. The log is flushed after every write, so this should be quite safe. =cut sub lg_fh { *LOG{IO} } =item pr Emit a status notification to STDERR (unless in quiet mode) and log it. =cut sub pr { my $msg = &_msg; print STDERR $msg unless $quiet_mode; _lg $msg; } BEGIN { open LOG, ">>" . log_file_name or die "$!: " . log_file_name . "\n"; ## Flush the LOG every print() so that we never miss data and ## so that we can pass the log to child processes to emit STDOUT ## and STDERR to. select LOG; $| = 1; select STDOUT; ## Print a header line guaranteed to start at the beginning of a ## line. print LOG "\n", "#" x 79, "\n"; eval <<'TIMEHIRES' or eval <<'GETTIMEOFDAY' or die "Problem loading Time::HiRes: $@"; use Time::HiRes qw( gettimeofday ); 1; TIMEHIRES sub gettimeofday { time } 1; GETTIMEOFDAY } BEGIN { $start_time = gettimeofday; lg "started"; } END { lg "ended"; } =item BUG Reports a bug using Carp::confess and logging the information. =cut sub BUG { print STDERR "***BUG REPORT***\n", @_, "\n"; print STDERR "Please see ", log_file_name, "\n"; print LOG "***BUG REPORT***\n", @_, "\n"; open STDOUT, ">&LOG" or warn "$! redirecting STDOUT to LOG\n"; open STDERR, ">&LOG" or warn "$! redirecting STDOUT to LOG\n"; system $^X, "-V" and warn "$! getting perl -V\n"; require Carp; eval { Carp::confess "stack trace" }; warn $@; exit 1; } =back =head1 COPYRIGHT Copyright 2000, Perforce Software, Inc. All Rights Reserved. This module and the VCP package are licensed according to the terms given in the file LICENSE accompanying this distribution, a copy of which is included in L<vcp>. =cut 1 ;
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#18 | 5403 | Barrie Slaymaker | - Misc logging, maintainability & debugging improvements | ||
#17 | 4491 | Barrie Slaymaker | - Another LOG filehandle select() fix from clkao | ||
#16 | 4480 | Barrie Slaymaker | - VCP::Logger does not assume STDOUT is selected (thanks, clkao) | ||
#15 | 4415 | Barrie Slaymaker | - "-quiet" does not suppress warning or debug messages | ||
#14 | 4404 | Barrie Slaymaker | - Added -q option (mostly to suppress banner & progress bars for test suite) | ||
#13 | 3990 | Barrie Slaymaker | - Progress bar now clicks over to next position at 10% intervals | ||
#12 | 3854 | Barrie Slaymaker | - VCP::Logger doesn't complain if pr_doing( ..., {}) is not called | ||
#11 | 3823 | Barrie Slaymaker | - Improved progress indicators. | ||
#10 | 3803 | Barrie Slaymaker | - Debugging output improved | ||
#9 | 3763 | Barrie Slaymaker |
- VCP::Source::cvs now explains what was ignored and what was scanned when it finds both foo,v and Attic/foo,v. |
||
#8 | 3706 | Barrie Slaymaker | - VCP gives some indication of output progress (need more) | ||
#7 | 3703 | Barrie Slaymaker |
- VCP::Logger emits newlines before a BUG REPORT to set set it off from progress bars |
||
#6 | 3678 | Barrie Slaymaker | - Progress bar now assumes 80 characters and fills to char 79 | ||
#5 | 3668 | Barrie Slaymaker |
- Progress bar can now be (nearly) fixed length when the total work to be done can be expressed as an integer. |
||
#4 | 3485 | Barrie Slaymaker | - BUG() now dumps %INC | ||
#3 | 3416 | Barrie Slaymaker | - Add progress indication routines to VCP::Logger | ||
#2 | 3167 | Barrie Slaymaker |
Add profiling report that details various chunks of time taken. |
||
#1 | 3155 | Barrie Slaymaker |
Convert to logging using VCP::Logger to reduce stdout/err spew. Simplify & speed up debugging quite a bit. Provide more verbose information in logs. Print to STDERR progress reports to keep users from wondering what's going on. Breaks test; halfway through upgrading run3() to an inline function for speed and for VCP specific features. |