xref: /AOO41X/main/solenv/bin/modules/installer/logger.pm (revision ff0525f24f03981d56b7579b645949f111420994)
1#**************************************************************
2#
3#  Licensed to the Apache Software Foundation (ASF) under one
4#  or more contributor license agreements.  See the NOTICE file
5#  distributed with this work for additional information
6#  regarding copyright ownership.  The ASF licenses this file
7#  to you under the Apache License, Version 2.0 (the
8#  "License"); you may not use this file except in compliance
9#  with the License.  You may obtain a copy of the License at
10#
11#    http://www.apache.org/licenses/LICENSE-2.0
12#
13#  Unless required by applicable law or agreed to in writing,
14#  software distributed under the License is distributed on an
15#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16#  KIND, either express or implied.  See the License for the
17#  specific language governing permissions and limitations
18#  under the License.
19#
20#**************************************************************
21
22
23
24package installer::logger;
25
26use installer::files;
27use installer::globals;
28
29####################################################
30# Including header files into the logfile
31####################################################
32
33sub include_header_into_logfile
34{
35    my ($message) = @_;
36
37    my $infoline;
38
39    $infoline = "\n" . get_time_string();
40    push( @installer::globals::logfileinfo, $infoline);
41
42    $infoline = "######################################################\n";
43    push( @installer::globals::logfileinfo, $infoline);
44
45    $infoline = "$message\n";
46    push( @installer::globals::logfileinfo, $infoline);
47
48
49    $infoline = "######################################################\n";
50    push( @installer::globals::logfileinfo, $infoline);
51}
52
53####################################################
54# Including header files into the logfile
55####################################################
56
57sub include_header_into_globallogfile
58{
59    my ($message) = @_;
60
61    my $infoline;
62
63    $infoline = "\n" . get_time_string();
64    push( @installer::globals::globallogfileinfo, $infoline);
65
66    $infoline = "######################################################\n";
67    push( @installer::globals::globallogfileinfo, $infoline);
68
69    $infoline = "$message\n";
70    push( @installer::globals::globallogfileinfo, $infoline);
71
72
73    $infoline = "######################################################\n";
74    push( @installer::globals::globallogfileinfo, $infoline);
75}
76
77####################################################
78# Write timestamp into log file
79####################################################
80
81sub include_timestamp_into_logfile
82{
83    my ($message) = @_;
84
85    my $infoline;
86    my $timestring = get_time_string();
87    $infoline = "$message\t$timestring";
88    push( @installer::globals::logfileinfo, $infoline);
89}
90
91####################################################
92# Writing all variables content into the log file
93####################################################
94
95sub log_hashref
96{
97    my ($hashref) = @_;
98
99    my $infoline = "\nLogging variable settings:\n";
100    push(@installer::globals::globallogfileinfo, $infoline);
101
102    my $itemkey;
103
104    foreach $itemkey ( keys %{$hashref} )
105    {
106        my $line = "";
107        my $itemvalue = "";
108        if ( $hashref->{$itemkey} ) { $itemvalue = $hashref->{$itemkey}; }
109        $line = $itemkey . "=" . $itemvalue . "\n";
110        push(@installer::globals::globallogfileinfo, $line);
111    }
112
113    $infoline = "\n";
114    push(@installer::globals::globallogfileinfo, $infoline);
115}
116
117#########################################################
118# Including global logging info into global log array
119#########################################################
120
121sub globallog
122{
123    my ($message) = @_;
124
125    my $infoline;
126
127    $infoline = "\n" . get_time_string();
128    push( @installer::globals::globallogfileinfo, $infoline);
129
130    $infoline = "################################################################\n";
131    push( @installer::globals::globallogfileinfo, $infoline);
132
133    $infoline = "$message\n";
134    push( @installer::globals::globallogfileinfo, $infoline);
135
136    $infoline = "################################################################\n";
137    push( @installer::globals::globallogfileinfo, $infoline);
138
139}
140
141###############################################################
142# For each product (new language) a new log file is created.
143# Therefore the global logging has to be saved in this file.
144###############################################################
145
146sub copy_globalinfo_into_logfile
147{
148    for ( my $i = 0; $i <= $#installer::globals::globallogfileinfo; $i++ )
149    {
150        push(@installer::globals::logfileinfo, $installer::globals::globallogfileinfo[$i]);
151    }
152}
153
154###############################################################
155# For each product (new language) a new log file is created.
156# Therefore the global logging has to be saved in this file.
157###############################################################
158
159sub debuginfo
160{
161    my  ( $message ) = @_;
162
163    $message = $message . "\n";
164    push(@installer::globals::functioncalls, $message);
165}
166
167###############################################################
168# Saving the debug information.
169###############################################################
170
171sub savedebug
172{
173    my ( $outputdir ) = @_;
174
175    installer::files::save_file($outputdir . $installer::globals::debugfilename, \@installer::globals::functioncalls);
176    print_message( "... writing debug file " . $outputdir . $installer::globals::debugfilename . "\n" );
177}
178
179###############################################################
180# Starting the time
181###############################################################
182
183sub starttime
184{
185    $installer::globals::starttime = time();
186}
187
188###############################################################
189# Convert time string
190###############################################################
191
192sub convert_timestring
193{
194    my ($secondstring) = @_;
195
196    my $timestring = "";
197
198    if ( $secondstring < 60 )    # less than a minute
199    {
200        if ( $secondstring < 10 ) { $secondstring = "0" . $secondstring; }
201        $timestring = "00\:$secondstring min\.";
202    }
203    elsif ( $secondstring < 3600 )
204    {
205        my $minutes = $secondstring / 60;
206        my $seconds = $secondstring % 60;
207        if ( $minutes =~ /(\d*)\.\d*/ ) { $minutes = $1; }
208        if ( $minutes < 10 ) { $minutes = "0" . $minutes; }
209        if ( $seconds < 10 ) { $seconds = "0" . $seconds; }
210        $timestring = "$minutes\:$seconds min\.";
211    }
212    else    # more than one hour
213    {
214        my $hours = $secondstring / 3600;
215        my $secondstring = $secondstring % 3600;
216        my $minutes = $secondstring / 60;
217        my $seconds = $secondstring % 60;
218        if ( $hours =~ /(\d*)\.\d*/ ) { $hours = $1; }
219        if ( $minutes =~ /(\d*)\.\d*/ ) { $minutes = $1; }
220        if ( $hours < 10 ) { $hours = "0" . $hours; }
221        if ( $minutes < 10 ) { $minutes = "0" . $minutes; }
222        if ( $seconds < 10 ) { $seconds = "0" . $seconds; }
223        $timestring = "$hours\:$minutes\:$seconds hours";
224    }
225
226    return $timestring;
227}
228
229###############################################################
230# Returning time string for logging
231###############################################################
232
233sub get_time_string
234{
235    my $currenttime = time();
236    $currenttime = $currenttime - $installer::globals::starttime;
237    $currenttime = convert_timestring($currenttime);
238    $currenttime = localtime() . " \(" . $currenttime . "\)\n";
239    return $currenttime;
240}
241
242###############################################################
243# Returning the age of a file (in seconds)
244###############################################################
245
246sub get_file_age
247{
248    my ( $filename ) = @_;
249
250    my $filetime = (stat($filename))[9];
251    my $timediff = time() - $filetime;
252    return $timediff;
253}
254
255###############################################################
256# Stopping the time
257###############################################################
258
259sub stoptime
260{
261    my $infoline = get_time_string();
262    print_message( "$infoline" );
263}
264
265###############################################################
266# Set date string, format: yymmdd
267###############################################################
268
269sub set_installation_date
270{
271    my $datestring = "";
272
273    my @timearray = localtime(time);
274
275    my $day = $timearray[3];
276    my $month = $timearray[4] + 1;
277    my $year = $timearray[5] - 100;
278
279    if ( $year < 10 ) { $year = "0" . $year; }
280    if ( $month < 10 ) { $month = "0" . $month; }
281    if ( $day < 10 ) { $day = "0" . $day; }
282
283    $datestring = $year . $month . $day;
284
285    return $datestring;
286}
287
288###############################################################
289# Console output: messages
290###############################################################
291
292sub print_message
293{
294    my $message = shift;
295    chomp $message;
296    my $force = shift || 0;
297    print "$message\n" if ( $force || ! $installer::globals::quiet );
298    return;
299}
300
301sub print_message_without_newline
302{
303    my $message = shift;
304    chomp $message;
305    print "$message" if ( ! $installer::globals::quiet );
306    return;
307}
308
309###############################################################
310# Console output: warnings
311###############################################################
312
313sub print_warning
314{
315    my $message = shift;
316    chomp $message;
317    print STDERR "WARNING: $message";
318    return;
319}
320
321###############################################################
322# Console output: errors
323###############################################################
324
325sub print_error
326{
327    my $message = shift;
328    chomp $message;
329    print STDERR "\n**************************************************\n";
330    print STDERR "ERROR: $message";
331    print STDERR "\n**************************************************\n";
332    return;
333}
334
3351;
336