File Coverage

lib/ProjectBuilder/Log.pm
Criterion Covered Total %
statement 12 151 7.9
branch 0 44 0.0
condition 0 17 0.0
subroutine 4 18 22.2
pod 0 14 0.0
total 16 244 6.5


line stmt bran cond sub pod time code
1             package ProjectBuilder::Log;
2              
3             # this class can be used to store and analyze the complete log from pb
4             # this includes more than one vm
5              
6 1     1   1053 use strict;
  1         2  
  1         39  
7 1     1   4 use ProjectBuilder::Version;
  1         1  
  1         44  
8 1     1   4 use ProjectBuilder::Base;
  1         2  
  1         501  
9 1     1   619 use ProjectBuilder::Log::Item;
  1         3  
  1         1924  
10              
11             sub new {
12             # contains the object name (here PBLog)
13 0     0 0   my $object = shift;
14 0           my $self = {};
15              
16             # $self should point to an object of type $object
17 0           bless($self, $object);
18            
19             # this array stores our childs
20 0           $self->{'logitems'} = [];
21              
22 0           return($self);
23             }
24              
25             # returns number of handled ProjectBuilder::Log::Item's
26             sub countItems {
27 0     0 0   my $self = shift;
28 0           return scalar(@{$self->{'logitems'}});
  0            
29             }
30              
31             # returns an array of all names of handled ProjectBuilder::Log::Item's
32             # the name is the vm name (e.g. ubuntu-10.04-i386 (by default))
33             sub itemNames {
34 0     0 0   my $self = shift;
35 0           my @result = ();
36              
37 0           foreach my $item (@{$self->{'logitems'}}) {
  0            
38 0           push(@result, $item->name());
39             }
40 0           return @result;
41             }
42              
43             # set's the log for ProjectBuilder::Log::item $vmname
44             # if such an item is not present, one is added
45             # $log should only contain the log of one machine
46             sub setLog {
47 0     0 0   my $self = shift;
48 0           my $vmname = shift;
49 0           my $log = shift;
50            
51 0           my $logitem = $self->findItem($vmname);
52 0 0         if (!$logitem) {
53 0           $logitem = new ProjectBuilder::Log::Item($vmname);
54 0           push(@{$self->{'logitems'}}, $logitem);
  0            
55             }
56 0           $logitem->setLog($log);
57             }
58              
59             # used to analyze the complete log of pb
60             sub setCompleteLog {
61 0     0 0   my $self = shift;
62 0           my $log = shift;
63 0           my $tmplog = "";
64 0           my $item = undef;
65            
66 0           foreach my $line (split("\n", $log)) {
67 0 0         if ($line =~ m/^Waiting [0-9]+ s for VM/) {
68             # here starts a new machine, so append the tmplog to the last one
69 0 0         if (defined($item)) {
70 0           $item->setLog($tmplog);
71             }
72 0 0         if($line =~ m/VM ([^\s]+)/){
73 0           $item = new ProjectBuilder::Log::Item($1);
74 0           push(@{$self->{'logitems'}}, $item);
  0            
75 0           $tmplog = 0;
76             }
77             } else {
78 0           $tmplog .= $line ."\n";
79             }
80             }
81 0 0 0       if (defined($item) && ($tmplog)) {
82 0           $item->setLog($tmplog);
83             }
84             }
85              
86             # nums the issues (Warnings and Errors from lintian and rpmlint
87             # if no name is given, the total of all ProjectBuilder::Log::Item's is returned
88             sub numQaIssues {
89 0     0 0   my $self = shift;
90 0   0       my $itemname = shift || "";
91 0           my $result = 0;
92              
93 0 0         if ($itemname eq "") {
94             # no machine selected, so return combine from all items
95 0           foreach my $item (@{$self->{'logitems'}}) {
  0            
96 0           $result += scalar($item->qaIssues());
97             }
98             } else {
99 0           my $item = $self->findItem($itemname);
100 0 0         if ($item) {
101 0           $result = $item->numQaIssues();
102             }
103             }
104 0           return $result;
105             }
106              
107             # returns the issues itself
108             # behaves like numQaIssues
109             sub qaIssues {
110 0     0 0   my $self = shift;
111 0   0       my $itemname = shift || "";
112 0           my @result = ();
113              
114 0 0         if ($itemname eq "") {
115             # no machine selected, so return combine from all items
116 0           foreach my $item (@{$self->{'logitems'}}) {
  0            
117 0           push(@result, $item->qaIssues());
118             }
119             } else {
120 0           my $item = $self->findItem($itemname);
121 0 0         if ($item) {
122 0           push(@result, $item->qaIssues());
123             }
124             }
125 0           return @result;
126             }
127              
128             # same as num qaIssues but for compile errors
129             sub numErrors {
130 0     0 0   my $self = shift;
131 0   0       my $itemname = shift || "";
132 0           my $result = 0;
133              
134 0 0         if ($itemname eq "") {
135             # no machine selected, so return combine from all items
136 0           foreach my $item (@{$self->{'logitems'}}) {
  0            
137 0           $result += $item->numErrors();
138             }
139             } else {
140 0           my $item = $self->findItem($itemname);
141 0 0         if ($item) {
142 0           $result = $item->numErrors();
143             }
144             }
145 0           return $result;
146             }
147              
148             # returns the compile errors itself
149             # behaves like numQaIssues
150             sub errors {
151 0     0 0   my $self = shift;
152 0   0       my $itemname = shift || "";
153 0           my @result = ();
154              
155 0 0         if ($itemname eq "") {
156             # no machine selected, so return combine from all items
157 0           foreach my $item (@{$self->{'logitems'}}) {
  0            
158 0           push(@result, $item->errors());
159             }
160             } else {
161 0           my $item = $self->findItem($itemname);
162 0 0         if ($item) {
163 0           push(@result, $item->errors());
164             }
165             }
166 0           return @result;
167             }
168              
169             # same as num qaIssues but for compile warnings
170             sub numWarnings {
171 0     0 0   my $self = shift;
172 0   0       my $itemname = shift || "";
173 0           my $result = 0;
174              
175 0 0         if ($itemname eq "") {
176             # no machine selected, so return combine from all items
177 0           foreach my $item (@{$self->{'logitems'}}) {
  0            
178 0           $result += $item->numWarnings();
179             }
180             } else {
181 0           my $item = $self->findItem($itemname);
182 0 0         if ($item) {
183 0           $result = $item->numWarnings();
184             }
185             }
186 0           return $result;
187             }
188              
189             # returns the compile warnings itself
190             # behaves like numQaIssues
191             sub warnings {
192 0     0 0   my $self = shift;
193 0   0       my $itemname = shift || "";
194 0           my @result = ();
195              
196 0 0         if ($itemname eq "") {
197             # no machine selected, so return combine from all items
198 0           foreach my $item (@{$self->{'logitems'}}) {
  0            
199 0           push(@result, $item->warnings());
200             }
201             } else {
202 0           my $item = $self->findItem($itemname);
203 0 0         if ($item) {
204 0           push(@result, $item->warnings());
205             }
206             }
207 0           return @result;
208             }
209              
210             # prints out a summary of the log
211             sub summary {
212 0     0 0   my $self = shift;
213 0           my $summary = "";
214              
215 0           $summary = "Items: ". $self->countItems();
216 0           $summary .= " (QA Issues: ". $self->numQaIssues();
217 0           $summary .= ", Warnings: ". $self->numWarnings();
218 0           $summary .= ", Errors: ". $self->numErrors() .")\n";
219 0           foreach my $name ($self->itemNames()) {
220 0           $summary .= $name ." (QA Issues: ". $self->numQaIssues($name);
221 0           $summary .= ", Warnings: ". $self->numWarnings($name);
222 0           $summary .= ", Errors: ". $self->numErrors($name) .")\n";
223             }
224 0           return $summary;
225             }
226              
227             # mails the summary to $to
228             sub mailSummary {
229             eval
230 0     0 0   {
231 0           require Mail::Sendmail;
232 0           Mail::Sendmail->import();
233             };
234 0 0         if ($@) {
235             # Mail::Sendmail not found not sending mail !
236 0           pb_log(0,"No Mail::Sendmail module found so not sending any mail !\n");
237             } else {
238 0           my $self = shift;
239 0   0       my $to = shift || "";
240              
241 0 0         if ($to eq "") {
242 0           pb_log(0,"Please give a To: address\n");
243 0           return;
244             }
245 0           my %mail = (
246             To => $to,
247             From => "pb\@localhost",
248             Message => $self->summary()
249             );
250 0 0         if (! sendmail(%mail)) {
251 0 0         if (defined $Mail::Sendmail::error) {
252 0           return $Mail::Sendmail::error;
253             } else {
254 0           return "Unkown error";
255             }
256             }
257 0           pb_log(0,"Mail send to ". $to ."\n");
258             }
259             }
260              
261             # private part (perl does not no about private, but it is meant so)
262             # find's item with name $vmname in handled OB::Log::Item's
263             sub findItem {
264 0     0 0   my $self = shift;
265 0           my $vmname = shift;
266              
267             # find existing item or add item if needed
268 0           foreach my $logitem (@{$self->{'logitems'}}) {
  0            
269 0 0         if ($logitem->name eq $vmname) {
270 0           return $logitem;
271             }
272             }
273 0           return 0;
274             }
275              
276             1;