| 1 | # | ||||||||||||||||||||||||||||
| 2 | # Copyright (c) 2003,2004,2005 Alexander Taler (dissent@0--0.org) | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # All rights reserved. This program is free software; you can redistribute it | ||||||||||||||||||||||||||||
| 5 | # and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | package VCS::LibCVS::Client::LoggingIOHandle; | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use IO::Handle; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # | ||||||||||||||||||||||||||||
| 14 | # An IO::Handle which logs everything that goes through it. Useful for | ||||||||||||||||||||||||||||
| 15 | # debugging protcol implementations | ||||||||||||||||||||||||||||
| 16 | # | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | ############################################################################### | ||||||||||||||||||||||||||||
| 19 | # Class constants | ||||||||||||||||||||||||||||
| 20 | ############################################################################### | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 1 | ▶ | use constant REVISION => '$Header: /cvsroot/libcvs-perl/libcvs-perl/VCS/LibCVS/Client/LoggingIOHandle.pm,v 1.5 2005/10/10 12:52:11 dissent Exp $ '; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 1 | ▶ | use vars ('@ISA'); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 1 | @ISA = ("IO::Handle"); | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | ############################################################################### | ||||||||||||||||||||||||||||
| 28 | # Private variables | ||||||||||||||||||||||||||||
| 29 | ############################################################################### | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | # $self->{next} The wrapped IOHandle, to which calls are forwarded | ||||||||||||||||||||||||||||
| 32 | # $self->{Prefix} String to prepend to each line | ||||||||||||||||||||||||||||
| 33 | # $self->{LogOutput} IOHandle where output is duplicated, with optional prefix | ||||||||||||||||||||||||||||
| 34 | # $self->{NotNewLine} False when a new line has just been printed, so the next | ||||||||||||||||||||||||||||
| 35 | # line can be prepended with the prefix. | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | ############################################################################### | ||||||||||||||||||||||||||||
| 38 | # Class routines | ||||||||||||||||||||||||||||
| 39 | ############################################################################### | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | # A new LoggingIOHandle is built around an existing IO::Handle. The | ||||||||||||||||||||||||||||
| 42 | # constructor takes one arg, which is an IO::Handle. | ||||||||||||||||||||||||||||
| 43 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 44 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 45 | 0 | my $ioh = shift; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | my $that = bless {}, $class; | |||||||||||||||||||||||||||
| 48 | 0 | $that->{next} = $ioh; | |||||||||||||||||||||||||||
| 49 | 0 | $that->logfile(); | |||||||||||||||||||||||||||
| 50 | 0 | $that->{NotNewLine} = 0; | |||||||||||||||||||||||||||
| 51 | 0 | return $that; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | ############################################################################### | ||||||||||||||||||||||||||||
| 55 | # Instance routines | ||||||||||||||||||||||||||||
| 56 | ############################################################################### | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # It doesn't put the prefix on each line of multiple line prints | ||||||||||||||||||||||||||||
| 59 | # and doesn't handle multiple line prints properly | ||||||||||||||||||||||||||||
| 60 | 0 | ▶ | sub print { | ||||||||||||||||||||||||||
|
Subroutine
print: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 61 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 62 | 0 | ▶ | if ($self->{Prefix} && (!$self->{NotNewLine})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"Prefix"} and not $self->{"NotNewLine"}
MC/DC0%0/2
$self->{"Prefix"} and not $self->{"NotNewLine"}
$self->{"Prefix"}
not $self->{"NotNewLine"}
Truth table0%0/3
A and not(B)
A= $self->{"Prefix"}B= not $self->{"NotNewLine"}
| |||||||||||||||||||||||||||||
| 63 | 0 | $self->{LogOutput}->print($self->{Prefix}); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | # Set NotNewLine | ||||||||||||||||||||||||||||
| 67 | 0 | ▶ | if (!$self->{NotNewLine}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 0 | $self->{NotNewLine} = 1; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | 0 | ▶ | map { $self->{NotNewLine} = 0 if /\n/ } @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | $self->{LogOutput}->print(@_); | |||||||||||||||||||||||||||
| 73 | 0 | $self->{LogOutput}->flush; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | return $self->{next}->print(@_); | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | sub getc { | ||||||||||||||||||||||||||
|
Subroutine
getc: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 79 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 80 | 0 | my $char = $self->{next}->getc(); | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 0 | ▶ | if (($self->{Prefix}) && (!$self->{NotNewLine})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"Prefix"} and not $self->{"NotNewLine"}
MC/DC0%0/2
$self->{"Prefix"} and not $self->{"NotNewLine"}
$self->{"Prefix"}
not $self->{"NotNewLine"}
Truth table0%0/3
A and not(B)
A= $self->{"Prefix"}B= not $self->{"NotNewLine"}
| |||||||||||||||||||||||||||||
| 83 | 0 | $self->{LogOutput}->print($self->{Prefix}); | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | $self->{NotNewLine} = ($char ne "\n"); | |||||||||||||||||||||||||||
| 87 | 0 | $self->{LogOutput}->print($char); | |||||||||||||||||||||||||||
| 88 | 0 | $self->{LogOutput}->flush(); | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | return $char; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | ▶ | sub getline { | ||||||||||||||||||||||||||
|
Subroutine
getline: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 94 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 95 | 0 | my $line = $self->{next}->getline(); | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | if (($self->{Prefix}) && (!$self->{NotNewLine})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"Prefix"} and not $self->{"NotNewLine"}
MC/DC0%0/2
$self->{"Prefix"} and not $self->{"NotNewLine"}
$self->{"Prefix"}
not $self->{"NotNewLine"}
Truth table0%0/3
A and not(B)
A= $self->{"Prefix"}B= not $self->{"NotNewLine"}
| |||||||||||||||||||||||||||||
| 98 | 0 | $self->{LogOutput}->print($self->{Prefix}); | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | $self->{NotNewLine} = 0; | |||||||||||||||||||||||||||
| 102 | 0 | $self->{LogOutput}->print($line); | |||||||||||||||||||||||||||
| 103 | 0 | $self->{LogOutput}->flush(); | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | return $line; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | sub read { | ||||||||||||||||||||||||||
|
Subroutine
read: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 109 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 110 | 0 | return $self->{next}->read(@_); | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # set and get the prefix to use | ||||||||||||||||||||||||||||
| 114 | # Undefined means don't print a prefix | ||||||||||||||||||||||||||||
| 115 | 0 | ▶ | sub prefix { | ||||||||||||||||||||||||||
|
Subroutine
prefix: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 116 | 0 | my ($self, $new_prefix) = @_; | |||||||||||||||||||||||||||
| 117 | 0 | ▶ | $self->{Prefix} = $new_prefix if (defined $new_prefix); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 0 | return $self->{Prefix}; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # set the output filename | ||||||||||||||||||||||||||||
| 122 | # If it's undefined, STDERR is used | ||||||||||||||||||||||||||||
| 123 | 0 | ▶ | sub logfile { | ||||||||||||||||||||||||||
|
Subroutine
logfile: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 124 | 0 | my ($self, $filename) = @_; | |||||||||||||||||||||||||||
| 125 | 0 | ▶ | if (defined $filename) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 0 | $self->{LogOutput} = IO::File->new(">> $filename"); | |||||||||||||||||||||||||||
| 127 | } else { | ||||||||||||||||||||||||||||
| 128 | 0 | $self->{LogOutput} = IO::Handle->new_from_fd(fileno(STDERR), ">>"); | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | ############################################################################### | ||||||||||||||||||||||||||||
| 133 | # Private routines | ||||||||||||||||||||||||||||
| 134 | ############################################################################### | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 1 | 1; | |||||||||||||||||||||||||||