xref: /AOO41X/main/solenv/bin/modules/installer/logger.pm (revision 2f2de527132889ec404cfd5d6888f1de663179ab)
19780544fSAndrew Rist#**************************************************************
2cdf0e10cSrcweir#
39780544fSAndrew Rist#  Licensed to the Apache Software Foundation (ASF) under one
49780544fSAndrew Rist#  or more contributor license agreements.  See the NOTICE file
59780544fSAndrew Rist#  distributed with this work for additional information
69780544fSAndrew Rist#  regarding copyright ownership.  The ASF licenses this file
79780544fSAndrew Rist#  to you under the Apache License, Version 2.0 (the
89780544fSAndrew Rist#  "License"); you may not use this file except in compliance
99780544fSAndrew Rist#  with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir#
119780544fSAndrew Rist#    http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir#
139780544fSAndrew Rist#  Unless required by applicable law or agreed to in writing,
149780544fSAndrew Rist#  software distributed under the License is distributed on an
159780544fSAndrew Rist#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
169780544fSAndrew Rist#  KIND, either express or implied.  See the License for the
179780544fSAndrew Rist#  specific language governing permissions and limitations
189780544fSAndrew Rist#  under the License.
19cdf0e10cSrcweir#
209780544fSAndrew Rist#**************************************************************
219780544fSAndrew Rist
229780544fSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweirpackage installer::logger;
25cdf0e10cSrcweir
26cdf0e10cSrcweiruse installer::files;
27cdf0e10cSrcweiruse installer::globals;
28b274bc22SAndre Fischeruse Time::HiRes qw(gettimeofday tv_interval);
29b274bc22SAndre Fischeruse English;
30b274bc22SAndre Fischeruse IO::Handle;
31b274bc22SAndre Fischeruse strict;
32b274bc22SAndre Fischer
33b274bc22SAndre Fischermy $StartTime = undef;
34b274bc22SAndre Fischer
3587d0bf7aSAndre Fischersub PrintStackTrace ();
3687d0bf7aSAndre Fischersub Die ($);
370374af79SAndre Fischer
38b274bc22SAndre Fischer=head1 NAME
39b274bc22SAndre Fischer
40b274bc22SAndre Fischer    installer::logger
41b274bc22SAndre Fischer
42b274bc22SAndre Fischer    Logging for the installer modules.
43b274bc22SAndre Fischer
44b274bc22SAndre Fischer=cut
45b274bc22SAndre Fischer
46b274bc22SAndre Fischer=head1 DESCRIPTION
47b274bc22SAndre Fischer
48b274bc22SAndre Fischer    This module is in a transition state from a set of loosly connected functions to a single class.
49b274bc22SAndre Fischer
50b274bc22SAndre Fischer    There are three globaly available logger objects:
51b274bc22SAndre Fischer
52b274bc22SAndre Fischer=over
53b274bc22SAndre Fischer
54b274bc22SAndre Fischer=item $Lang
55b274bc22SAndre Fischer
56b274bc22SAndre Fischer    is language specific and writes messages to a log file.
57b274bc22SAndre Fischer
58b274bc22SAndre Fischer=cut
59b274bc22SAndre Fischer
60b274bc22SAndre Fischer=item $Glob
61b274bc22SAndre Fischer
62b274bc22SAndre Fischer    is independent of the current language.  Its messages are prepended to each $Lang logger.
63b274bc22SAndre Fischer
64b274bc22SAndre Fischer=cut
65b274bc22SAndre Fischer
66b274bc22SAndre Fischer=item $Info
67b274bc22SAndre Fischer
68b274bc22SAndre Fischer    is for output to the console.
69b274bc22SAndre Fischer
70b274bc22SAndre Fischer=cut
71b274bc22SAndre Fischer
72b274bc22SAndre Fischer=back
73b274bc22SAndre Fischer
74b274bc22SAndre Fischer=cut
75b274bc22SAndre Fischer
76b274bc22SAndre Fischer
77b274bc22SAndre Fischerour $Global = installer::logger->new("glob",
78b274bc22SAndre Fischer    'is_save_lines' => 1,
79b274bc22SAndre Fischer    'is_print_to_console' => 0,
80b274bc22SAndre Fischer    'is_show_relative_time' => 1);
81b274bc22SAndre Fischerour $Lang = installer::logger->new("lang",
82b274bc22SAndre Fischer    'is_print_to_console' => 0,
83b274bc22SAndre Fischer    'is_show_relative_time' => 1,
84b274bc22SAndre Fischer    'is_show_log_id' => 1
85b274bc22SAndre Fischer    );
86b274bc22SAndre Fischerour $Info = installer::logger->new("info",
87b274bc22SAndre Fischer    'is_show_relative_time' => 0,
88b274bc22SAndre Fischer    'is_show_process_id' => 0,
89b274bc22SAndre Fischer    'is_show_log_id' => 0
90b274bc22SAndre Fischer    );
91b274bc22SAndre Fischer
92*2f2de527SAndre Fischer
93*2f2de527SAndre Fischer
94*2f2de527SAndre Fischer=head2 SetupSimpleLogging ($filename)
95*2f2de527SAndre Fischer
96*2f2de527SAndre Fischer    Setup logging so that $Global, $Lang and $Info all print to the console AND to the log file.
97*2f2de527SAndre Fischer
98*2f2de527SAndre Fischer=cut
99*2f2de527SAndre Fischersub SetupSimpleLogging ($)
100*2f2de527SAndre Fischer{
101*2f2de527SAndre Fischer    my ($log_filename) = @_;
102*2f2de527SAndre Fischer
103*2f2de527SAndre Fischer    $Info = installer::logger->new("info",
104*2f2de527SAndre Fischer        'is_print_to_console' => 1,
105*2f2de527SAndre Fischer        'is_show_relative_time' => 1,
106*2f2de527SAndre Fischer        );
107*2f2de527SAndre Fischer    $Global = installer::logger->new("glob",
108*2f2de527SAndre Fischer        'is_print_to_console' => 0,
109*2f2de527SAndre Fischer        'is_show_relative_time' => 1,
110*2f2de527SAndre Fischer        'forward' => [$Info]
111*2f2de527SAndre Fischer        );
112*2f2de527SAndre Fischer    $Lang = installer::logger->new("lang",
113*2f2de527SAndre Fischer        'is_print_to_console' => 0,
114*2f2de527SAndre Fischer        'is_show_relative_time' => 1,
115*2f2de527SAndre Fischer        'forward' => [$Info]
116*2f2de527SAndre Fischer        );
117*2f2de527SAndre Fischer    $Info->set_filename($log_filename);
118*2f2de527SAndre Fischer    $Info->{'is_print_to_console'} = 1;
119*2f2de527SAndre Fischer    $installer::globals::quiet = 0;
120*2f2de527SAndre Fischer    starttime();
121*2f2de527SAndre Fischer}
122*2f2de527SAndre Fischer
123*2f2de527SAndre Fischer
124*2f2de527SAndre Fischer
125*2f2de527SAndre Fischer
126b274bc22SAndre Fischer=head2 new($class, $id, @arguments)
127b274bc22SAndre Fischer
128b274bc22SAndre Fischer    Create a new instance of the logger class.
129b274bc22SAndre Fischer    @arguments lets you override default values.
130b274bc22SAndre Fischer
131b274bc22SAndre Fischer=cut
132b274bc22SAndre Fischer
133b274bc22SAndre Fischersub new ($$@)
134b274bc22SAndre Fischer{
135b274bc22SAndre Fischer    my ($class, $id, @arguments) = @_;
136b274bc22SAndre Fischer
137b274bc22SAndre Fischer    my $self = {
138b274bc22SAndre Fischer        'id' => $id,
139b274bc22SAndre Fischer        'filename' => "",
140b274bc22SAndre Fischer        # When set then lines are printed to this file.
141b274bc22SAndre Fischer        'file' => undef,
142b274bc22SAndre Fischer        # When true then lines are printed to the console.
143b274bc22SAndre Fischer        'is_print_to_console' => 1,
144b274bc22SAndre Fischer        'is_save_lines' => 0,
145b274bc22SAndre Fischer        # A container of printed lines.  Lines are added only when 'is_save_lines' is true.
146b274bc22SAndre Fischer        'lines' => [],
147b274bc22SAndre Fischer        # Another logger to which all prints are forwarded.
148b274bc22SAndre Fischer        'forward' => [],
149b274bc22SAndre Fischer        # A filter function that for example can recoginze build errors.
150b274bc22SAndre Fischer        'filter' => undef,
151b274bc22SAndre Fischer        # Show relative time
152b274bc22SAndre Fischer        'is_show_relative_time' => 0,
153b274bc22SAndre Fischer        # Show log id (mostly for debugging the logger)
154b274bc22SAndre Fischer        'is_show_log_id' => 0,
155b274bc22SAndre Fischer        # Show the process id, useful on the console when doing a multiprocessor build.
156*2f2de527SAndre Fischer        'is_show_process_id' => 0,
157*2f2de527SAndre Fischer        # Current indentation
158*2f2de527SAndre Fischer        'indentation' => "",
159b274bc22SAndre Fischer    };
160b274bc22SAndre Fischer    while (scalar @arguments >= 2)
161b274bc22SAndre Fischer    {
162b274bc22SAndre Fischer        my $key = shift @arguments;
163b274bc22SAndre Fischer        my $value = shift @arguments;
164b274bc22SAndre Fischer        $self->{$key} = $value;
165b274bc22SAndre Fischer    }
166b274bc22SAndre Fischer
167b274bc22SAndre Fischer    bless($self, $class);
168b274bc22SAndre Fischer
169b274bc22SAndre Fischer    return $self;
170b274bc22SAndre Fischer}
171b274bc22SAndre Fischer
172b274bc22SAndre Fischer
173b274bc22SAndre Fischer
174b274bc22SAndre Fischer=head2 printf($self, $message, @arguments)
175b274bc22SAndre Fischer
176b274bc22SAndre Fischer    Identical in syntax and semantics to the usual perl (s)printf.
177b274bc22SAndre Fischer
178b274bc22SAndre Fischer=cut
179b274bc22SAndre Fischersub printf ($$@)
180b274bc22SAndre Fischer{
181b274bc22SAndre Fischer    my ($self, $format, @arguments) = @_;
182b274bc22SAndre Fischer
1839daef0aeSAndre Fischer    if ($format =~ /\%\{/)
18487d0bf7aSAndre Fischer    {
1859daef0aeSAndre Fischer        printf(">%s<\n", $format);
18687d0bf7aSAndre Fischer        PrintStackTrace();
18787d0bf7aSAndre Fischer    }
1889daef0aeSAndre Fischer    my $message = sprintf($format, @arguments);
18987d0bf7aSAndre Fischer    $self->print($message, 0);
190b274bc22SAndre Fischer}
191b274bc22SAndre Fischer
192b274bc22SAndre Fischer
193b274bc22SAndre Fischer
194b274bc22SAndre Fischer
195b274bc22SAndre Fischer=head2 print ($self, $message, [optional] $force)
196b274bc22SAndre Fischer
197b274bc22SAndre Fischer    Print the given message.
198b274bc22SAndre Fischer    If the optional $force parameter is given and it evaluates to true then the message
199b274bc22SAndre Fischer    is printed even when the golbal $installer::globals::quiet is true.
200b274bc22SAndre Fischer
201b274bc22SAndre Fischer=cut
202b274bc22SAndre Fischersub print ($$;$)
203b274bc22SAndre Fischer{
204b274bc22SAndre Fischer    my ($self, $message, $force) = @_;
205b274bc22SAndre Fischer
2060374af79SAndre Fischer    Die "newline at start of line" if ($message =~ /^\n.+/);
207b274bc22SAndre Fischer
208b274bc22SAndre Fischer    $force = 0 unless defined $force;
209b274bc22SAndre Fischer
210b274bc22SAndre Fischer    my $relative_time = tv_interval($StartTime, [gettimeofday()]);
211b274bc22SAndre Fischer    foreach my $target ($self, @{$self->{'forward'}})
212b274bc22SAndre Fischer    {
213b274bc22SAndre Fischer        $target->process_line(
214b274bc22SAndre Fischer            $relative_time,
215b274bc22SAndre Fischer            $self->{'id'},
216b274bc22SAndre Fischer            $PID,
217b274bc22SAndre Fischer            $message,
218b274bc22SAndre Fischer            $force);
219b274bc22SAndre Fischer    }
220b274bc22SAndre Fischer}
221b274bc22SAndre Fischer
222b274bc22SAndre Fischer
223b274bc22SAndre Fischer
224b274bc22SAndre Fischer
225b274bc22SAndre Fischer=head2 process_line ($self, $relative_time, $log_id, $pid, $message, $force)
226b274bc22SAndre Fischer
227b274bc22SAndre Fischer    Internal function that decides whether to
228b274bc22SAndre Fischer    a) write to a log file
229b274bc22SAndre Fischer    b) print to the console
230b274bc22SAndre Fischer    c) store in an array for later use
231b274bc22SAndre Fischer    the preformatted message.
232b274bc22SAndre Fischer
233b274bc22SAndre Fischer=cut
234b274bc22SAndre Fischersub process_line ($$$$$$)
235b274bc22SAndre Fischer{
236b274bc22SAndre Fischer    my ($self, $relative_time, $log_id, $pid, $message, $force) = @_;
237b274bc22SAndre Fischer
238b274bc22SAndre Fischer    # Apply the line filter.
239b274bc22SAndre Fischer    if (defined $self->{'filter'})
240b274bc22SAndre Fischer    {
241b274bc22SAndre Fischer        $message = &{$self->{'filter'}}($relative_time, $log_id, $pid, $message);
242b274bc22SAndre Fischer    }
243b274bc22SAndre Fischer
244b274bc22SAndre Fischer    # Format the line.
245b274bc22SAndre Fischer    my $line = "";
246b274bc22SAndre Fischer    if ($self->{'is_show_relative_time'})
247b274bc22SAndre Fischer    {
248b274bc22SAndre Fischer        $line .= sprintf("%12.6f : ", $relative_time);
249b274bc22SAndre Fischer    }
250b274bc22SAndre Fischer    if ($self->{'is_show_log_id'})
251b274bc22SAndre Fischer    {
252b274bc22SAndre Fischer        $line .= $log_id . " : ";
253b274bc22SAndre Fischer    }
254b274bc22SAndre Fischer    if ($self->{'is_show_process_id'})
255b274bc22SAndre Fischer    {
256b274bc22SAndre Fischer        $line .= $pid . " : ";
257b274bc22SAndre Fischer    }
258*2f2de527SAndre Fischer    $line .= $self->{'indentation'};
259b274bc22SAndre Fischer    $line .= $message;
260b274bc22SAndre Fischer
261b274bc22SAndre Fischer    # Print the line to a file or to the console or store it for later use.
262b274bc22SAndre Fischer    my $fid = $self->{'file'};
263b274bc22SAndre Fischer    if (defined $fid)
264b274bc22SAndre Fischer    {
265b274bc22SAndre Fischer        print $fid ($line);
266b274bc22SAndre Fischer    }
267b274bc22SAndre Fischer    if (($force || ! $installer::globals::quiet)
268b274bc22SAndre Fischer        && $self->{'is_print_to_console'})
269b274bc22SAndre Fischer    {
270b274bc22SAndre Fischer        print($line);
271b274bc22SAndre Fischer    }
272b274bc22SAndre Fischer    if ($self->{'is_save_lines'})
273b274bc22SAndre Fischer    {
274b274bc22SAndre Fischer        push @{$self->{'lines'}}, [$relative_time, $log_id, $pid, $message, $force];
275b274bc22SAndre Fischer    }
276b274bc22SAndre Fischer}
277b274bc22SAndre Fischer
278b274bc22SAndre Fischer
279b274bc22SAndre Fischer
280b274bc22SAndre Fischer
281b274bc22SAndre Fischer=head2 set_filename (Self, $filename)
282b274bc22SAndre Fischer
283b274bc22SAndre Fischer    When the name of a writable file is given then all future messages will go to that file.
284b274bc22SAndre Fischer    Output to the console is turned off.
285b274bc22SAndre Fischer    This method is typically used to tie the language dependent $Lang logger to different log files.
286b274bc22SAndre Fischer
287b274bc22SAndre Fischer=cut
288b274bc22SAndre Fischersub set_filename ($$)
289b274bc22SAndre Fischer{
290b274bc22SAndre Fischer    my ($self, $filename) = @_;
291b274bc22SAndre Fischer
292b274bc22SAndre Fischer    $filename = "" unless defined $filename;
293b274bc22SAndre Fischer    if ($self->{'filename'} ne $filename)
294b274bc22SAndre Fischer    {
295b274bc22SAndre Fischer        if (defined $self->{'file'})
296b274bc22SAndre Fischer        {
297b274bc22SAndre Fischer            $self->{'is_print_to_console'} = 1;
298b274bc22SAndre Fischer            close $self->{'file'};
299b274bc22SAndre Fischer            $self->{'file'} = undef;
300b274bc22SAndre Fischer        }
301b274bc22SAndre Fischer
302b274bc22SAndre Fischer        $self->{'filename'} = $filename;
303b274bc22SAndre Fischer
304b274bc22SAndre Fischer        if ($filename ne "")
305b274bc22SAndre Fischer        {
306b274bc22SAndre Fischer            open $self->{'file'}, ">", $self->{'filename'}
3070374af79SAndre Fischer            || Die "can not open log file ".$self->{'filename'}." for writing";
308b274bc22SAndre Fischer            $self->{'is_print_to_console'} = 0;
309b274bc22SAndre Fischer
310b274bc22SAndre Fischer            # Make all writes synchronous so that we don't loose any messages on an
311b274bc22SAndre Fischer            # 'abrupt' end.
312b274bc22SAndre Fischer            my $handle = select $self->{'file'};
313b274bc22SAndre Fischer            $| = 1;
314b274bc22SAndre Fischer            select $handle;
315b274bc22SAndre Fischer        }
316b274bc22SAndre Fischer    }
317b274bc22SAndre Fischer}
318b274bc22SAndre Fischer
319b274bc22SAndre Fischer
320b274bc22SAndre Fischer
321b274bc22SAndre Fischer
322b274bc22SAndre Fischer=head2 set_filter ($self, $filter)
323b274bc22SAndre Fischer
324b274bc22SAndre Fischer    Sets $filter (a function reference) as line filter.  It is applied to each line.
325b274bc22SAndre Fischer    The filter can extract information from the given message and modify it before it is printed.
326b274bc22SAndre Fischer
327b274bc22SAndre Fischer=cut
328b274bc22SAndre Fischersub set_filter ($$)
329b274bc22SAndre Fischer{
330b274bc22SAndre Fischer    my ($self, $filter) = @_;
331b274bc22SAndre Fischer    $self->{'filter'} = $filter;
332b274bc22SAndre Fischer}
333b274bc22SAndre Fischer
334b274bc22SAndre Fischer
335b274bc22SAndre Fischer
336b274bc22SAndre Fischer
337b274bc22SAndre Fischer=head2 add_timestamp ($self, $message)
338b274bc22SAndre Fischer
339b274bc22SAndre Fischer    Print the given message together with the current (absolute) time.
340b274bc22SAndre Fischer
341b274bc22SAndre Fischer=cut
342b274bc22SAndre Fischersub add_timestamp ($$)
343b274bc22SAndre Fischer{
344b274bc22SAndre Fischer	my ($self, $message) = @_;
345b274bc22SAndre Fischer
346b274bc22SAndre Fischer	my $timestring = get_time_string();
347b274bc22SAndre Fischer    $self->printf("%s\t%s", $message, $timestring);
348b274bc22SAndre Fischer}
349b274bc22SAndre Fischer
350b274bc22SAndre Fischer
351b274bc22SAndre Fischer
352b274bc22SAndre Fischer=head2 copy_lines_from ($self, $other)
353b274bc22SAndre Fischer
354b274bc22SAndre Fischer    Copy saved lines from another logger object.
355b274bc22SAndre Fischer
356b274bc22SAndre Fischer=cut
357b274bc22SAndre Fischersub copy_lines_from ($$)
358b274bc22SAndre Fischer{
359b274bc22SAndre Fischer    my ($self, $other) = @_;
360b274bc22SAndre Fischer
361b274bc22SAndre Fischer    my $is_print_to_console = $self->{'is_print_to_console'};
362b274bc22SAndre Fischer    my $is_save_lines = $self->{'is_save_lines'};
363b274bc22SAndre Fischer    my $fid = $self->{'file'};
364b274bc22SAndre Fischer
365b274bc22SAndre Fischer    foreach my $line (@{$other->{'lines'}})
366b274bc22SAndre Fischer    {
367b274bc22SAndre Fischer        $self->process_line(@$line);
368b274bc22SAndre Fischer    }
369b274bc22SAndre Fischer}
370b274bc22SAndre Fischer
371b274bc22SAndre Fischer
372b274bc22SAndre Fischer
373b274bc22SAndre Fischer
374b274bc22SAndre Fischer=head2 set_forward ($self, $other)
375b274bc22SAndre Fischer
376b274bc22SAndre Fischer    Set a forwarding target.  All future messages are forwarded (copied) to $other.
377b274bc22SAndre Fischer    A typical use is to tie $Info to $Lang so that all messages sent to $Info are
378b274bc22SAndre Fischer    printed to the console AND written to the log file.
379b274bc22SAndre Fischer
380b274bc22SAndre Fischer=cut
381b274bc22SAndre Fischersub set_forward ($$)
382b274bc22SAndre Fischer{
383b274bc22SAndre Fischer    my ($self, $other) = @_;
384b274bc22SAndre Fischer
385b274bc22SAndre Fischer    # At the moment at most one forward target is allowed.
386b274bc22SAndre Fischer    if (defined $other)
387b274bc22SAndre Fischer    {
388b274bc22SAndre Fischer        $self->{'forward'} = [$other];
389b274bc22SAndre Fischer    }
390b274bc22SAndre Fischer    else
391b274bc22SAndre Fischer    {
392b274bc22SAndre Fischer        $self->{'forward'} = [];
393b274bc22SAndre Fischer    }
394b274bc22SAndre Fischer}
395b274bc22SAndre Fischer
396b274bc22SAndre Fischer
397b274bc22SAndre Fischer
398cdf0e10cSrcweir
399*2f2de527SAndre Fischersub increase_indentation ($)
400*2f2de527SAndre Fischer{
401*2f2de527SAndre Fischer    my ($self) = @_;
402*2f2de527SAndre Fischer    $self->{'indentation'} .= "    ";
403*2f2de527SAndre Fischer}
404*2f2de527SAndre Fischer
405*2f2de527SAndre Fischer
406*2f2de527SAndre Fischer
407*2f2de527SAndre Fischer
408*2f2de527SAndre Fischersub decrease_indentation ($)
409*2f2de527SAndre Fischer{
410*2f2de527SAndre Fischer    my ($self) = @_;
411*2f2de527SAndre Fischer    $self->{'indentation'} = substr($self->{'indentation'}, 4);
412*2f2de527SAndre Fischer}
413*2f2de527SAndre Fischer
414*2f2de527SAndre Fischer
415*2f2de527SAndre Fischer
416*2f2de527SAndre Fischer
417cdf0e10cSrcweir####################################################
418cdf0e10cSrcweir# Including header files into the logfile
419cdf0e10cSrcweir####################################################
420cdf0e10cSrcweir
421cdf0e10cSrcweirsub include_header_into_logfile
422cdf0e10cSrcweir{
423cdf0e10cSrcweir	my ($message) = @_;
424cdf0e10cSrcweir
425b274bc22SAndre Fischer	$Lang->print("\n");
426b274bc22SAndre Fischer	$Lang->print(get_time_string());
427b274bc22SAndre Fischer	$Lang->print("######################################################\n");
428b274bc22SAndre Fischer	$Lang->print($message."\n");
429b274bc22SAndre Fischer	$Lang->print("######################################################\n");
430cdf0e10cSrcweir}
431cdf0e10cSrcweir
432cdf0e10cSrcweir####################################################
433cdf0e10cSrcweir# Including header files into the logfile
434cdf0e10cSrcweir####################################################
435cdf0e10cSrcweir
436cdf0e10cSrcweirsub include_header_into_globallogfile
437cdf0e10cSrcweir{
438cdf0e10cSrcweir	my ($message) = @_;
439cdf0e10cSrcweir
440b274bc22SAndre Fischer	$Global->print("\n");
441b274bc22SAndre Fischer	$Global->print(get_time_string());
442b274bc22SAndre Fischer	$Global->print("######################################################\n");
443b274bc22SAndre Fischer	$Global->print($message."\n");
444b274bc22SAndre Fischer	$Global->print("######################################################\n");
445cdf0e10cSrcweir}
446cdf0e10cSrcweir
447cdf0e10cSrcweir####################################################
448cdf0e10cSrcweir# Write timestamp into log file
449cdf0e10cSrcweir####################################################
450cdf0e10cSrcweir
451cdf0e10cSrcweirsub include_timestamp_into_logfile
452cdf0e10cSrcweir{
4530374af79SAndre Fischer    Die "deprected";
454cdf0e10cSrcweir	my ($message) = @_;
455cdf0e10cSrcweir
456cdf0e10cSrcweir	my $infoline;
457cdf0e10cSrcweir	my $timestring = get_time_string();
458b274bc22SAndre Fischer    $Lang->printf("%s\t%s", $message, $timestring);
459cdf0e10cSrcweir}
460cdf0e10cSrcweir
461cdf0e10cSrcweir####################################################
462cdf0e10cSrcweir# Writing all variables content into the log file
463cdf0e10cSrcweir####################################################
464cdf0e10cSrcweir
465cdf0e10cSrcweirsub log_hashref
466cdf0e10cSrcweir{
467cdf0e10cSrcweir	my ($hashref) = @_;
468cdf0e10cSrcweir
469b274bc22SAndre Fischer    $Global->print("\n");
470b274bc22SAndre Fischer    $Global->print("Logging variable settings:\n");
471cdf0e10cSrcweir
472cdf0e10cSrcweir	my $itemkey;
473cdf0e10cSrcweir
474cdf0e10cSrcweir	foreach $itemkey ( keys %{$hashref} )
475cdf0e10cSrcweir	{
476cdf0e10cSrcweir		my $line = "";
477cdf0e10cSrcweir		my $itemvalue = "";
478cdf0e10cSrcweir		if ( $hashref->{$itemkey} ) { $itemvalue = $hashref->{$itemkey}; }
479b274bc22SAndre Fischer        $Global->printf("%s=%s\n", $itemkey, $itemvalue);
480cdf0e10cSrcweir	}
481cdf0e10cSrcweir
482b274bc22SAndre Fischer    $Global->print("\n");
483cdf0e10cSrcweir}
484cdf0e10cSrcweir
485cdf0e10cSrcweir#########################################################
486cdf0e10cSrcweir# Including global logging info into global log array
487cdf0e10cSrcweir#########################################################
488cdf0e10cSrcweir
489cdf0e10cSrcweirsub globallog
490cdf0e10cSrcweir{
491cdf0e10cSrcweir	my ($message) = @_;
492cdf0e10cSrcweir
493cdf0e10cSrcweir	my $infoline;
494cdf0e10cSrcweir
495b274bc22SAndre Fischer    $Global->print("\n");
496b274bc22SAndre Fischer    $Global->print(get_time_string());
497b274bc22SAndre Fischer    $Global->print("################################################################\n");
498b274bc22SAndre Fischer    $Global->print($message."\n");
499b274bc22SAndre Fischer    $Global->print("################################################################\n");
500cdf0e10cSrcweir}
501cdf0e10cSrcweir
502cdf0e10cSrcweir###############################################################
503cdf0e10cSrcweir# For each product (new language) a new log file is created.
504cdf0e10cSrcweir# Therefore the global logging has to be saved in this file.
505cdf0e10cSrcweir###############################################################
506cdf0e10cSrcweir
507cdf0e10cSrcweirsub copy_globalinfo_into_logfile
508cdf0e10cSrcweir{
509cdf0e10cSrcweir	for ( my $i = 0; $i <= $#installer::globals::globallogfileinfo; $i++ )
510cdf0e10cSrcweir	{
511cdf0e10cSrcweir		push(@installer::globals::logfileinfo, $installer::globals::globallogfileinfo[$i]);
512cdf0e10cSrcweir	}
513cdf0e10cSrcweir}
514cdf0e10cSrcweir
515cdf0e10cSrcweir###############################################################
516cdf0e10cSrcweir# For each product (new language) a new log file is created.
517cdf0e10cSrcweir# Therefore the global logging has to be saved in this file.
518cdf0e10cSrcweir###############################################################
519cdf0e10cSrcweir
520cdf0e10cSrcweirsub debuginfo
521cdf0e10cSrcweir{
522cdf0e10cSrcweir	my  ( $message ) = @_;
523cdf0e10cSrcweir
524cdf0e10cSrcweir	$message = $message . "\n";
525cdf0e10cSrcweir	push(@installer::globals::functioncalls, $message);
526cdf0e10cSrcweir}
527cdf0e10cSrcweir
528cdf0e10cSrcweir###############################################################
529cdf0e10cSrcweir# Saving the debug information.
530cdf0e10cSrcweir###############################################################
531cdf0e10cSrcweir
532cdf0e10cSrcweirsub savedebug
533cdf0e10cSrcweir{
534cdf0e10cSrcweir	my ( $outputdir ) = @_;
535cdf0e10cSrcweir
536cdf0e10cSrcweir	installer::files::save_file($outputdir . $installer::globals::debugfilename, \@installer::globals::functioncalls);
537cdf0e10cSrcweir	print_message( "... writing debug file " . $outputdir . $installer::globals::debugfilename . "\n" );
538cdf0e10cSrcweir}
539cdf0e10cSrcweir
540cdf0e10cSrcweir###############################################################
541cdf0e10cSrcweir# Starting the time
542cdf0e10cSrcweir###############################################################
543cdf0e10cSrcweir
544cdf0e10cSrcweirsub starttime
545cdf0e10cSrcweir{
546cdf0e10cSrcweir	$installer::globals::starttime = time();
547b274bc22SAndre Fischer	$StartTime = [gettimeofday()];
548b274bc22SAndre Fischer
549b274bc22SAndre Fischer    my $localtime = localtime();
550cdf0e10cSrcweir}
551cdf0e10cSrcweir
552cdf0e10cSrcweir###############################################################
553cdf0e10cSrcweir# Convert time string
554cdf0e10cSrcweir###############################################################
555cdf0e10cSrcweir
556cdf0e10cSrcweirsub convert_timestring
557cdf0e10cSrcweir{
558cdf0e10cSrcweir	my ($secondstring) = @_;
559cdf0e10cSrcweir
560cdf0e10cSrcweir	my $timestring = "";
561cdf0e10cSrcweir
562cdf0e10cSrcweir	if ( $secondstring < 60 )	 # less than a minute
563cdf0e10cSrcweir	{
564cdf0e10cSrcweir		if ( $secondstring < 10 ) { $secondstring = "0" . $secondstring; }
565cdf0e10cSrcweir		$timestring = "00\:$secondstring min\.";
566cdf0e10cSrcweir	}
567cdf0e10cSrcweir	elsif ( $secondstring < 3600 )
568cdf0e10cSrcweir	{
569cdf0e10cSrcweir		my $minutes = $secondstring / 60;
570cdf0e10cSrcweir		my $seconds = $secondstring % 60;
571cdf0e10cSrcweir		if ( $minutes =~ /(\d*)\.\d*/ ) { $minutes = $1; }
572cdf0e10cSrcweir		if ( $minutes < 10 ) { $minutes = "0" . $minutes; }
573cdf0e10cSrcweir		if ( $seconds < 10 ) { $seconds = "0" . $seconds; }
574cdf0e10cSrcweir		$timestring = "$minutes\:$seconds min\.";
575cdf0e10cSrcweir	}
576cdf0e10cSrcweir	else	# more than one hour
577cdf0e10cSrcweir	{
578cdf0e10cSrcweir		my $hours = $secondstring / 3600;
579cdf0e10cSrcweir		my $secondstring = $secondstring % 3600;
580cdf0e10cSrcweir		my $minutes = $secondstring / 60;
581cdf0e10cSrcweir		my $seconds = $secondstring % 60;
582cdf0e10cSrcweir		if ( $hours =~ /(\d*)\.\d*/ ) { $hours = $1; }
583cdf0e10cSrcweir		if ( $minutes =~ /(\d*)\.\d*/ ) { $minutes = $1; }
584cdf0e10cSrcweir		if ( $hours < 10 ) { $hours = "0" . $hours; }
585cdf0e10cSrcweir		if ( $minutes < 10 ) { $minutes = "0" . $minutes; }
586cdf0e10cSrcweir		if ( $seconds < 10 ) { $seconds = "0" . $seconds; }
587cdf0e10cSrcweir		$timestring = "$hours\:$minutes\:$seconds hours";
588cdf0e10cSrcweir	}
589cdf0e10cSrcweir
590cdf0e10cSrcweir	return $timestring;
591cdf0e10cSrcweir}
592cdf0e10cSrcweir
593cdf0e10cSrcweir###############################################################
594cdf0e10cSrcweir# Returning time string for logging
595cdf0e10cSrcweir###############################################################
596cdf0e10cSrcweir
597cdf0e10cSrcweirsub get_time_string
598cdf0e10cSrcweir{
599cdf0e10cSrcweir	my $currenttime = time();
600cdf0e10cSrcweir	$currenttime = $currenttime - $installer::globals::starttime;
601cdf0e10cSrcweir	$currenttime = convert_timestring($currenttime);
602cdf0e10cSrcweir	$currenttime = localtime() . " \(" . $currenttime . "\)\n";
603cdf0e10cSrcweir	return $currenttime;
604cdf0e10cSrcweir}
605cdf0e10cSrcweir
606cdf0e10cSrcweir###############################################################
607cdf0e10cSrcweir# Returning the age of a file (in seconds)
608cdf0e10cSrcweir###############################################################
609cdf0e10cSrcweir
610cdf0e10cSrcweirsub get_file_age
611cdf0e10cSrcweir{
612cdf0e10cSrcweir	my ( $filename ) = @_;
613cdf0e10cSrcweir
614cdf0e10cSrcweir	my $filetime = (stat($filename))[9];
615cdf0e10cSrcweir	my $timediff = time() - $filetime;
616cdf0e10cSrcweir	return $timediff;
617cdf0e10cSrcweir}
618cdf0e10cSrcweir
619cdf0e10cSrcweir###############################################################
620cdf0e10cSrcweir# Stopping the time
621cdf0e10cSrcweir###############################################################
622cdf0e10cSrcweir
623cdf0e10cSrcweirsub stoptime
624cdf0e10cSrcweir{
625b274bc22SAndre Fischer    my $localtime = localtime();
626b274bc22SAndre Fischer    $Info->printf("stopping log at %s\n", $localtime);
627cdf0e10cSrcweir}
628cdf0e10cSrcweir
629cdf0e10cSrcweir###############################################################
630cdf0e10cSrcweir# Set date string, format: yymmdd
631cdf0e10cSrcweir###############################################################
632cdf0e10cSrcweir
633cdf0e10cSrcweirsub set_installation_date
634cdf0e10cSrcweir{
635cdf0e10cSrcweir	my $datestring = "";
636cdf0e10cSrcweir
637cdf0e10cSrcweir	my @timearray = localtime(time);
638cdf0e10cSrcweir
639cdf0e10cSrcweir	my $day = $timearray[3];
640cdf0e10cSrcweir	my $month = $timearray[4] + 1;
641cdf0e10cSrcweir	my $year = $timearray[5] - 100;
642cdf0e10cSrcweir
643cdf0e10cSrcweir	if ( $year < 10 ) { $year = "0" . $year; }
644cdf0e10cSrcweir	if ( $month < 10 ) { $month = "0" . $month; }
645cdf0e10cSrcweir	if ( $day < 10 ) { $day = "0" . $day; }
646cdf0e10cSrcweir
647cdf0e10cSrcweir	$datestring = $year . $month . $day;
648cdf0e10cSrcweir
649cdf0e10cSrcweir	return $datestring;
650cdf0e10cSrcweir}
651cdf0e10cSrcweir
652cdf0e10cSrcweir###############################################################
653cdf0e10cSrcweir# Console output: messages
654cdf0e10cSrcweir###############################################################
655cdf0e10cSrcweir
656cdf0e10cSrcweirsub print_message
657cdf0e10cSrcweir{
6580374af79SAndre Fischer    Die "print_message is deprecated";
659b274bc22SAndre Fischer
660cdf0e10cSrcweir    my $message = shift;
661cdf0e10cSrcweir    chomp $message;
662cdf0e10cSrcweir    my $force = shift || 0;
663cdf0e10cSrcweir    print "$message\n" if ( $force || ! $installer::globals::quiet );
664cdf0e10cSrcweir    return;
665cdf0e10cSrcweir}
666cdf0e10cSrcweir
667cdf0e10cSrcweirsub print_message_without_newline
668cdf0e10cSrcweir{
669cdf0e10cSrcweir    my $message = shift;
670cdf0e10cSrcweir    chomp $message;
671cdf0e10cSrcweir    print "$message" if ( ! $installer::globals::quiet );
672cdf0e10cSrcweir    return;
673cdf0e10cSrcweir}
674cdf0e10cSrcweir
675cdf0e10cSrcweir###############################################################
676cdf0e10cSrcweir# Console output: warnings
677cdf0e10cSrcweir###############################################################
678cdf0e10cSrcweir
679cdf0e10cSrcweirsub print_warning
680cdf0e10cSrcweir{
681cdf0e10cSrcweir    my $message = shift;
682cdf0e10cSrcweir    chomp $message;
683cdf0e10cSrcweir    print STDERR "WARNING: $message";
684cdf0e10cSrcweir    return;
685cdf0e10cSrcweir}
686cdf0e10cSrcweir
687cdf0e10cSrcweir###############################################################
688cdf0e10cSrcweir# Console output: errors
689cdf0e10cSrcweir###############################################################
690cdf0e10cSrcweir
691cdf0e10cSrcweirsub print_error
692cdf0e10cSrcweir{
693cdf0e10cSrcweir    my $message = shift;
694cdf0e10cSrcweir    chomp $message;
695*2f2de527SAndre Fischer
696*2f2de527SAndre Fischer    PrintError($message);
697*2f2de527SAndre Fischer
698b274bc22SAndre Fischer    print STDERR "\n";
699b274bc22SAndre Fischer    print STDERR "**************************************************\n";
700cdf0e10cSrcweir    print STDERR "ERROR: $message";
701b274bc22SAndre Fischer    print STDERR "\n";
702b274bc22SAndre Fischer    print STDERR "**************************************************\n";
703cdf0e10cSrcweir    return;
704cdf0e10cSrcweir}
705cdf0e10cSrcweir
70687d0bf7aSAndre Fischer
707*2f2de527SAndre Fischer
708*2f2de527SAndre Fischer
709*2f2de527SAndre Fischersub PrintError ($@)
710*2f2de527SAndre Fischer{
711*2f2de527SAndre Fischer    my ($format, @arguments) = @_;
712*2f2de527SAndre Fischer
713*2f2de527SAndre Fischer    $Info->printf("Error: ".$format, @arguments);
714*2f2de527SAndre Fischer}
715*2f2de527SAndre Fischer
716*2f2de527SAndre Fischer
717*2f2de527SAndre Fischer
718*2f2de527SAndre Fischer
71987d0bf7aSAndre Fischer=head2 PrintStackTrace()
72087d0bf7aSAndre Fischer    This is for debugging the print and printf methods of the logger class and their use.
72187d0bf7aSAndre Fischer    Therefore we use the Perl print/printf directly and not the logger methods to avoid loops in case of errors.
72287d0bf7aSAndre Fischer=cut
72387d0bf7aSAndre Fischersub PrintStackTrace ()
72487d0bf7aSAndre Fischer{
72587d0bf7aSAndre Fischer    print "Stack Trace:\n";
72687d0bf7aSAndre Fischer    my $i = 1;
72787d0bf7aSAndre Fischer    while ((my @call_details = (caller($i++))))
72887d0bf7aSAndre Fischer    {
72987d0bf7aSAndre Fischer        printf("%s:%s in function %s\n", $call_details[1], $call_details[2], $call_details[3]);
73087d0bf7aSAndre Fischer    }
73187d0bf7aSAndre Fischer}
73287d0bf7aSAndre Fischer
7339daef0aeSAndre Fischer
73487d0bf7aSAndre Fischersub Die ($)
73587d0bf7aSAndre Fischer{
73687d0bf7aSAndre Fischer    my ($message) = @_;
73787d0bf7aSAndre Fischer    PrintStackTrace();
73887d0bf7aSAndre Fischer    die $message;
73987d0bf7aSAndre Fischer}
74087d0bf7aSAndre Fischer
74187d0bf7aSAndre Fischer
74287d0bf7aSAndre Fischer
743cdf0e10cSrcweir1;
744