File Coverage

blib/lib/Log/Dispatch/TextTable.pm
Criterion Covered Total %
statement 45 46 97.8
branch 7 8 87.5
condition 1 2 50.0
subroutine 13 14 92.8
pod 7 7 100.0
total 73 77 94.8


line stmt bran cond sub pod time code
1 3     3   131879 use strict;
  3         6  
  3         106  
2 3     3   17 use warnings;
  3         8  
  3         163  
3             package Log::Dispatch::TextTable;
4             {
5             $Log::Dispatch::TextTable::VERSION = '0.032';
6             }
7 3     3   2530 use parent qw(Log::Dispatch::Output);
  3         924  
  3         17  
8             # ABSTRACT: log events to a textual table
9              
10 3     3   7328 use Log::Dispatch 2.0 ();
  3         101  
  3         224  
11 3     3   4127 use Text::Table;
  3         64070  
  3         1249  
12              
13              
14             sub new {
15 3     3 1 2631 my ($class, %arg) = @_;
16              
17             # when done, by default, print out the passed-in table
18 3   50 0   17 $arg{send_to} ||= sub { print $_[0] };
  0         0  
19              
20             # construct the column list, using the default if no columns were given
21 3 100       21 my @columns = $arg{columns} ? @{ $arg{columns} } : qw(time level message);
  1         4  
22 3         9 my @header = map { $_, \q{ | } } @columns;
  8         20  
23 3         14 $#header--; # drop the final |-divider
24              
25 3         30 my $table = Text::Table->new(@header);
26              
27 3         3770 my $self = {
28             columns => \@columns,
29             table => $table,
30             send_to => $arg{send_to},
31             flush_if => $arg{flush_if},
32             };
33              
34 3         10 bless $self => $class;
35              
36             # this is our duty as a well-behaved Log::Dispatch plugin
37 3         43 $self->_basic_init(%arg);
38              
39 3         390 return $self;
40             }
41              
42              
43             sub log_message {
44 11     11 1 8937 my ($self, %p) = @_;
45 11 50       937 $p{time} = localtime unless exists $p{time};
46              
47 11         61 $self->table->add(
48 11         40 @p{ @{ $self->{columns} } }
49             );
50              
51 11 100       732 $self->flush(\%p) if $self->should_flush;
52             }
53              
54              
55 28     28 1 1123 sub table { return $_[0]->{table} }
56              
57              
58             sub entry_count {
59 6     6 1 25 my ($self) = @_;
60 6         11 $self->table->body_height;
61             }
62              
63              
64             sub flush {
65 1     1 1 15 my ($self) = @_;
66 1         5 $self->transmit;
67 1         7950 $self->table->clear;
68             }
69              
70              
71             sub should_flush {
72 11     11 1 21 my ($self, $p) = @_;
73              
74 11 100       77 return unless (ref $self->{flush_if} eq 'CODE');
75              
76 6         19 return $self->{flush_if}->($self, $p);
77             }
78              
79              
80             sub transmit {
81 4     4 1 7 my ($self) = @_;
82 4         14 $self->{send_to}->($self->table);
83             }
84              
85             sub DESTROY {
86 3     3   81 my ($self) = @_;
87 3         13 $self->transmit;
88             }
89              
90              
91             1;
92              
93             __END__