File Coverage

blib/lib/POE/Component/Client/opentick/Output.pm
Criterion Covered Total %
statement 71 71 100.0
branch 20 32 62.5
condition 12 30 40.0
subroutine 22 22 100.0
pod 14 14 100.0
total 139 169 82.2


line stmt bran cond sub pod time code
1             package POE::Component::Client::opentick::Output;
2             #
3             # opentick.com POE client
4             #
5             # Diagnostic message output class
6             #
7             # infi/2008
8             #
9             # $Id: Output.pm 56 2009-01-08 16:51:14Z infidel $
10             #
11             # Full POD documentation after __END__
12             #
13              
14 3     3   52117 use strict;
  3         5  
  3         116  
15 3     3   16 use warnings;
  3         5  
  3         92  
16 3     3   1274 use Time::HiRes qw( time );
  3         3135  
  3         28  
17              
18 3     3   973 use overload '""' => \&stringify;
  3         8  
  3         26  
19              
20 3     3   229 use vars qw( $VERSION $TRUE $FALSE $DEBUG $QUIET $PREFIX );
  3         12  
  3         615  
21              
22             BEGIN {
23 3     3   18 require Exporter;
24 3         45 our @ISA = qw( Exporter );
25 3         16 our @EXPORT = qw( O_DEBUG O_INFO O_NOTICE O_WARN O_ERROR );
26 3         3752 ($VERSION) = q$Revision: 56 $ =~ /(\d+)/;
27             }
28              
29             ###
30             ### Variables
31             ###
32              
33             *TRUE = \1;
34             *FALSE = \0;
35             $DEBUG = $FALSE;
36             $QUIET = $FALSE;
37             $PREFIX = $TRUE;
38              
39             my $output = {
40             DEBUG => *STDERR,
41             INFO => *STDOUT,
42             NOTICE => *STDOUT,
43             WARN => *STDERR,
44             ERROR => *STDERR,
45             };
46              
47             ###
48             ### Exported functions
49             ###
50              
51             sub O_DEBUG
52             {
53 24     24 1 73 my @msg = @_;
54 24         61 my $msg = join( ' ', @msg );
55              
56 24 100 66     107 _output( $msg, 'DEBUG' ) if $DEBUG and( !defined( wantarray ) );
57              
58             return( defined( wantarray )
59 24   33     102 and POE::Component::Client::opentick::Output->new( 'DEBUG', $msg ) );
60             }
61              
62             sub O_INFO
63             {
64 2     2 1 1846 my @msg = @_;
65 2         6 my $msg = join( ' ', @msg );
66              
67 2 50       8 _output( $msg, 'INFO' ) unless( defined( wantarray ) );
68              
69             return( defined( wantarray )
70 2   33     9 and POE::Component::Client::opentick::Output->new( 'INFO', $msg ) );
71             }
72              
73             sub O_NOTICE
74             {
75 6     6 1 408 my @msg = @_;
76 6         19 my $msg = join( ' ', @msg );
77              
78 6 50       25 _output( $msg, 'NOTICE' ) unless( defined( wantarray ) );
79              
80             return( defined( wantarray )
81 6   33     27 and POE::Component::Client::opentick::Output->new( 'NOTICE', $msg ) );
82             }
83              
84             sub O_WARN
85             {
86 3     3 1 395 my @msg = @_;
87 3         6 my $msg = join( ' ', @msg );
88              
89 3 50       11 _output( $msg, 'WARN' ) unless( defined( wantarray ) );
90              
91             return( defined( wantarray )
92 3   33     12 and POE::Component::Client::opentick::Output->new( 'WARN', $msg ) );
93             }
94              
95             sub O_ERROR
96             {
97 2     2 1 782 my @msg = @_;
98 2         6 my $msg = join( ' ', @msg );
99              
100 2 100       8 _output( $msg, 'ERROR' ) unless( defined( wantarray ) );
101              
102             return( defined( wantarray )
103 2   66     18 and POE::Component::Client::opentick::Output->new( 'ERROR', $msg ) );
104             }
105              
106             ###
107             ### Class methods
108             ###
109              
110             # Constructor
111             sub new
112             {
113 1     1 1 3 my( $class, $level, $msg, @args ) = @_;
114              
115 1         11 my $self = bless( {
116             level => $level,
117             message => $msg,
118             timestamp => time,
119             }, 'POE::Component::Client::opentick::Output' );
120              
121 1         4 $self->initialize( @args );
122              
123 1         65 return( $self );
124             }
125              
126             # Overload this.
127 1     1 1 2 sub initialize {}
128              
129             # And this.
130             sub stringify
131             {
132 2     2 1 4 my( $self ) = @_;
133              
134 2         6 my $message = sprintf( "OT:%s:%s:%s\n",
135             $self->get_level(),
136             $self->get_timestamp(),
137             $self->get_message() );
138              
139 2         23 return( $message );
140             }
141              
142             ###
143             ### Accessor methods
144             ###
145              
146             # Hybrid method; set the DEBUG flag
147             sub set_debug
148             {
149 1     1 1 1605 my $junk = shift;
150 1 50 33     13 my $value = ref( $junk ) || $junk =~ /::/ ? shift : $junk;
151              
152 1 50       6 return( $DEBUG = $value ? $TRUE : $FALSE );
153             }
154              
155             # Hybrid method: set the QUIET flag; overrides DEBUG
156             sub set_quiet
157             {
158 2     2 1 436 my( $junk ) = shift;
159 2 50 33     24 my $value = ref( $junk ) || $junk =~ /::/ ? shift : $junk;
160              
161 2 50       11 return( $QUIET = $value ? $TRUE : $FALSE );
162             }
163              
164             # Hybrid method: set the PREFIX flag.
165             sub set_prefix
166             {
167 1     1 1 372 my $junk = shift;
168 1 50 33     10 my $value = ref( $junk ) || $junk =~ /::/ ? shift : $junk;
169              
170 1 50       4 return( $PREFIX = $value ? $TRUE : $FALSE );
171             }
172              
173             # INSTANCE METHOD
174             sub get_level
175             {
176 3     3 1 4 my( $self ) = shift;
177 3 50       10 return unless( ref( $self ) );
178              
179 3         10 return( $self->{level} );
180             }
181              
182             # INSTANCE METHOD
183             sub get_message
184             {
185 3     3 1 6 my( $self ) = shift;
186 3 50       17 return unless( ref( $self ) );
187              
188 3         39 return( $self->{message} );
189             }
190              
191             # INSTANCE METHOD
192             sub get_timestamp
193             {
194 3     3 1 7 my( $self ) = shift;
195 3 50       7 return unless( ref( $self ) );
196              
197 3         9 return( $self->{timestamp} );
198             }
199              
200             ###
201             ### Private methods
202             ###
203              
204             # Private output method
205             sub _output
206             {
207 13 100   13   49 return if $QUIET;
208 7         10 my( $msg, $level ) = @_;
209              
210 7 100       9 printf { _get_filehandle( $level ) } "%s%s\n",
  7         14  
211             $PREFIX
212             ? 'OT:' . $level . ': '
213             : '',
214             $msg;
215             }
216              
217             sub _get_filehandle
218             {
219 7     7   8 my( $level ) = @_;
220              
221 7   33     98 return( $output->{$level} || *STDOUT );
222             }
223              
224             1;
225              
226             __END__