Branch libreoffice-5-0-4
[LibreOffice.git] / solenv / bin / modules / installer / logger.pm
blob24fe5fc7be3abbeeb051c539c34b0d3319e66a07
2 # This file is part of the LibreOffice project.
4 # This Source Code Form is subject to the terms of the Mozilla Public
5 # License, v. 2.0. If a copy of the MPL was not distributed with this
6 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 # This file incorporates work covered by the following license notice:
10 # Licensed to the Apache Software Foundation (ASF) under one or more
11 # contributor license agreements. See the NOTICE file distributed
12 # with this work for additional information regarding copyright
13 # ownership. The ASF licenses this file to you under the Apache
14 # License, Version 2.0 (the "License"); you may not use this file
15 # except in compliance with the License. You may obtain a copy of
16 # the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package installer::logger;
21 use strict;
22 use warnings;
24 use base 'Exporter';
26 use installer::files;
27 use installer::globals;
29 our @EXPORT_OK = qw(
30 include_header_into_logfile
31 include_timestamp_into_logfile
32 log_hashref
33 globallog
34 copy_globalinfo_into_logfile
35 starttime
36 stoptime
37 print_message
38 print_warning
39 print_error
42 my $starttime;
44 ####################################################
45 # Including header files into the logfile
46 ####################################################
48 sub include_header_into_logfile
50 my ($message) = @_;
52 my $infoline;
54 $infoline = "\n" . _get_time_string();
55 push( @installer::globals::logfileinfo, $infoline);
57 $infoline = "######################################################\n";
58 push( @installer::globals::logfileinfo, $infoline);
60 $infoline = "$message\n";
61 push( @installer::globals::logfileinfo, $infoline);
64 $infoline = "######################################################\n";
65 push( @installer::globals::logfileinfo, $infoline);
68 ####################################################
69 # Write timestamp into log file
70 ####################################################
72 sub include_timestamp_into_logfile
74 my ($message) = @_;
76 my $infoline;
77 my $timestring = _get_time_string();
78 $infoline = "$message\t$timestring";
79 push( @installer::globals::logfileinfo, $infoline);
82 ####################################################
83 # Writing all variables content into the log file
84 ####################################################
86 sub log_hashref
88 my ($hashref) = @_;
90 my $infoline = "\nLogging variable settings:\n";
91 push(@installer::globals::globallogfileinfo, $infoline);
93 my $itemkey;
95 foreach $itemkey ( keys %{$hashref} )
97 my $line = "";
98 my $itemvalue = "";
99 if ( $hashref->{$itemkey} ) { $itemvalue = $hashref->{$itemkey}; }
100 $line = $itemkey . "=" . $itemvalue . "\n";
101 push(@installer::globals::globallogfileinfo, $line);
104 $infoline = "\n";
105 push(@installer::globals::globallogfileinfo, $infoline);
108 #########################################################
109 # Including global logging info into global log array
110 #########################################################
112 sub globallog
114 my ($message) = @_;
116 my $infoline;
118 $infoline = "\n" . _get_time_string();
119 push( @installer::globals::globallogfileinfo, $infoline);
121 $infoline = "######################################################\n";
122 push( @installer::globals::globallogfileinfo, $infoline);
124 $infoline = "$message\n";
125 push( @installer::globals::globallogfileinfo, $infoline);
127 $infoline = "######################################################\n";
128 push( @installer::globals::globallogfileinfo, $infoline);
132 ###############################################################
133 # For each product (new language) a new log file is created.
134 # Therefore the global logging has to be saved in this file.
135 ###############################################################
137 sub copy_globalinfo_into_logfile
139 for ( my $i = 0; $i <= $#installer::globals::globallogfileinfo; $i++ )
141 push(@installer::globals::logfileinfo, $installer::globals::globallogfileinfo[$i]);
145 ###############################################################
146 # Starting the time
147 ###############################################################
149 sub starttime
151 $starttime = time();
154 ###############################################################
155 # Convert time string
156 ###############################################################
158 sub _convert_timestring
160 my ($secondstring) = @_;
162 my $timestring = "";
164 if ( $secondstring < 60 ) # less than a minute
166 if ( $secondstring < 10 ) { $secondstring = "0" . $secondstring; }
167 $timestring = "00\:$secondstring min\.";
169 elsif ( $secondstring < 3600 )
171 my $minutes = $secondstring / 60;
172 my $seconds = $secondstring % 60;
173 if ( $minutes =~ /(\d*)\.\d*/ ) { $minutes = $1; }
174 if ( $minutes < 10 ) { $minutes = "0" . $minutes; }
175 if ( $seconds < 10 ) { $seconds = "0" . $seconds; }
176 $timestring = "$minutes\:$seconds min\.";
178 else # more than one hour
180 my $hours = $secondstring / 3600;
181 my $secondstring = $secondstring % 3600;
182 my $minutes = $secondstring / 60;
183 my $seconds = $secondstring % 60;
184 if ( $hours =~ /(\d*)\.\d*/ ) { $hours = $1; }
185 if ( $minutes =~ /(\d*)\.\d*/ ) { $minutes = $1; }
186 if ( $hours < 10 ) { $hours = "0" . $hours; }
187 if ( $minutes < 10 ) { $minutes = "0" . $minutes; }
188 if ( $seconds < 10 ) { $seconds = "0" . $seconds; }
189 $timestring = "$hours\:$minutes\:$seconds hours";
192 return $timestring;
195 ###############################################################
196 # Returning time string for logging
197 ###############################################################
199 sub _get_time_string
201 my $currenttime = time();
202 $currenttime = $currenttime - $starttime;
203 $currenttime = _convert_timestring($currenttime);
204 $currenttime = localtime() . " \(" . $currenttime . "\)\n";
205 return $currenttime;
208 ###############################################################
209 # Stopping the time
210 ###############################################################
212 sub stoptime
214 print_message( _get_time_string() );
217 ###############################################################
218 # Console output: messages
219 ###############################################################
221 sub print_message
223 my $message = shift;
224 chomp $message;
225 my $force = shift || 0;
226 print "$message\n" if ( $force || ! $installer::globals::quiet );
227 return;
230 ###############################################################
231 # Console output: warnings
232 ###############################################################
234 sub print_warning
236 my $message = shift;
237 chomp $message;
238 print STDERR "WARNING: $message";
239 return;
242 ###############################################################
243 # Console output: errors
244 ###############################################################
246 sub print_error
248 my $message = shift;
249 chomp $message;
250 print STDERR "\n**************************************************\n";
251 print STDERR "ERROR: $message";
252 print STDERR "\n**************************************************\n";
253 return;