source: ProjectBuilder/devel/pb-modules/lib/ProjectBuilder/Log.pm@ 1148

Last change on this file since 1148 was 1148, checked in by Bruno Cornec, 13 years ago
  • Most modules now have a VERSION declared
  • Moulde Version.pm move to pb-modules due to that
File size: 5.8 KB
Line 
1package 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
6use strict;
7use ProjectBuilder::Version;
8use ProjectBuilder::Base;
9use ProjectBuilder::Log::Item;
10
11sub new {
12 # contains the object name (here PBLog)
13 my $object = shift;
14 my $self = {};
15
16 # $self should point to an object of type $object
17 bless($self, $object);
18
19 # this array stores our childs
20 $self->{'logitems'} = [];
21
22 return($self);
23}
24
25# returns number of handled ProjectBuilder::Log::Item's
26sub countItems {
27 my $self = shift;
28 return scalar(@{$self->{'logitems'}});
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))
33sub itemNames {
34 my $self = shift;
35 my @result = ();
36
37 foreach my $item (@{$self->{'logitems'}}) {
38 push(@result, $item->name());
39 }
40 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
46sub setLog {
47 my $self = shift;
48 my $vmname = shift;
49 my $log = shift;
50
51 my $logitem = $self->findItem($vmname);
52 if (!$logitem) {
53 $logitem = new ProjectBuilder::Log::Item($vmname);
54 push(@{$self->{'logitems'}}, $logitem);
55 }
56 $logitem->setLog($log);
57}
58
59# used to analyze the complete log of pb
60sub setCompleteLog {
61 my $self = shift;
62 my $log = shift;
63 my $tmplog = "";
64 my $item = undef;
65
66 foreach my $line (split("\n", $log)) {
67 if ($line =~ m/^Waiting [0-9]+ s for VM/) {
68 # here starts a new machine, so append the tmplog to the last one
69 if (defined($item)) {
70 $item->setLog($tmplog);
71 }
72 if($line =~ m/VM ([^\s]+)/){
73 $item = new ProjectBuilder::Log::Item($1);
74 push(@{$self->{'logitems'}}, $item);
75 $tmplog = 0;
76 }
77 } else {
78 $tmplog .= $line ."\n";
79 }
80 }
81 if (defined($item) && ($tmplog)) {
82 $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
88sub numQaIssues {
89 my $self = shift;
90 my $itemname = shift || "";
91 my $result = 0;
92
93 if ($itemname eq "") {
94 # no machine selected, so return combine from all items
95 foreach my $item (@{$self->{'logitems'}}) {
96 $result += scalar($item->qaIssues());
97 }
98 } else {
99 my $item = $self->findItem($itemname);
100 if ($item) {
101 $result = $item->numQaIssues();
102 }
103 }
104 return $result;
105}
106
107# returns the issues itself
108# behaves like numQaIssues
109sub qaIssues {
110 my $self = shift;
111 my $itemname = shift || "";
112 my @result = ();
113
114 if ($itemname eq "") {
115 # no machine selected, so return combine from all items
116 foreach my $item (@{$self->{'logitems'}}) {
117 push(@result, $item->qaIssues());
118 }
119 } else {
120 my $item = $self->findItem($itemname);
121 if ($item) {
122 push(@result, $item->qaIssues());
123 }
124 }
125 return @result;
126}
127
128# same as num qaIssues but for compile errors
129sub numErrors {
130 my $self = shift;
131 my $itemname = shift || "";
132 my $result = 0;
133
134 if ($itemname eq "") {
135 # no machine selected, so return combine from all items
136 foreach my $item (@{$self->{'logitems'}}) {
137 $result += $item->numErrors();
138 }
139 } else {
140 my $item = $self->findItem($itemname);
141 if ($item) {
142 $result = $item->numErrors();
143 }
144 }
145 return $result;
146}
147
148# returns the compile errors itself
149# behaves like numQaIssues
150sub errors {
151 my $self = shift;
152 my $itemname = shift || "";
153 my @result = ();
154
155 if ($itemname eq "") {
156 # no machine selected, so return combine from all items
157 foreach my $item (@{$self->{'logitems'}}) {
158 push(@result, $item->errors());
159 }
160 } else {
161 my $item = $self->findItem($itemname);
162 if ($item) {
163 push(@result, $item->errors());
164 }
165 }
166 return @result;
167}
168
169# same as num qaIssues but for compile warnings
170sub numWarnings {
171 my $self = shift;
172 my $itemname = shift || "";
173 my $result = 0;
174
175 if ($itemname eq "") {
176 # no machine selected, so return combine from all items
177 foreach my $item (@{$self->{'logitems'}}) {
178 $result += $item->numWarnings();
179 }
180 } else {
181 my $item = $self->findItem($itemname);
182 if ($item) {
183 $result = $item->numWarnings();
184 }
185 }
186 return $result;
187}
188
189# returns the compile warnings itself
190# behaves like numQaIssues
191sub warnings {
192 my $self = shift;
193 my $itemname = shift || "";
194 my @result = ();
195
196 if ($itemname eq "") {
197 # no machine selected, so return combine from all items
198 foreach my $item (@{$self->{'logitems'}}) {
199 push(@result, $item->warnings());
200 }
201 } else {
202 my $item = $self->findItem($itemname);
203 if ($item) {
204 push(@result, $item->warnings());
205 }
206 }
207 return @result;
208}
209
210# prints out a summary of the log
211sub summary {
212 my $self = shift;
213 my $summary = "";
214
215 $summary = "Items: ". $self->countItems();
216 $summary .= " (QA Issues: ". $self->numQaIssues();
217 $summary .= ", Warnings: ". $self->numWarnings();
218 $summary .= ", Errors: ". $self->numErrors() .")\n";
219 foreach my $name ($self->itemNames()) {
220 $summary .= $name ." (QA Issues: ". $self->numQaIssues($name);
221 $summary .= ", Warnings: ". $self->numWarnings($name);
222 $summary .= ", Errors: ". $self->numErrors($name) .")\n";
223 }
224 return $summary;
225}
226
227# mails the summary to $to
228sub mailSummary {
229 require Mail::Sendmail;
230 my $self = shift;
231 my $to = shift || "";
232
233 if ($to eq "") {
234 pb_log(0,"Please give a To: address\n");
235 return;
236 }
237 my %mail = (
238 To => $to,
239 From => "pb\@localhost",
240 Message => $self->summary()
241 );
242 if (! sendmail(%mail)) {
243 if (defined $Mail::Sendmail::error) {
244 return $Mail::Sendmail::error;
245 } else {
246 return "Unkown error";
247 }
248 }
249 pb_log(0,"Mail send to ". $to ."\n");
250}
251
252# private part (perl does not no about private, but it is meant so)
253# find's item with name $vmname in handled OB::Log::Item's
254sub findItem {
255 my $self = shift;
256 my $vmname = shift;
257
258 # find existing item or add item if needed
259 foreach my $logitem (@{$self->{'logitems'}}) {
260 if ($logitem->name eq $vmname) {
261 return $logitem;
262 }
263 }
264 return 0;
265}
266
2671;
Note: See TracBrowser for help on using the repository browser.