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