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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
|
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
log_file_name
pr
pr_active
pr_did
pr_doing
pr_done
pr_done_failed
program_name
set_quiet_mode
start_time
);
@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;
## We "gracefully" degrade to 1 second resolution if no Time::HiRes.
BEGIN { eval "use Time::HiRes qw( time )" }
BEGIN {
$start_time = 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 ", time - $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
Print a status notification to STDERR (unless in quiet mode) and log it.
=cut
my $doing;
my %did;
my @did_keys;
my $last_progress;
my $need_progress_prompt;
my $expect;
my $count;
my $spinner_pos;
my $last_char_count;
my $fmt;
my $bar_width = 10;
my @spinner = qw( - \ | / - \ | / );
sub _reset_progress {
$expect = 0;
$count = 0;
$last_char_count = 0;
$spinner_pos = 0;
$last_progress = "";
$need_progress_prompt = 0;
@did_keys = ();
%did = ();
}
sub _start_progress {
$need_progress_prompt = 1;
if ( ! $expect || $expect < 0 ) {
$expect = 0;
}
else {
my $l = length $expect;
$fmt = "[%-${bar_width}s] %${l}d/$expect";
}
}
sub _char_count {
my ( $count ) = @_;
return $bar_width if $count >= $expect;
return int( $bar_width * $count / $expect );
## Do not ever return a full bar in mid-process
}
sub _interrupt_progress {
return unless defined $doing && ! $need_progress_prompt;
print STDERR "\n";
$last_progress = "";
$need_progress_prompt = 1;
}
sub _show_progress {
my $progress;
if ( $expect ) {
my $char_count = _char_count $count;
my $chars = join "",
"#" x $char_count,
$char_count < $bar_width
? $spinner[ $spinner_pos++ & 0x07 ]
: ();
$progress = sprintf $fmt, $chars, $count;
$last_char_count = $char_count;
}
else {
$progress = $spinner[ $spinner_pos++ & 0x07 ] . " " . $count;
}
$progress .= join "", map " $_ $did{$_}", @did_keys;
$progress .= " "; ## So there's a space before any truely unexpected
## errors perl might emit.
print STDERR
$need_progress_prompt
? $doing
: defined $last_progress
? "\010" x length $last_progress
: (),
$progress;
$last_progress = $progress;
$need_progress_prompt = 0;
}
sub pr {
_interrupt_progress unless $quiet_mode;
my $msg = &_msg;
print STDERR $msg;
_lg $msg;
}
=item pr_doing
pr_doing "Fooo";
pr_doing "Fooo", { ...options... };
pr_doing; ## to show progress
Print a status notification and show progress. Call repeatedly to
show continuing progress. Works with pr() to manage lineends.
Call with no parameters to show progress on the current task. Call
pr_done or pr_done_failed to finish up.
Options:
Expect => $c, # There should be this number of calls, total, not
# including the call with the options set..
=cut
sub pr_doing {
return if $quiet_mode;
if ( @_ ) {
my $options = ref $_[-1] eq "HASH" ? pop : undef;
my $msg = &_msg;
1 while chomp $msg;
if ( $options || ! defined $doing || $doing ne $msg ) {
_reset_progress;
if ( $options ) {
$expect = $options->{Expect};
}
print STDERR " completed (perhaps; pr_done() not called)\n"
if defined $doing;
$doing = $msg;
_lg $doing, "\n";
}
_start_progress;
return;
}
++$count;
_show_progress;
}
=item pr_did
pr_did $what, $status;
Adds a message to the progress bar, does not affect progress otherwise.
Useful to display additional progress metrics.
Call before pr_doing.
=cut
sub pr_did {
my ( $what, $status ) = @_;
lg "did ", $what, " ", $status;
push @did_keys, $what unless exists $did{$what};
$did{$what} = $status;
}
=item pr_active
Show that we're active.
=cut
sub pr_active {
_show_progress if defined $doing && !$quiet_mode;
}
=item pr_done
Called to end a "pr_doing" sucessfully. Logs the completion bug does not
emit to STDERR. Prints and logs any message passed.
=cut
sub pr_done {
return unless defined $doing;
_lg $doing, " completed\n";
print STDERR "\n";
_reset_progress;
$doing = undef;
goto &pr if @_;
}
=item p4_done_failed
Called to end a "pr_doing" in dismal failure. Logs the (in)completion and
and emits a message to the log and STDERR if one is passed.
=cut
sub pr_done_failed {
print STDERR "\n";
_lg $doing, " FAILED\n";
$doing = undef;
$need_progress_prompt = 0;
goto &pr if @_;
}
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.
# {
# my $old_fh = select LOG;
# $| = 1;
# select $old_fh;
# }
## Print a header line guaranteed to start at the beginning of a
## line.
print LOG "\n", "#" x 79, "\n";
lg "started ",
scalar localtime $start_time,
" (",
scalar gmtime $start_time,
" GMT)";
}
END {
lg "ended";
}
=item BUG
Reports a bug using Carp::confess and logging the information.
=cut
sub BUG {
_interrupt_progress;
print STDERR "\n";
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";
print LOG "\n\%INC:\n";
print LOG " $_ => '$INC{$_}'\n" for sort keys %INC;
print LOG "\n";
require Carp;
eval { Carp::confess "stack trace" };
warn $@;
system $^X, "-V" and warn "$! getting perl -V\n";
exit 1;
}
=item set_quiet_mode
set_quiet_mode;
set_quiet_mode( 1 );
set_quiet_mode( 0 );
Called to quash (or allow) progress bars. See the "--quiet" option
on the command line.
=cut
sub set_quiet_mode {
$quiet_mode = @_ ? shift : 1;
}
=item start_time
Returns the time the application started. This is a floating point
number if Time::HiRes was found.
=cut
sub start_time() { $start_time }
=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 ;
|