xref: /AOO41X/main/solenv/bin/modules/installer/logger.pm (revision 9daef0aea08ed9e23d1d5d6179b1211574e5fa13)
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
92b274bc22SAndre Fischer=head2 new($class, $id, @arguments)
93b274bc22SAndre Fischer
94b274bc22SAndre Fischer    Create a new instance of the logger class.
95b274bc22SAndre Fischer    @arguments lets you override default values.
96b274bc22SAndre Fischer
97b274bc22SAndre Fischer=cut
98b274bc22SAndre Fischer
99b274bc22SAndre Fischersub new ($$@)
100b274bc22SAndre Fischer{
101b274bc22SAndre Fischer    my ($class, $id, @arguments) = @_;
102b274bc22SAndre Fischer
103b274bc22SAndre Fischer    my $self = {
104b274bc22SAndre Fischer        'id' => $id,
105b274bc22SAndre Fischer        'filename' => "",
106b274bc22SAndre Fischer        # When set then lines are printed to this file.
107b274bc22SAndre Fischer        'file' => undef,
108b274bc22SAndre Fischer        # When true then lines are printed to the console.
109b274bc22SAndre Fischer        'is_print_to_console' => 1,
110b274bc22SAndre Fischer        'is_save_lines' => 0,
111b274bc22SAndre Fischer        # A container of printed lines.  Lines are added only when 'is_save_lines' is true.
112b274bc22SAndre Fischer        'lines' => [],
113b274bc22SAndre Fischer        # Another logger to which all prints are forwarded.
114b274bc22SAndre Fischer        'forward' => [],
115b274bc22SAndre Fischer        # A filter function that for example can recoginze build errors.
116b274bc22SAndre Fischer        'filter' => undef,
117b274bc22SAndre Fischer        # Show relative time
118b274bc22SAndre Fischer        'is_show_relative_time' => 0,
119b274bc22SAndre Fischer        # Show log id (mostly for debugging the logger)
120b274bc22SAndre Fischer        'is_show_log_id' => 0,
121b274bc22SAndre Fischer        # Show the process id, useful on the console when doing a multiprocessor build.
122b274bc22SAndre Fischer        'is_show_process_id' => 0
123b274bc22SAndre Fischer    };
124b274bc22SAndre Fischer    while (scalar @arguments >= 2)
125b274bc22SAndre Fischer    {
126b274bc22SAndre Fischer        my $key = shift @arguments;
127b274bc22SAndre Fischer        my $value = shift @arguments;
128b274bc22SAndre Fischer        $self->{$key} = $value;
129b274bc22SAndre Fischer    }
130b274bc22SAndre Fischer
131b274bc22SAndre Fischer    bless($self, $class);
132b274bc22SAndre Fischer
133b274bc22SAndre Fischer    return $self;
134b274bc22SAndre Fischer}
135b274bc22SAndre Fischer
136b274bc22SAndre Fischer
137b274bc22SAndre Fischer
138b274bc22SAndre Fischer=head2 printf($self, $message, @arguments)
139b274bc22SAndre Fischer
140b274bc22SAndre Fischer    Identical in syntax and semantics to the usual perl (s)printf.
141b274bc22SAndre Fischer
142b274bc22SAndre Fischer=cut
143b274bc22SAndre Fischersub printf ($$@)
144b274bc22SAndre Fischer{
145b274bc22SAndre Fischer    my ($self, $format, @arguments) = @_;
146b274bc22SAndre Fischer
147*9daef0aeSAndre Fischer    if ($format =~ /\%\{/)
14887d0bf7aSAndre Fischer    {
149*9daef0aeSAndre Fischer        printf(">%s<\n", $format);
15087d0bf7aSAndre Fischer        PrintStackTrace();
15187d0bf7aSAndre Fischer    }
152*9daef0aeSAndre Fischer    my $message = sprintf($format, @arguments);
15387d0bf7aSAndre Fischer    $self->print($message, 0);
154b274bc22SAndre Fischer}
155b274bc22SAndre Fischer
156b274bc22SAndre Fischer
157b274bc22SAndre Fischer
158b274bc22SAndre Fischer
159b274bc22SAndre Fischer=head2 print ($self, $message, [optional] $force)
160b274bc22SAndre Fischer
161b274bc22SAndre Fischer    Print the given message.
162b274bc22SAndre Fischer    If the optional $force parameter is given and it evaluates to true then the message
163b274bc22SAndre Fischer    is printed even when the golbal $installer::globals::quiet is true.
164b274bc22SAndre Fischer
165b274bc22SAndre Fischer=cut
166b274bc22SAndre Fischersub print ($$;$)
167b274bc22SAndre Fischer{
168b274bc22SAndre Fischer    my ($self, $message, $force) = @_;
169b274bc22SAndre Fischer
1700374af79SAndre Fischer    Die "newline at start of line" if ($message =~ /^\n.+/);
171b274bc22SAndre Fischer
172b274bc22SAndre Fischer    $force = 0 unless defined $force;
173b274bc22SAndre Fischer
174b274bc22SAndre Fischer    my $relative_time = tv_interval($StartTime, [gettimeofday()]);
175b274bc22SAndre Fischer    foreach my $target ($self, @{$self->{'forward'}})
176b274bc22SAndre Fischer    {
177b274bc22SAndre Fischer        $target->process_line(
178b274bc22SAndre Fischer            $relative_time,
179b274bc22SAndre Fischer            $self->{'id'},
180b274bc22SAndre Fischer            $PID,
181b274bc22SAndre Fischer            $message,
182b274bc22SAndre Fischer            $force);
183b274bc22SAndre Fischer    }
184b274bc22SAndre Fischer}
185b274bc22SAndre Fischer
186b274bc22SAndre Fischer
187b274bc22SAndre Fischer
188b274bc22SAndre Fischer
189b274bc22SAndre Fischer=head2 process_line ($self, $relative_time, $log_id, $pid, $message, $force)
190b274bc22SAndre Fischer
191b274bc22SAndre Fischer    Internal function that decides whether to
192b274bc22SAndre Fischer    a) write to a log file
193b274bc22SAndre Fischer    b) print to the console
194b274bc22SAndre Fischer    c) store in an array for later use
195b274bc22SAndre Fischer    the preformatted message.
196b274bc22SAndre Fischer
197b274bc22SAndre Fischer=cut
198b274bc22SAndre Fischersub process_line ($$$$$$)
199b274bc22SAndre Fischer{
200b274bc22SAndre Fischer    my ($self, $relative_time, $log_id, $pid, $message, $force) = @_;
201b274bc22SAndre Fischer
202b274bc22SAndre Fischer    # Apply the line filter.
203b274bc22SAndre Fischer    if (defined $self->{'filter'})
204b274bc22SAndre Fischer    {
205b274bc22SAndre Fischer        $message = &{$self->{'filter'}}($relative_time, $log_id, $pid, $message);
206b274bc22SAndre Fischer    }
207b274bc22SAndre Fischer
208b274bc22SAndre Fischer    # Format the line.
209b274bc22SAndre Fischer    my $line = "";
210b274bc22SAndre Fischer    if ($self->{'is_show_relative_time'})
211b274bc22SAndre Fischer    {
212b274bc22SAndre Fischer        $line .= sprintf("%12.6f : ", $relative_time);
213b274bc22SAndre Fischer    }
214b274bc22SAndre Fischer    if ($self->{'is_show_log_id'})
215b274bc22SAndre Fischer    {
216b274bc22SAndre Fischer        $line .= $log_id . " : ";
217b274bc22SAndre Fischer    }
218b274bc22SAndre Fischer    if ($self->{'is_show_process_id'})
219b274bc22SAndre Fischer    {
220b274bc22SAndre Fischer        $line .= $pid . " : ";
221b274bc22SAndre Fischer    }
222b274bc22SAndre Fischer    $line .= $message;
223b274bc22SAndre Fischer
224b274bc22SAndre Fischer    # Print the line to a file or to the console or store it for later use.
225b274bc22SAndre Fischer    my $fid = $self->{'file'};
226b274bc22SAndre Fischer    if (defined $fid)
227b274bc22SAndre Fischer    {
228b274bc22SAndre Fischer        print $fid ($line);
229b274bc22SAndre Fischer    }
230b274bc22SAndre Fischer    if (($force || ! $installer::globals::quiet)
231b274bc22SAndre Fischer        && $self->{'is_print_to_console'})
232b274bc22SAndre Fischer    {
233b274bc22SAndre Fischer        print($line);
234b274bc22SAndre Fischer    }
235b274bc22SAndre Fischer    if ($self->{'is_save_lines'})
236b274bc22SAndre Fischer    {
237b274bc22SAndre Fischer        push @{$self->{'lines'}}, [$relative_time, $log_id, $pid, $message, $force];
238b274bc22SAndre Fischer    }
239b274bc22SAndre Fischer}
240b274bc22SAndre Fischer
241b274bc22SAndre Fischer
242b274bc22SAndre Fischer
243b274bc22SAndre Fischer
244b274bc22SAndre Fischer=head2 set_filename (Self, $filename)
245b274bc22SAndre Fischer
246b274bc22SAndre Fischer    When the name of a writable file is given then all future messages will go to that file.
247b274bc22SAndre Fischer    Output to the console is turned off.
248b274bc22SAndre Fischer    This method is typically used to tie the language dependent $Lang logger to different log files.
249b274bc22SAndre Fischer
250b274bc22SAndre Fischer=cut
251b274bc22SAndre Fischersub set_filename ($$)
252b274bc22SAndre Fischer{
253b274bc22SAndre Fischer    my ($self, $filename) = @_;
254b274bc22SAndre Fischer
255b274bc22SAndre Fischer    $filename = "" unless defined $filename;
256b274bc22SAndre Fischer    if ($self->{'filename'} ne $filename)
257b274bc22SAndre Fischer    {
258b274bc22SAndre Fischer        if (defined $self->{'file'})
259b274bc22SAndre Fischer        {
260b274bc22SAndre Fischer            $self->{'is_print_to_console'} = 1;
261b274bc22SAndre Fischer            close $self->{'file'};
262b274bc22SAndre Fischer            $self->{'file'} = undef;
263b274bc22SAndre Fischer        }
264b274bc22SAndre Fischer
265b274bc22SAndre Fischer        $self->{'filename'} = $filename;
266b274bc22SAndre Fischer
267b274bc22SAndre Fischer        if ($filename ne "")
268b274bc22SAndre Fischer        {
269b274bc22SAndre Fischer            open $self->{'file'}, ">", $self->{'filename'}
2700374af79SAndre Fischer            || Die "can not open log file ".$self->{'filename'}." for writing";
271b274bc22SAndre Fischer            $self->{'is_print_to_console'} = 0;
272b274bc22SAndre Fischer
273b274bc22SAndre Fischer            # Make all writes synchronous so that we don't loose any messages on an
274b274bc22SAndre Fischer            # 'abrupt' end.
275b274bc22SAndre Fischer            my $handle = select $self->{'file'};
276b274bc22SAndre Fischer            $| = 1;
277b274bc22SAndre Fischer            select $handle;
278b274bc22SAndre Fischer        }
279b274bc22SAndre Fischer    }
280b274bc22SAndre Fischer}
281b274bc22SAndre Fischer
282b274bc22SAndre Fischer
283b274bc22SAndre Fischer
284b274bc22SAndre Fischer
285b274bc22SAndre Fischer=head2 set_filter ($self, $filter)
286b274bc22SAndre Fischer
287b274bc22SAndre Fischer    Sets $filter (a function reference) as line filter.  It is applied to each line.
288b274bc22SAndre Fischer    The filter can extract information from the given message and modify it before it is printed.
289b274bc22SAndre Fischer
290b274bc22SAndre Fischer=cut
291b274bc22SAndre Fischersub set_filter ($$)
292b274bc22SAndre Fischer{
293b274bc22SAndre Fischer    my ($self, $filter) = @_;
294b274bc22SAndre Fischer    $self->{'filter'} = $filter;
295b274bc22SAndre Fischer}
296b274bc22SAndre Fischer
297b274bc22SAndre Fischer
298b274bc22SAndre Fischer
299b274bc22SAndre Fischer
300b274bc22SAndre Fischer=head2 add_timestamp ($self, $message)
301b274bc22SAndre Fischer
302b274bc22SAndre Fischer    Print the given message together with the current (absolute) time.
303b274bc22SAndre Fischer
304b274bc22SAndre Fischer=cut
305b274bc22SAndre Fischersub add_timestamp ($$)
306b274bc22SAndre Fischer{
307b274bc22SAndre Fischer	my ($self, $message) = @_;
308b274bc22SAndre Fischer
309b274bc22SAndre Fischer	my $timestring = get_time_string();
310b274bc22SAndre Fischer    $self->printf("%s\t%s", $message, $timestring);
311b274bc22SAndre Fischer}
312b274bc22SAndre Fischer
313b274bc22SAndre Fischer
314b274bc22SAndre Fischer
315b274bc22SAndre Fischer=head2 copy_lines_from ($self, $other)
316b274bc22SAndre Fischer
317b274bc22SAndre Fischer    Copy saved lines from another logger object.
318b274bc22SAndre Fischer
319b274bc22SAndre Fischer=cut
320b274bc22SAndre Fischersub copy_lines_from ($$)
321b274bc22SAndre Fischer{
322b274bc22SAndre Fischer    my ($self, $other) = @_;
323b274bc22SAndre Fischer
324b274bc22SAndre Fischer    my $is_print_to_console = $self->{'is_print_to_console'};
325b274bc22SAndre Fischer    my $is_save_lines = $self->{'is_save_lines'};
326b274bc22SAndre Fischer    my $fid = $self->{'file'};
327b274bc22SAndre Fischer
328b274bc22SAndre Fischer    foreach my $line (@{$other->{'lines'}})
329b274bc22SAndre Fischer    {
330b274bc22SAndre Fischer        $self->process_line(@$line);
331b274bc22SAndre Fischer    }
332b274bc22SAndre Fischer}
333b274bc22SAndre Fischer
334b274bc22SAndre Fischer
335b274bc22SAndre Fischer
336b274bc22SAndre Fischer
337b274bc22SAndre Fischer=head2 set_forward ($self, $other)
338b274bc22SAndre Fischer
339b274bc22SAndre Fischer    Set a forwarding target.  All future messages are forwarded (copied) to $other.
340b274bc22SAndre Fischer    A typical use is to tie $Info to $Lang so that all messages sent to $Info are
341b274bc22SAndre Fischer    printed to the console AND written to the log file.
342b274bc22SAndre Fischer
343b274bc22SAndre Fischer=cut
344b274bc22SAndre Fischersub set_forward ($$)
345b274bc22SAndre Fischer{
346b274bc22SAndre Fischer    my ($self, $other) = @_;
347b274bc22SAndre Fischer
348b274bc22SAndre Fischer    # At the moment at most one forward target is allowed.
349b274bc22SAndre Fischer    if (defined $other)
350b274bc22SAndre Fischer    {
351b274bc22SAndre Fischer        $self->{'forward'} = [$other];
352b274bc22SAndre Fischer    }
353b274bc22SAndre Fischer    else
354b274bc22SAndre Fischer    {
355b274bc22SAndre Fischer        $self->{'forward'} = [];
356b274bc22SAndre Fischer    }
357b274bc22SAndre Fischer}
358b274bc22SAndre Fischer
359b274bc22SAndre Fischer
360b274bc22SAndre Fischer
361cdf0e10cSrcweir
362cdf0e10cSrcweir####################################################
363cdf0e10cSrcweir# Including header files into the logfile
364cdf0e10cSrcweir####################################################
365cdf0e10cSrcweir
366cdf0e10cSrcweirsub include_header_into_logfile
367cdf0e10cSrcweir{
368cdf0e10cSrcweir	my ($message) = @_;
369cdf0e10cSrcweir
370b274bc22SAndre Fischer	$Lang->print("\n");
371b274bc22SAndre Fischer	$Lang->print(get_time_string());
372b274bc22SAndre Fischer	$Lang->print("######################################################\n");
373b274bc22SAndre Fischer	$Lang->print($message."\n");
374b274bc22SAndre Fischer	$Lang->print("######################################################\n");
375cdf0e10cSrcweir}
376cdf0e10cSrcweir
377cdf0e10cSrcweir####################################################
378cdf0e10cSrcweir# Including header files into the logfile
379cdf0e10cSrcweir####################################################
380cdf0e10cSrcweir
381cdf0e10cSrcweirsub include_header_into_globallogfile
382cdf0e10cSrcweir{
383cdf0e10cSrcweir	my ($message) = @_;
384cdf0e10cSrcweir
385b274bc22SAndre Fischer	$Global->print("\n");
386b274bc22SAndre Fischer	$Global->print(get_time_string());
387b274bc22SAndre Fischer	$Global->print("######################################################\n");
388b274bc22SAndre Fischer	$Global->print($message."\n");
389b274bc22SAndre Fischer	$Global->print("######################################################\n");
390cdf0e10cSrcweir}
391cdf0e10cSrcweir
392cdf0e10cSrcweir####################################################
393cdf0e10cSrcweir# Write timestamp into log file
394cdf0e10cSrcweir####################################################
395cdf0e10cSrcweir
396cdf0e10cSrcweirsub include_timestamp_into_logfile
397cdf0e10cSrcweir{
3980374af79SAndre Fischer    Die "deprected";
399cdf0e10cSrcweir	my ($message) = @_;
400cdf0e10cSrcweir
401cdf0e10cSrcweir	my $infoline;
402cdf0e10cSrcweir	my $timestring = get_time_string();
403b274bc22SAndre Fischer    $Lang->printf("%s\t%s", $message, $timestring);
404cdf0e10cSrcweir}
405cdf0e10cSrcweir
406cdf0e10cSrcweir####################################################
407cdf0e10cSrcweir# Writing all variables content into the log file
408cdf0e10cSrcweir####################################################
409cdf0e10cSrcweir
410cdf0e10cSrcweirsub log_hashref
411cdf0e10cSrcweir{
412cdf0e10cSrcweir	my ($hashref) = @_;
413cdf0e10cSrcweir
414b274bc22SAndre Fischer    $Global->print("\n");
415b274bc22SAndre Fischer    $Global->print("Logging variable settings:\n");
416cdf0e10cSrcweir
417cdf0e10cSrcweir	my $itemkey;
418cdf0e10cSrcweir
419cdf0e10cSrcweir	foreach $itemkey ( keys %{$hashref} )
420cdf0e10cSrcweir	{
421cdf0e10cSrcweir		my $line = "";
422cdf0e10cSrcweir		my $itemvalue = "";
423cdf0e10cSrcweir		if ( $hashref->{$itemkey} ) { $itemvalue = $hashref->{$itemkey}; }
424b274bc22SAndre Fischer        $Global->printf("%s=%s\n", $itemkey, $itemvalue);
425cdf0e10cSrcweir	}
426cdf0e10cSrcweir
427b274bc22SAndre Fischer    $Global->print("\n");
428cdf0e10cSrcweir}
429cdf0e10cSrcweir
430cdf0e10cSrcweir#########################################################
431cdf0e10cSrcweir# Including global logging info into global log array
432cdf0e10cSrcweir#########################################################
433cdf0e10cSrcweir
434cdf0e10cSrcweirsub globallog
435cdf0e10cSrcweir{
436cdf0e10cSrcweir	my ($message) = @_;
437cdf0e10cSrcweir
438cdf0e10cSrcweir	my $infoline;
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# For each product (new language) a new log file is created.
449cdf0e10cSrcweir# Therefore the global logging has to be saved in this file.
450cdf0e10cSrcweir###############################################################
451cdf0e10cSrcweir
452cdf0e10cSrcweirsub copy_globalinfo_into_logfile
453cdf0e10cSrcweir{
454cdf0e10cSrcweir	for ( my $i = 0; $i <= $#installer::globals::globallogfileinfo; $i++ )
455cdf0e10cSrcweir	{
456cdf0e10cSrcweir		push(@installer::globals::logfileinfo, $installer::globals::globallogfileinfo[$i]);
457cdf0e10cSrcweir	}
458cdf0e10cSrcweir}
459cdf0e10cSrcweir
460cdf0e10cSrcweir###############################################################
461cdf0e10cSrcweir# For each product (new language) a new log file is created.
462cdf0e10cSrcweir# Therefore the global logging has to be saved in this file.
463cdf0e10cSrcweir###############################################################
464cdf0e10cSrcweir
465cdf0e10cSrcweirsub debuginfo
466cdf0e10cSrcweir{
467cdf0e10cSrcweir	my  ( $message ) = @_;
468cdf0e10cSrcweir
469cdf0e10cSrcweir	$message = $message . "\n";
470cdf0e10cSrcweir	push(@installer::globals::functioncalls, $message);
471cdf0e10cSrcweir}
472cdf0e10cSrcweir
473cdf0e10cSrcweir###############################################################
474cdf0e10cSrcweir# Saving the debug information.
475cdf0e10cSrcweir###############################################################
476cdf0e10cSrcweir
477cdf0e10cSrcweirsub savedebug
478cdf0e10cSrcweir{
479cdf0e10cSrcweir	my ( $outputdir ) = @_;
480cdf0e10cSrcweir
481cdf0e10cSrcweir	installer::files::save_file($outputdir . $installer::globals::debugfilename, \@installer::globals::functioncalls);
482cdf0e10cSrcweir	print_message( "... writing debug file " . $outputdir . $installer::globals::debugfilename . "\n" );
483cdf0e10cSrcweir}
484cdf0e10cSrcweir
485cdf0e10cSrcweir###############################################################
486cdf0e10cSrcweir# Starting the time
487cdf0e10cSrcweir###############################################################
488cdf0e10cSrcweir
489cdf0e10cSrcweirsub starttime
490cdf0e10cSrcweir{
491cdf0e10cSrcweir	$installer::globals::starttime = time();
492b274bc22SAndre Fischer	$StartTime = [gettimeofday()];
493b274bc22SAndre Fischer
494b274bc22SAndre Fischer    my $localtime = localtime();
495cdf0e10cSrcweir}
496cdf0e10cSrcweir
497cdf0e10cSrcweir###############################################################
498cdf0e10cSrcweir# Convert time string
499cdf0e10cSrcweir###############################################################
500cdf0e10cSrcweir
501cdf0e10cSrcweirsub convert_timestring
502cdf0e10cSrcweir{
503cdf0e10cSrcweir	my ($secondstring) = @_;
504cdf0e10cSrcweir
505cdf0e10cSrcweir	my $timestring = "";
506cdf0e10cSrcweir
507cdf0e10cSrcweir	if ( $secondstring < 60 )	 # less than a minute
508cdf0e10cSrcweir	{
509cdf0e10cSrcweir		if ( $secondstring < 10 ) { $secondstring = "0" . $secondstring; }
510cdf0e10cSrcweir		$timestring = "00\:$secondstring min\.";
511cdf0e10cSrcweir	}
512cdf0e10cSrcweir	elsif ( $secondstring < 3600 )
513cdf0e10cSrcweir	{
514cdf0e10cSrcweir		my $minutes = $secondstring / 60;
515cdf0e10cSrcweir		my $seconds = $secondstring % 60;
516cdf0e10cSrcweir		if ( $minutes =~ /(\d*)\.\d*/ ) { $minutes = $1; }
517cdf0e10cSrcweir		if ( $minutes < 10 ) { $minutes = "0" . $minutes; }
518cdf0e10cSrcweir		if ( $seconds < 10 ) { $seconds = "0" . $seconds; }
519cdf0e10cSrcweir		$timestring = "$minutes\:$seconds min\.";
520cdf0e10cSrcweir	}
521cdf0e10cSrcweir	else	# more than one hour
522cdf0e10cSrcweir	{
523cdf0e10cSrcweir		my $hours = $secondstring / 3600;
524cdf0e10cSrcweir		my $secondstring = $secondstring % 3600;
525cdf0e10cSrcweir		my $minutes = $secondstring / 60;
526cdf0e10cSrcweir		my $seconds = $secondstring % 60;
527cdf0e10cSrcweir		if ( $hours =~ /(\d*)\.\d*/ ) { $hours = $1; }
528cdf0e10cSrcweir		if ( $minutes =~ /(\d*)\.\d*/ ) { $minutes = $1; }
529cdf0e10cSrcweir		if ( $hours < 10 ) { $hours = "0" . $hours; }
530cdf0e10cSrcweir		if ( $minutes < 10 ) { $minutes = "0" . $minutes; }
531cdf0e10cSrcweir		if ( $seconds < 10 ) { $seconds = "0" . $seconds; }
532cdf0e10cSrcweir		$timestring = "$hours\:$minutes\:$seconds hours";
533cdf0e10cSrcweir	}
534cdf0e10cSrcweir
535cdf0e10cSrcweir	return $timestring;
536cdf0e10cSrcweir}
537cdf0e10cSrcweir
538cdf0e10cSrcweir###############################################################
539cdf0e10cSrcweir# Returning time string for logging
540cdf0e10cSrcweir###############################################################
541cdf0e10cSrcweir
542cdf0e10cSrcweirsub get_time_string
543cdf0e10cSrcweir{
544cdf0e10cSrcweir	my $currenttime = time();
545cdf0e10cSrcweir	$currenttime = $currenttime - $installer::globals::starttime;
546cdf0e10cSrcweir	$currenttime = convert_timestring($currenttime);
547cdf0e10cSrcweir	$currenttime = localtime() . " \(" . $currenttime . "\)\n";
548cdf0e10cSrcweir	return $currenttime;
549cdf0e10cSrcweir}
550cdf0e10cSrcweir
551cdf0e10cSrcweir###############################################################
552cdf0e10cSrcweir# Returning the age of a file (in seconds)
553cdf0e10cSrcweir###############################################################
554cdf0e10cSrcweir
555cdf0e10cSrcweirsub get_file_age
556cdf0e10cSrcweir{
557cdf0e10cSrcweir	my ( $filename ) = @_;
558cdf0e10cSrcweir
559cdf0e10cSrcweir	my $filetime = (stat($filename))[9];
560cdf0e10cSrcweir	my $timediff = time() - $filetime;
561cdf0e10cSrcweir	return $timediff;
562cdf0e10cSrcweir}
563cdf0e10cSrcweir
564cdf0e10cSrcweir###############################################################
565cdf0e10cSrcweir# Stopping the time
566cdf0e10cSrcweir###############################################################
567cdf0e10cSrcweir
568cdf0e10cSrcweirsub stoptime
569cdf0e10cSrcweir{
570b274bc22SAndre Fischer    my $localtime = localtime();
571b274bc22SAndre Fischer    $Info->printf("stopping log at %s\n", $localtime);
572cdf0e10cSrcweir}
573cdf0e10cSrcweir
574cdf0e10cSrcweir###############################################################
575cdf0e10cSrcweir# Set date string, format: yymmdd
576cdf0e10cSrcweir###############################################################
577cdf0e10cSrcweir
578cdf0e10cSrcweirsub set_installation_date
579cdf0e10cSrcweir{
580cdf0e10cSrcweir	my $datestring = "";
581cdf0e10cSrcweir
582cdf0e10cSrcweir	my @timearray = localtime(time);
583cdf0e10cSrcweir
584cdf0e10cSrcweir	my $day = $timearray[3];
585cdf0e10cSrcweir	my $month = $timearray[4] + 1;
586cdf0e10cSrcweir	my $year = $timearray[5] - 100;
587cdf0e10cSrcweir
588cdf0e10cSrcweir	if ( $year < 10 ) { $year = "0" . $year; }
589cdf0e10cSrcweir	if ( $month < 10 ) { $month = "0" . $month; }
590cdf0e10cSrcweir	if ( $day < 10 ) { $day = "0" . $day; }
591cdf0e10cSrcweir
592cdf0e10cSrcweir	$datestring = $year . $month . $day;
593cdf0e10cSrcweir
594cdf0e10cSrcweir	return $datestring;
595cdf0e10cSrcweir}
596cdf0e10cSrcweir
597cdf0e10cSrcweir###############################################################
598cdf0e10cSrcweir# Console output: messages
599cdf0e10cSrcweir###############################################################
600cdf0e10cSrcweir
601cdf0e10cSrcweirsub print_message
602cdf0e10cSrcweir{
6030374af79SAndre Fischer    Die "print_message is deprecated";
604b274bc22SAndre Fischer
605cdf0e10cSrcweir    my $message = shift;
606cdf0e10cSrcweir    chomp $message;
607cdf0e10cSrcweir    my $force = shift || 0;
608cdf0e10cSrcweir    print "$message\n" if ( $force || ! $installer::globals::quiet );
609cdf0e10cSrcweir    return;
610cdf0e10cSrcweir}
611cdf0e10cSrcweir
612cdf0e10cSrcweirsub print_message_without_newline
613cdf0e10cSrcweir{
614cdf0e10cSrcweir    my $message = shift;
615cdf0e10cSrcweir    chomp $message;
616cdf0e10cSrcweir    print "$message" if ( ! $installer::globals::quiet );
617cdf0e10cSrcweir    return;
618cdf0e10cSrcweir}
619cdf0e10cSrcweir
620cdf0e10cSrcweir###############################################################
621cdf0e10cSrcweir# Console output: warnings
622cdf0e10cSrcweir###############################################################
623cdf0e10cSrcweir
624cdf0e10cSrcweirsub print_warning
625cdf0e10cSrcweir{
626cdf0e10cSrcweir    my $message = shift;
627cdf0e10cSrcweir    chomp $message;
628cdf0e10cSrcweir    print STDERR "WARNING: $message";
629cdf0e10cSrcweir    return;
630cdf0e10cSrcweir}
631cdf0e10cSrcweir
632cdf0e10cSrcweir###############################################################
633cdf0e10cSrcweir# Console output: errors
634cdf0e10cSrcweir###############################################################
635cdf0e10cSrcweir
636cdf0e10cSrcweirsub print_error
637cdf0e10cSrcweir{
638cdf0e10cSrcweir    my $message = shift;
639cdf0e10cSrcweir    chomp $message;
640b274bc22SAndre Fischer    print STDERR "\n";
641b274bc22SAndre Fischer    print STDERR "**************************************************\n";
642cdf0e10cSrcweir    print STDERR "ERROR: $message";
643b274bc22SAndre Fischer    print STDERR "\n";
644b274bc22SAndre Fischer    print STDERR "**************************************************\n";
645cdf0e10cSrcweir    return;
646cdf0e10cSrcweir}
647cdf0e10cSrcweir
64887d0bf7aSAndre Fischer
64987d0bf7aSAndre Fischer=head2 PrintStackTrace()
65087d0bf7aSAndre Fischer    This is for debugging the print and printf methods of the logger class and their use.
65187d0bf7aSAndre Fischer    Therefore we use the Perl print/printf directly and not the logger methods to avoid loops in case of errors.
65287d0bf7aSAndre Fischer=cut
65387d0bf7aSAndre Fischersub PrintStackTrace ()
65487d0bf7aSAndre Fischer{
65587d0bf7aSAndre Fischer    print "Stack Trace:\n";
65687d0bf7aSAndre Fischer    my $i = 1;
65787d0bf7aSAndre Fischer    while ((my @call_details = (caller($i++))))
65887d0bf7aSAndre Fischer    {
65987d0bf7aSAndre Fischer        printf("%s:%s in function %s\n", $call_details[1], $call_details[2], $call_details[3]);
66087d0bf7aSAndre Fischer    }
66187d0bf7aSAndre Fischer}
66287d0bf7aSAndre Fischer
663*9daef0aeSAndre Fischer
66487d0bf7aSAndre Fischersub Die ($)
66587d0bf7aSAndre Fischer{
66687d0bf7aSAndre Fischer    my ($message) = @_;
66787d0bf7aSAndre Fischer    PrintStackTrace();
66887d0bf7aSAndre Fischer    die $message;
66987d0bf7aSAndre Fischer}
67087d0bf7aSAndre Fischer
67187d0bf7aSAndre Fischer
67287d0bf7aSAndre Fischer
673cdf0e10cSrcweir1;
674