| 1 |
# |
| 2 |
# This file is part of the LibreOffice project. |
| 3 |
# |
| 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/. |
| 7 |
# |
| 8 |
# This file incorporates work covered by the following license notice: |
| 9 |
# |
| 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 . |
| 17 |
# |
| 18 |
|
| 19 |
package installer::logger; |
| 20 |
|
| 21 |
use strict; |
| 22 |
use warnings; |
| 23 |
|
| 24 |
use base 'Exporter'; |
| 25 |
|
| 26 |
use installer::files; |
| 27 |
use installer::globals; |
| 28 |
|
| 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 |
| 40 |
); |
| 41 |
|
| 42 |
my $starttime; |
| 43 |
|
| 44 |
#################################################### |
| 45 |
# Including header files into the logfile |
| 46 |
#################################################### |
| 47 |
|
| 48 |
sub include_header_into_logfile |
| 49 |
{ |
| 50 |
my ($message) = @_; |
| 51 |
|
| 52 |
my $infoline; |
| 53 |
|
| 54 |
$infoline = "\n" . _get_time_string(); |
| 55 |
push( @installer::globals::logfileinfo, $infoline); |
| 56 |
|
| 57 |
$infoline = "######################################################\n"; |
| 58 |
push( @installer::globals::logfileinfo, $infoline); |
| 59 |
|
| 60 |
$infoline = "$message\n"; |
| 61 |
push( @installer::globals::logfileinfo, $infoline); |
| 62 |
|
| 63 |
|
| 64 |
$infoline = "######################################################\n"; |
| 65 |
push( @installer::globals::logfileinfo, $infoline); |
| 66 |
} |
| 67 |
|
| 68 |
#################################################### |
| 69 |
# Write timestamp into log file |
| 70 |
#################################################### |
| 71 |
|
| 72 |
sub include_timestamp_into_logfile |
| 73 |
{ |
| 74 |
my ($message) = @_; |
| 75 |
|
| 76 |
my $infoline; |
| 77 |
my $timestring = _get_time_string(); |
| 78 |
$infoline = "$message\t$timestring"; |
| 79 |
push( @installer::globals::logfileinfo, $infoline); |
| 80 |
} |
| 81 |
|
| 82 |
#################################################### |
| 83 |
# Writing all variables content into the log file |
| 84 |
#################################################### |
| 85 |
|
| 86 |
sub log_hashref |
| 87 |
{ |
| 88 |
my ($hashref) = @_; |
| 89 |
|
| 90 |
my $infoline = "\nLogging variable settings:\n"; |
| 91 |
push(@installer::globals::globallogfileinfo, $infoline); |
| 92 |
|
| 93 |
my $itemkey; |
| 94 |
|
| 95 |
foreach $itemkey ( keys %{$hashref} ) |
| 96 |
{ |
| 97 |
my $line = ""; |
| 98 |
my $itemvalue = ""; |
| 99 |
if ( $hashref->{$itemkey} ) { $itemvalue = $hashref->{$itemkey}; } |
| 100 |
$line = $itemkey . "=" . $itemvalue . "\n"; |
| 101 |
push(@installer::globals::globallogfileinfo, $line); |
| 102 |
} |
| 103 |
|
| 104 |
$infoline = "\n"; |
| 105 |
push(@installer::globals::globallogfileinfo, $infoline); |
| 106 |
} |
| 107 |
|
| 108 |
######################################################### |
| 109 |
# Including global logging info into global log array |
| 110 |
######################################################### |
| 111 |
|
| 112 |
sub globallog |
| 113 |
{ |
| 114 |
my ($message) = @_; |
| 115 |
|
| 116 |
my $infoline; |
| 117 |
|
| 118 |
$infoline = "\n" . _get_time_string(); |
| 119 |
push( @installer::globals::globallogfileinfo, $infoline); |
| 120 |
|
| 121 |
$infoline = "######################################################\n"; |
| 122 |
push( @installer::globals::globallogfileinfo, $infoline); |
| 123 |
|
| 124 |
$infoline = "$message\n"; |
| 125 |
push( @installer::globals::globallogfileinfo, $infoline); |
| 126 |
|
| 127 |
$infoline = "######################################################\n"; |
| 128 |
push( @installer::globals::globallogfileinfo, $infoline); |
| 129 |
|
| 130 |
} |
| 131 |
|
| 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 |
############################################################### |
| 136 |
|
| 137 |
sub copy_globalinfo_into_logfile |
| 138 |
{ |
| 139 |
for ( my $i = 0; $i <= $#installer::globals::globallogfileinfo; $i++ ) |
| 140 |
{ |
| 141 |
push(@installer::globals::logfileinfo, $installer::globals::globallogfileinfo[$i]); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
############################################################### |
| 146 |
# Starting the time |
| 147 |
############################################################### |
| 148 |
|
| 149 |
sub starttime |
| 150 |
{ |
| 151 |
$starttime = time(); |
| 152 |
} |
| 153 |
|
| 154 |
############################################################### |
| 155 |
# Convert time string |
| 156 |
############################################################### |
| 157 |
|
| 158 |
sub _convert_timestring |
| 159 |
{ |
| 160 |
my ($secondstring) = @_; |
| 161 |
|
| 162 |
my $timestring = ""; |
| 163 |
|
| 164 |
if ( $secondstring < 60 ) # less than a minute |
| 165 |
{ |
| 166 |
if ( $secondstring < 10 ) { $secondstring = "0" . $secondstring; } |
| 167 |
$timestring = "00\:$secondstring min\."; |
| 168 |
} |
| 169 |
elsif ( $secondstring < 3600 ) |
| 170 |
{ |
| 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\."; |
| 177 |
} |
| 178 |
else # more than one hour |
| 179 |
{ |
| 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"; |
| 190 |
} |
| 191 |
|
| 192 |
return $timestring; |
| 193 |
} |
| 194 |
|
| 195 |
############################################################### |
| 196 |
# Returning time string for logging |
| 197 |
############################################################### |
| 198 |
|
| 199 |
sub _get_time_string |
| 200 |
{ |
| 201 |
my $currenttime = time(); |
| 202 |
$currenttime = $currenttime - $starttime; |
| 203 |
$currenttime = _convert_timestring($currenttime); |
| 204 |
$currenttime = localtime() . " \(" . $currenttime . "\)\n"; |
| 205 |
return $currenttime; |
| 206 |
} |
| 207 |
|
| 208 |
############################################################### |
| 209 |
# Stopping the time |
| 210 |
############################################################### |
| 211 |
|
| 212 |
sub stoptime |
| 213 |
{ |
| 214 |
print_message( _get_time_string() ); |
| 215 |
} |
| 216 |
|
| 217 |
############################################################### |
| 218 |
# Console output: messages |
| 219 |
############################################################### |
| 220 |
|
| 221 |
sub print_message |
| 222 |
{ |
| 223 |
my $message = shift; |
| 224 |
chomp $message; |
| 225 |
my $force = shift || 0; |
| 226 |
print "$message\n" if ( $force || ! $installer::globals::quiet ); |
| 227 |
return; |
| 228 |
} |
| 229 |
|
| 230 |
############################################################### |
| 231 |
# Console output: warnings |
| 232 |
############################################################### |
| 233 |
|
| 234 |
sub print_warning |
| 235 |
{ |
| 236 |
my $message = shift; |
| 237 |
chomp $message; |
| 238 |
print STDERR "WARNING: $message"; |
| 239 |
return; |
| 240 |
} |
| 241 |
|
| 242 |
############################################################### |
| 243 |
# Console output: errors |
| 244 |
############################################################### |
| 245 |
|
| 246 |
sub print_error |
| 247 |
{ |
| 248 |
my $message = shift; |
| 249 |
chomp $message; |
| 250 |
print STDERR "\n**************************************************\n"; |
| 251 |
print STDERR "ERROR: $message"; |
| 252 |
print STDERR "\n**************************************************\n"; |
| 253 |
return; |
| 254 |
} |
| 255 |
|
| 256 |
1; |