File Coverage

blib/lib/Paranoid/Log/Buffer.pm
Criterion Covered Total %
statement 43 43 100.0
branch 5 8 62.5
condition n/a
subroutine 11 11 100.0
pod 5 5 100.0
total 64 67 95.5


line stmt bran cond sub pod time code
1             # Paranoid::Log::Buffer -- Log buffer support for paranoid programs
2             #
3             # $Id: lib/Paranoid/Log/Buffer.pm, 2.09 2021/12/28 15:46:49 acorliss Exp $
4             #
5             # This software is free software. Similar to Perl, you can redistribute it
6             # and/or modify it under the terms of either:
7             #
8             # a) the GNU General Public License
9             # as published by the
10             # Free Software Foundation ; either version 1
11             # , or any later version
12             # , or
13             # b) the Artistic License 2.0
14             # ,
15             #
16             # subject to the following additional term: No trademark rights to
17             # "Paranoid" have been or are conveyed under any of the above licenses.
18             # However, "Paranoid" may be used fairly to describe this unmodified
19             # software, in good faith, but not as a trademark.
20             #
21             # (c) 2005 - 2020, Arthur Corliss (corliss@digitalmages.com)
22             # (tm) 2008 - 2020, Paranoid Inc. (www.paranoid.com)
23             #
24             #####################################################################
25              
26             #####################################################################
27             #
28             # Environment definitions
29             #
30             #####################################################################
31              
32             package Paranoid::Log::Buffer;
33              
34 3     3   498 use 5.008;
  3         11  
35              
36 3     3   17 use strict;
  3         7  
  3         64  
37 3     3   15 use warnings;
  3         5  
  3         120  
38 3     3   16 use vars qw($VERSION);
  3         5  
  3         141  
39 3     3   15 use Paranoid::Debug qw(:all);
  3         6  
  3         831  
40              
41             ($VERSION) = ( q$Revision: 2.09 $ =~ /(\d+(?:\.\d+)+)/sm );
42              
43 3     3   25 use constant DEFAULT_BUFFSIZE => 20;
  3         15  
  3         1368  
44              
45             #####################################################################
46             #
47             # Module code follows
48             #
49             #####################################################################
50              
51             {
52              
53             # Buffers
54             my %buffers = ();
55              
56             sub addLogger {
57              
58             # Purpose: Creates the named buffer
59             # Returns: Boolean
60             # Usage: $rv = addLogger(%rec);
61              
62 4     4 1 16 my %rec = @_;
63              
64 4         11 $buffers{ $rec{name} } = [];
65              
66 4         13 return 1;
67             }
68              
69             sub delLogger {
70              
71             # Purpose: Deletes the named buffer
72             # Returns: True (1)
73             # Usage: $rv = _delBuffer($name);
74              
75 1     1 1 2 my $name = shift;
76              
77 1 50       3 delete $buffers{$name} if exists $buffers{$name};
78              
79 1         3 return 1;
80             }
81              
82             sub init {
83 2     2 1 111 return 1;
84             }
85              
86             sub logMsg {
87 134     134 1 437 my %record = @_;
88 134         197 my ( $rv, $size, $buffer );
89              
90 134         395 pdebug( 'entering w/%s', PDLEVEL1, %record );
91 134         355 pIn();
92              
93 134 50       296 if ( exists $buffers{ $record{name} } ) {
94             $size =
95             exists $record{options}{size}
96             ? $record{options}{size}
97 134 100       248 : DEFAULT_BUFFSIZE;
98 134         218 $buffer = $buffers{ $record{name} };
99              
100             # Add the message
101 134         323 push @$buffer, [ @record{qw(msgtime message)} ];
102              
103             # Trim if needed
104 134         285 while ( scalar @$buffer > $size ) {
105 52         114 shift @$buffer;
106             }
107              
108 134         186 $rv = 1;
109             }
110              
111 134         308 pOut();
112 134         299 pdebug( 'leaving w/rv: %s', PDLEVEL1, $rv );
113              
114 134         459 return $rv;
115             }
116              
117             sub dumpBuffer {
118              
119             # Purpose: Returns the contents of the named buffer
120             # Returns: Array
121             # Usage: @events = dump($name);
122              
123 12     12 1 45 my $name = shift;
124 12         22 my @rv;
125              
126 12 50       49 @rv = @{ $buffers{$name} } if exists $buffers{$name};
  12         36  
127              
128 12         58 return @rv;
129             }
130              
131             }
132              
133             1;
134              
135             __END__