Paranoid/Log/Buffer.pm

filter: Statementstmt 100% 45 / 45 Branchbran 70.0% 7 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 100% 5 / 5 totaltotal 95.7% 68 / 71 CC 6 SCAR 7.0
CC
6
Cov
95%
logMsg
4.0
delLogger
2.0
dumpBuffer
2.0
CRAP 2.0
« Paranoid.pm ↑ Summary Paranoid/Data/AVLTree/AVLNode.pm »
1 # Paranoid::Log::Buffer -- Log buffer support for paranoid programs
2 #
3 # $Id: lib/Paranoid/Log/Buffer.pm, 2.10 2022/03/08 00:01:04 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 # <https://www.gnu.org/licenses/gpl-1.0.html> as published by the
10 # Free Software Foundation <http://www.fsf.org/>; either version 1
11 # <https://www.gnu.org/licenses/gpl-1.0.html>, or any later version
12 # <https://www.gnu.org/licenses/license-list.html#GNUGPL>, or
13 # b) the Artistic License 2.0
14 # <https://opensource.org/licenses/Artistic-2.0>,
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 4 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 4 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
39 4 ▶use Paranoid::Debug qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 4 ($VERSION) = ( q$Revision: 2.10 $ =~ /(\d+(?:\.\d+)+)/sm );
42  
43 4 ▶use constant DEFAULT_BUFFSIZE => 20;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 #####################################################################
46 #
47 # Module code follows
48 #
49 #####################################################################
50  
51 {
52  
53 # Buffers
54 4 my %buffers = ();
55  
56 4 ▶ sub addLogger {
Subroutine addLogger: called CC=1 SCAR=0.0
Pod documented
57  
58 # Purpose: Creates the named buffer
59 # Returns: Boolean
60 # Usage: $rv = addLogger(%rec);
61  
62 4 my %rec = @_;
63  
64 4 $buffers{ $rec{name} } = [];
65  
66 4 return 1;
67 }
68  
69 1 ▶ sub delLogger {
Subroutine delLogger: called CC=2 SCAR=7.1
Pod documented
70  
71 # Purpose: Deletes the named buffer
72 # Returns: True (1)
73 # Usage: $rv = _delBuffer($name);
74  
75 1 my $name = shift;
76  
77 1 ▶ delete $buffers{$name} if exists $buffers{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $buffers{$name} 0 1 1
78  
79 1 return 1;
80 }
81  
82 2 ▶ sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod documented
83 2 return 1;
84 }
85  
86 134 ▶ sub logMsg {
Subroutine logMsg: called CC=4 SCAR=13.9
Pod documented
87 134 my %record = @_;
88 134 my ( $rv, $size, $buffer );
89  
90 134 subPreamble( PDLEVEL1, '%' );
91  
92 134 ▶ if ( exists $buffers{ $record{name} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $buffers{$record{"name"}}) 134 0 134
93 $size =
94 exists $record{options}{size}
95 ? $record{options}{size}
96 134 ▶ : DEFAULT_BUFFSIZE;
Branch: 1 branch
BranchTrueFalseTotal
exists $record{'options'}{'size'} ? : 80 54 134
97 134 $buffer = $buffers{ $record{name} };
98  
99 # Add the message
100 134 push @$buffer, [ @record{qw(msgtime message)} ];
101  
102 # Trim if needed
103 134 ▶ while ( scalar @$buffer > $size ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$buffer > $size) 52 134 186
104 52 shift @$buffer;
105 }
106  
107 134 $rv = 1;
108 }
109  
110 134 subPostamble( PDLEVEL1, '$', $rv );
111  
112 134 return $rv;
113 }
114  
115 12 ▶ sub dumpBuffer {
Subroutine dumpBuffer: called CC=2 SCAR=7.0
Pod documented
116  
117 # Purpose: Returns the contents of the named buffer
118 # Returns: Array
119 # Usage: @events = dump($name);
120  
121 12 my $name = shift;
122 12 my @rv;
123  
124 12 ▶ @rv = @{ $buffers{$name} } if exists $buffers{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $buffers{$name} 12 0 12
125  
126 12 return @rv;
127 }
128  
129 }
130  
131 4 1;
132  
133 __END__
« Paranoid.pm ↑ Summary Paranoid/Data/AVLTree/AVLNode.pm »