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 922f2de527SAndre Fischer 932f2de527SAndre Fischer 942f2de527SAndre Fischer=head2 SetupSimpleLogging ($filename) 952f2de527SAndre Fischer 96*9f91b7e3SAndre Fischer Setup logging so that $Global, $Lang and $Info all print to the console. 97*9f91b7e3SAndre Fischer If $filename is given then logging also goes to that file. 982f2de527SAndre Fischer 992f2de527SAndre Fischer=cut 100*9f91b7e3SAndre Fischersub SetupSimpleLogging (;$) 1012f2de527SAndre Fischer{ 1022f2de527SAndre Fischer my ($log_filename) = @_; 1032f2de527SAndre Fischer 1042f2de527SAndre Fischer $Info = installer::logger->new("info", 1052f2de527SAndre Fischer 'is_print_to_console' => 1, 1062f2de527SAndre Fischer 'is_show_relative_time' => 1, 1072f2de527SAndre Fischer ); 1082f2de527SAndre Fischer $Global = installer::logger->new("glob", 1092f2de527SAndre Fischer 'is_print_to_console' => 0, 1102f2de527SAndre Fischer 'is_show_relative_time' => 1, 1112f2de527SAndre Fischer 'forward' => [$Info] 1122f2de527SAndre Fischer ); 1132f2de527SAndre Fischer $Lang = installer::logger->new("lang", 1142f2de527SAndre Fischer 'is_print_to_console' => 0, 1152f2de527SAndre Fischer 'is_show_relative_time' => 1, 1162f2de527SAndre Fischer 'forward' => [$Info] 1172f2de527SAndre Fischer ); 118*9f91b7e3SAndre Fischer if (defined $log_filename) 119*9f91b7e3SAndre Fischer { 1202f2de527SAndre Fischer $Info->set_filename($log_filename); 121*9f91b7e3SAndre Fischer } 1222f2de527SAndre Fischer $Info->{'is_print_to_console'} = 1; 1232f2de527SAndre Fischer $installer::globals::quiet = 0; 1242f2de527SAndre Fischer starttime(); 1252f2de527SAndre Fischer} 1262f2de527SAndre Fischer 1272f2de527SAndre Fischer 1282f2de527SAndre Fischer 1292f2de527SAndre Fischer 130b274bc22SAndre Fischer=head2 new($class, $id, @arguments) 131b274bc22SAndre Fischer 132b274bc22SAndre Fischer Create a new instance of the logger class. 133b274bc22SAndre Fischer @arguments lets you override default values. 134b274bc22SAndre Fischer 135b274bc22SAndre Fischer=cut 136b274bc22SAndre Fischer 137b274bc22SAndre Fischersub new ($$@) 138b274bc22SAndre Fischer{ 139b274bc22SAndre Fischer my ($class, $id, @arguments) = @_; 140b274bc22SAndre Fischer 141b274bc22SAndre Fischer my $self = { 142b274bc22SAndre Fischer 'id' => $id, 143b274bc22SAndre Fischer 'filename' => "", 144b274bc22SAndre Fischer # When set then lines are printed to this file. 145b274bc22SAndre Fischer 'file' => undef, 146b274bc22SAndre Fischer # When true then lines are printed to the console. 147b274bc22SAndre Fischer 'is_print_to_console' => 1, 148b274bc22SAndre Fischer 'is_save_lines' => 0, 149b274bc22SAndre Fischer # A container of printed lines. Lines are added only when 'is_save_lines' is true. 150b274bc22SAndre Fischer 'lines' => [], 151b274bc22SAndre Fischer # Another logger to which all prints are forwarded. 152b274bc22SAndre Fischer 'forward' => [], 153b274bc22SAndre Fischer # A filter function that for example can recoginze build errors. 154b274bc22SAndre Fischer 'filter' => undef, 155b274bc22SAndre Fischer # Show relative time 156b274bc22SAndre Fischer 'is_show_relative_time' => 0, 157b274bc22SAndre Fischer # Show log id (mostly for debugging the logger) 158b274bc22SAndre Fischer 'is_show_log_id' => 0, 159b274bc22SAndre Fischer # Show the process id, useful on the console when doing a multiprocessor build. 1602f2de527SAndre Fischer 'is_show_process_id' => 0, 1612f2de527SAndre Fischer # Current indentation 1622f2de527SAndre Fischer 'indentation' => "", 163b274bc22SAndre Fischer }; 164b274bc22SAndre Fischer while (scalar @arguments >= 2) 165b274bc22SAndre Fischer { 166b274bc22SAndre Fischer my $key = shift @arguments; 167b274bc22SAndre Fischer my $value = shift @arguments; 168b274bc22SAndre Fischer $self->{$key} = $value; 169b274bc22SAndre Fischer } 170b274bc22SAndre Fischer 171b274bc22SAndre Fischer bless($self, $class); 172b274bc22SAndre Fischer 173b274bc22SAndre Fischer return $self; 174b274bc22SAndre Fischer} 175b274bc22SAndre Fischer 176b274bc22SAndre Fischer 177b274bc22SAndre Fischer 178b274bc22SAndre Fischer=head2 printf($self, $message, @arguments) 179b274bc22SAndre Fischer 180b274bc22SAndre Fischer Identical in syntax and semantics to the usual perl (s)printf. 181b274bc22SAndre Fischer 182b274bc22SAndre Fischer=cut 183b274bc22SAndre Fischersub printf ($$@) 184b274bc22SAndre Fischer{ 185b274bc22SAndre Fischer my ($self, $format, @arguments) = @_; 186b274bc22SAndre Fischer 1879daef0aeSAndre Fischer if ($format =~ /\%\{/) 18887d0bf7aSAndre Fischer { 1899daef0aeSAndre Fischer printf(">%s<\n", $format); 19087d0bf7aSAndre Fischer PrintStackTrace(); 19187d0bf7aSAndre Fischer } 1929daef0aeSAndre Fischer my $message = sprintf($format, @arguments); 19387d0bf7aSAndre Fischer $self->print($message, 0); 194b274bc22SAndre Fischer} 195b274bc22SAndre Fischer 196b274bc22SAndre Fischer 197b274bc22SAndre Fischer 198b274bc22SAndre Fischer 199b274bc22SAndre Fischer=head2 print ($self, $message, [optional] $force) 200b274bc22SAndre Fischer 201b274bc22SAndre Fischer Print the given message. 202b274bc22SAndre Fischer If the optional $force parameter is given and it evaluates to true then the message 203b274bc22SAndre Fischer is printed even when the golbal $installer::globals::quiet is true. 204b274bc22SAndre Fischer 205b274bc22SAndre Fischer=cut 206b274bc22SAndre Fischersub print ($$;$) 207b274bc22SAndre Fischer{ 208b274bc22SAndre Fischer my ($self, $message, $force) = @_; 209b274bc22SAndre Fischer 2100374af79SAndre Fischer Die "newline at start of line" if ($message =~ /^\n.+/); 211b274bc22SAndre Fischer 212b274bc22SAndre Fischer $force = 0 unless defined $force; 213b274bc22SAndre Fischer 214b274bc22SAndre Fischer my $relative_time = tv_interval($StartTime, [gettimeofday()]); 215b274bc22SAndre Fischer foreach my $target ($self, @{$self->{'forward'}}) 216b274bc22SAndre Fischer { 217b274bc22SAndre Fischer $target->process_line( 218b274bc22SAndre Fischer $relative_time, 219b274bc22SAndre Fischer $self->{'id'}, 220b274bc22SAndre Fischer $PID, 221b274bc22SAndre Fischer $message, 222b274bc22SAndre Fischer $force); 223b274bc22SAndre Fischer } 224b274bc22SAndre Fischer} 225b274bc22SAndre Fischer 226b274bc22SAndre Fischer 227b274bc22SAndre Fischer 228b274bc22SAndre Fischer 229b274bc22SAndre Fischer=head2 process_line ($self, $relative_time, $log_id, $pid, $message, $force) 230b274bc22SAndre Fischer 231b274bc22SAndre Fischer Internal function that decides whether to 232b274bc22SAndre Fischer a) write to a log file 233b274bc22SAndre Fischer b) print to the console 234b274bc22SAndre Fischer c) store in an array for later use 235b274bc22SAndre Fischer the preformatted message. 236b274bc22SAndre Fischer 237b274bc22SAndre Fischer=cut 238b274bc22SAndre Fischersub process_line ($$$$$$) 239b274bc22SAndre Fischer{ 240b274bc22SAndre Fischer my ($self, $relative_time, $log_id, $pid, $message, $force) = @_; 241b274bc22SAndre Fischer 242b274bc22SAndre Fischer # Apply the line filter. 243b274bc22SAndre Fischer if (defined $self->{'filter'}) 244b274bc22SAndre Fischer { 245b274bc22SAndre Fischer $message = &{$self->{'filter'}}($relative_time, $log_id, $pid, $message); 246b274bc22SAndre Fischer } 247b274bc22SAndre Fischer 248b274bc22SAndre Fischer # Format the line. 249b274bc22SAndre Fischer my $line = ""; 250b274bc22SAndre Fischer if ($self->{'is_show_relative_time'}) 251b274bc22SAndre Fischer { 252b274bc22SAndre Fischer $line .= sprintf("%12.6f : ", $relative_time); 253b274bc22SAndre Fischer } 254b274bc22SAndre Fischer if ($self->{'is_show_log_id'}) 255b274bc22SAndre Fischer { 256b274bc22SAndre Fischer $line .= $log_id . " : "; 257b274bc22SAndre Fischer } 258b274bc22SAndre Fischer if ($self->{'is_show_process_id'}) 259b274bc22SAndre Fischer { 260b274bc22SAndre Fischer $line .= $pid . " : "; 261b274bc22SAndre Fischer } 2622f2de527SAndre Fischer $line .= $self->{'indentation'}; 263b274bc22SAndre Fischer $line .= $message; 264b274bc22SAndre Fischer 265b274bc22SAndre Fischer # Print the line to a file or to the console or store it for later use. 266b274bc22SAndre Fischer my $fid = $self->{'file'}; 267b274bc22SAndre Fischer if (defined $fid) 268b274bc22SAndre Fischer { 269b274bc22SAndre Fischer print $fid ($line); 270b274bc22SAndre Fischer } 271b274bc22SAndre Fischer if (($force || ! $installer::globals::quiet) 272b274bc22SAndre Fischer && $self->{'is_print_to_console'}) 273b274bc22SAndre Fischer { 274b274bc22SAndre Fischer print($line); 275b274bc22SAndre Fischer } 276b274bc22SAndre Fischer if ($self->{'is_save_lines'}) 277b274bc22SAndre Fischer { 278b274bc22SAndre Fischer push @{$self->{'lines'}}, [$relative_time, $log_id, $pid, $message, $force]; 279b274bc22SAndre Fischer } 280b274bc22SAndre Fischer} 281b274bc22SAndre Fischer 282b274bc22SAndre Fischer 283b274bc22SAndre Fischer 284b274bc22SAndre Fischer 285b274bc22SAndre Fischer=head2 set_filename (Self, $filename) 286b274bc22SAndre Fischer 287b274bc22SAndre Fischer When the name of a writable file is given then all future messages will go to that file. 288b274bc22SAndre Fischer Output to the console is turned off. 289b274bc22SAndre Fischer This method is typically used to tie the language dependent $Lang logger to different log files. 290b274bc22SAndre Fischer 291b274bc22SAndre Fischer=cut 292b274bc22SAndre Fischersub set_filename ($$) 293b274bc22SAndre Fischer{ 294b274bc22SAndre Fischer my ($self, $filename) = @_; 295b274bc22SAndre Fischer 296b274bc22SAndre Fischer $filename = "" unless defined $filename; 297b274bc22SAndre Fischer if ($self->{'filename'} ne $filename) 298b274bc22SAndre Fischer { 299b274bc22SAndre Fischer if (defined $self->{'file'}) 300b274bc22SAndre Fischer { 301b274bc22SAndre Fischer $self->{'is_print_to_console'} = 1; 302b274bc22SAndre Fischer close $self->{'file'}; 303b274bc22SAndre Fischer $self->{'file'} = undef; 304b274bc22SAndre Fischer } 305b274bc22SAndre Fischer 306b274bc22SAndre Fischer $self->{'filename'} = $filename; 307b274bc22SAndre Fischer 308b274bc22SAndre Fischer if ($filename ne "") 309b274bc22SAndre Fischer { 310b274bc22SAndre Fischer open $self->{'file'}, ">", $self->{'filename'} 3110374af79SAndre Fischer || Die "can not open log file ".$self->{'filename'}." for writing"; 312b274bc22SAndre Fischer $self->{'is_print_to_console'} = 0; 313b274bc22SAndre Fischer 314b274bc22SAndre Fischer # Make all writes synchronous so that we don't loose any messages on an 315b274bc22SAndre Fischer # 'abrupt' end. 316b274bc22SAndre Fischer my $handle = select $self->{'file'}; 317b274bc22SAndre Fischer $| = 1; 318b274bc22SAndre Fischer select $handle; 319b274bc22SAndre Fischer } 320b274bc22SAndre Fischer } 321b274bc22SAndre Fischer} 322b274bc22SAndre Fischer 323b274bc22SAndre Fischer 324b274bc22SAndre Fischer 325b274bc22SAndre Fischer 326b274bc22SAndre Fischer=head2 set_filter ($self, $filter) 327b274bc22SAndre Fischer 328b274bc22SAndre Fischer Sets $filter (a function reference) as line filter. It is applied to each line. 329b274bc22SAndre Fischer The filter can extract information from the given message and modify it before it is printed. 330b274bc22SAndre Fischer 331b274bc22SAndre Fischer=cut 332b274bc22SAndre Fischersub set_filter ($$) 333b274bc22SAndre Fischer{ 334b274bc22SAndre Fischer my ($self, $filter) = @_; 335b274bc22SAndre Fischer $self->{'filter'} = $filter; 336b274bc22SAndre Fischer} 337b274bc22SAndre Fischer 338b274bc22SAndre Fischer 339b274bc22SAndre Fischer 340b274bc22SAndre Fischer 341b274bc22SAndre Fischer=head2 add_timestamp ($self, $message) 342b274bc22SAndre Fischer 343b274bc22SAndre Fischer Print the given message together with the current (absolute) time. 344b274bc22SAndre Fischer 345b274bc22SAndre Fischer=cut 346b274bc22SAndre Fischersub add_timestamp ($$) 347b274bc22SAndre Fischer{ 348b274bc22SAndre Fischer my ($self, $message) = @_; 349b274bc22SAndre Fischer 350b274bc22SAndre Fischer my $timestring = get_time_string(); 351b274bc22SAndre Fischer $self->printf("%s\t%s", $message, $timestring); 352b274bc22SAndre Fischer} 353b274bc22SAndre Fischer 354b274bc22SAndre Fischer 355b274bc22SAndre Fischer 356b274bc22SAndre Fischer=head2 copy_lines_from ($self, $other) 357b274bc22SAndre Fischer 358b274bc22SAndre Fischer Copy saved lines from another logger object. 359b274bc22SAndre Fischer 360b274bc22SAndre Fischer=cut 361b274bc22SAndre Fischersub copy_lines_from ($$) 362b274bc22SAndre Fischer{ 363b274bc22SAndre Fischer my ($self, $other) = @_; 364b274bc22SAndre Fischer 365b274bc22SAndre Fischer my $is_print_to_console = $self->{'is_print_to_console'}; 366b274bc22SAndre Fischer my $is_save_lines = $self->{'is_save_lines'}; 367b274bc22SAndre Fischer my $fid = $self->{'file'}; 368b274bc22SAndre Fischer 369b274bc22SAndre Fischer foreach my $line (@{$other->{'lines'}}) 370b274bc22SAndre Fischer { 371b274bc22SAndre Fischer $self->process_line(@$line); 372b274bc22SAndre Fischer } 373b274bc22SAndre Fischer} 374b274bc22SAndre Fischer 375b274bc22SAndre Fischer 376b274bc22SAndre Fischer 377b274bc22SAndre Fischer 378b274bc22SAndre Fischer=head2 set_forward ($self, $other) 379b274bc22SAndre Fischer 380b274bc22SAndre Fischer Set a forwarding target. All future messages are forwarded (copied) to $other. 381b274bc22SAndre Fischer A typical use is to tie $Info to $Lang so that all messages sent to $Info are 382b274bc22SAndre Fischer printed to the console AND written to the log file. 383b274bc22SAndre Fischer 384b274bc22SAndre Fischer=cut 385b274bc22SAndre Fischersub set_forward ($$) 386b274bc22SAndre Fischer{ 387b274bc22SAndre Fischer my ($self, $other) = @_; 388b274bc22SAndre Fischer 389b274bc22SAndre Fischer # At the moment at most one forward target is allowed. 390b274bc22SAndre Fischer if (defined $other) 391b274bc22SAndre Fischer { 392b274bc22SAndre Fischer $self->{'forward'} = [$other]; 393b274bc22SAndre Fischer } 394b274bc22SAndre Fischer else 395b274bc22SAndre Fischer { 396b274bc22SAndre Fischer $self->{'forward'} = []; 397b274bc22SAndre Fischer } 398b274bc22SAndre Fischer} 399b274bc22SAndre Fischer 400b274bc22SAndre Fischer 401b274bc22SAndre Fischer 402cdf0e10cSrcweir 4032f2de527SAndre Fischersub increase_indentation ($) 4042f2de527SAndre Fischer{ 4052f2de527SAndre Fischer my ($self) = @_; 4062f2de527SAndre Fischer $self->{'indentation'} .= " "; 4072f2de527SAndre Fischer} 4082f2de527SAndre Fischer 4092f2de527SAndre Fischer 4102f2de527SAndre Fischer 4112f2de527SAndre Fischer 4122f2de527SAndre Fischersub decrease_indentation ($) 4132f2de527SAndre Fischer{ 4142f2de527SAndre Fischer my ($self) = @_; 4152f2de527SAndre Fischer $self->{'indentation'} = substr($self->{'indentation'}, 4); 4162f2de527SAndre Fischer} 4172f2de527SAndre Fischer 4182f2de527SAndre Fischer 4192f2de527SAndre Fischer 4202f2de527SAndre Fischer 421cdf0e10cSrcweir#################################################### 422cdf0e10cSrcweir# Including header files into the logfile 423cdf0e10cSrcweir#################################################### 424cdf0e10cSrcweir 425cdf0e10cSrcweirsub include_header_into_logfile 426cdf0e10cSrcweir{ 427cdf0e10cSrcweir my ($message) = @_; 428cdf0e10cSrcweir 429b274bc22SAndre Fischer $Lang->print("\n"); 430b274bc22SAndre Fischer $Lang->print(get_time_string()); 431b274bc22SAndre Fischer $Lang->print("######################################################\n"); 432b274bc22SAndre Fischer $Lang->print($message."\n"); 433b274bc22SAndre Fischer $Lang->print("######################################################\n"); 434cdf0e10cSrcweir} 435cdf0e10cSrcweir 436cdf0e10cSrcweir#################################################### 437cdf0e10cSrcweir# Including header files into the logfile 438cdf0e10cSrcweir#################################################### 439cdf0e10cSrcweir 440cdf0e10cSrcweirsub include_header_into_globallogfile 441cdf0e10cSrcweir{ 442cdf0e10cSrcweir my ($message) = @_; 443cdf0e10cSrcweir 444b274bc22SAndre Fischer $Global->print("\n"); 445b274bc22SAndre Fischer $Global->print(get_time_string()); 446b274bc22SAndre Fischer $Global->print("######################################################\n"); 447b274bc22SAndre Fischer $Global->print($message."\n"); 448b274bc22SAndre Fischer $Global->print("######################################################\n"); 449cdf0e10cSrcweir} 450cdf0e10cSrcweir 451cdf0e10cSrcweir#################################################### 452cdf0e10cSrcweir# Write timestamp into log file 453cdf0e10cSrcweir#################################################### 454cdf0e10cSrcweir 455cdf0e10cSrcweirsub include_timestamp_into_logfile 456cdf0e10cSrcweir{ 4570374af79SAndre Fischer Die "deprected"; 458cdf0e10cSrcweir my ($message) = @_; 459cdf0e10cSrcweir 460cdf0e10cSrcweir my $infoline; 461cdf0e10cSrcweir my $timestring = get_time_string(); 462b274bc22SAndre Fischer $Lang->printf("%s\t%s", $message, $timestring); 463cdf0e10cSrcweir} 464cdf0e10cSrcweir 465cdf0e10cSrcweir#################################################### 466cdf0e10cSrcweir# Writing all variables content into the log file 467cdf0e10cSrcweir#################################################### 468cdf0e10cSrcweir 469cdf0e10cSrcweirsub log_hashref 470cdf0e10cSrcweir{ 471cdf0e10cSrcweir my ($hashref) = @_; 472cdf0e10cSrcweir 473b274bc22SAndre Fischer $Global->print("\n"); 474b274bc22SAndre Fischer $Global->print("Logging variable settings:\n"); 475cdf0e10cSrcweir 476cdf0e10cSrcweir my $itemkey; 477cdf0e10cSrcweir 478cdf0e10cSrcweir foreach $itemkey ( keys %{$hashref} ) 479cdf0e10cSrcweir { 480cdf0e10cSrcweir my $line = ""; 481cdf0e10cSrcweir my $itemvalue = ""; 482cdf0e10cSrcweir if ( $hashref->{$itemkey} ) { $itemvalue = $hashref->{$itemkey}; } 483b274bc22SAndre Fischer $Global->printf("%s=%s\n", $itemkey, $itemvalue); 484cdf0e10cSrcweir } 485cdf0e10cSrcweir 486b274bc22SAndre Fischer $Global->print("\n"); 487cdf0e10cSrcweir} 488cdf0e10cSrcweir 489cdf0e10cSrcweir######################################################### 490cdf0e10cSrcweir# Including global logging info into global log array 491cdf0e10cSrcweir######################################################### 492cdf0e10cSrcweir 493cdf0e10cSrcweirsub globallog 494cdf0e10cSrcweir{ 495cdf0e10cSrcweir my ($message) = @_; 496cdf0e10cSrcweir 497cdf0e10cSrcweir my $infoline; 498cdf0e10cSrcweir 499b274bc22SAndre Fischer $Global->print("\n"); 500b274bc22SAndre Fischer $Global->print(get_time_string()); 501b274bc22SAndre Fischer $Global->print("################################################################\n"); 502b274bc22SAndre Fischer $Global->print($message."\n"); 503b274bc22SAndre Fischer $Global->print("################################################################\n"); 504cdf0e10cSrcweir} 505cdf0e10cSrcweir 506cdf0e10cSrcweir############################################################### 507cdf0e10cSrcweir# For each product (new language) a new log file is created. 508cdf0e10cSrcweir# Therefore the global logging has to be saved in this file. 509cdf0e10cSrcweir############################################################### 510cdf0e10cSrcweir 511cdf0e10cSrcweirsub copy_globalinfo_into_logfile 512cdf0e10cSrcweir{ 513cdf0e10cSrcweir for ( my $i = 0; $i <= $#installer::globals::globallogfileinfo; $i++ ) 514cdf0e10cSrcweir { 515cdf0e10cSrcweir push(@installer::globals::logfileinfo, $installer::globals::globallogfileinfo[$i]); 516cdf0e10cSrcweir } 517cdf0e10cSrcweir} 518cdf0e10cSrcweir 519cdf0e10cSrcweir############################################################### 520cdf0e10cSrcweir# For each product (new language) a new log file is created. 521cdf0e10cSrcweir# Therefore the global logging has to be saved in this file. 522cdf0e10cSrcweir############################################################### 523cdf0e10cSrcweir 524cdf0e10cSrcweirsub debuginfo 525cdf0e10cSrcweir{ 526cdf0e10cSrcweir my ( $message ) = @_; 527cdf0e10cSrcweir 528cdf0e10cSrcweir $message = $message . "\n"; 529cdf0e10cSrcweir push(@installer::globals::functioncalls, $message); 530cdf0e10cSrcweir} 531cdf0e10cSrcweir 532cdf0e10cSrcweir############################################################### 533cdf0e10cSrcweir# Saving the debug information. 534cdf0e10cSrcweir############################################################### 535cdf0e10cSrcweir 536cdf0e10cSrcweirsub savedebug 537cdf0e10cSrcweir{ 538cdf0e10cSrcweir my ( $outputdir ) = @_; 539cdf0e10cSrcweir 540cdf0e10cSrcweir installer::files::save_file($outputdir . $installer::globals::debugfilename, \@installer::globals::functioncalls); 541cdf0e10cSrcweir print_message( "... writing debug file " . $outputdir . $installer::globals::debugfilename . "\n" ); 542cdf0e10cSrcweir} 543cdf0e10cSrcweir 544cdf0e10cSrcweir############################################################### 545cdf0e10cSrcweir# Starting the time 546cdf0e10cSrcweir############################################################### 547cdf0e10cSrcweir 548cdf0e10cSrcweirsub starttime 549cdf0e10cSrcweir{ 550cdf0e10cSrcweir $installer::globals::starttime = time(); 551b274bc22SAndre Fischer $StartTime = [gettimeofday()]; 552b274bc22SAndre Fischer 553b274bc22SAndre Fischer my $localtime = localtime(); 554cdf0e10cSrcweir} 555cdf0e10cSrcweir 556cdf0e10cSrcweir############################################################### 557cdf0e10cSrcweir# Convert time string 558cdf0e10cSrcweir############################################################### 559cdf0e10cSrcweir 560cdf0e10cSrcweirsub convert_timestring 561cdf0e10cSrcweir{ 562cdf0e10cSrcweir my ($secondstring) = @_; 563cdf0e10cSrcweir 564cdf0e10cSrcweir my $timestring = ""; 565cdf0e10cSrcweir 566cdf0e10cSrcweir if ( $secondstring < 60 ) # less than a minute 567cdf0e10cSrcweir { 568cdf0e10cSrcweir if ( $secondstring < 10 ) { $secondstring = "0" . $secondstring; } 569cdf0e10cSrcweir $timestring = "00\:$secondstring min\."; 570cdf0e10cSrcweir } 571cdf0e10cSrcweir elsif ( $secondstring < 3600 ) 572cdf0e10cSrcweir { 573cdf0e10cSrcweir my $minutes = $secondstring / 60; 574cdf0e10cSrcweir my $seconds = $secondstring % 60; 575cdf0e10cSrcweir if ( $minutes =~ /(\d*)\.\d*/ ) { $minutes = $1; } 576cdf0e10cSrcweir if ( $minutes < 10 ) { $minutes = "0" . $minutes; } 577cdf0e10cSrcweir if ( $seconds < 10 ) { $seconds = "0" . $seconds; } 578cdf0e10cSrcweir $timestring = "$minutes\:$seconds min\."; 579cdf0e10cSrcweir } 580cdf0e10cSrcweir else # more than one hour 581cdf0e10cSrcweir { 582cdf0e10cSrcweir my $hours = $secondstring / 3600; 583cdf0e10cSrcweir my $secondstring = $secondstring % 3600; 584cdf0e10cSrcweir my $minutes = $secondstring / 60; 585cdf0e10cSrcweir my $seconds = $secondstring % 60; 586cdf0e10cSrcweir if ( $hours =~ /(\d*)\.\d*/ ) { $hours = $1; } 587cdf0e10cSrcweir if ( $minutes =~ /(\d*)\.\d*/ ) { $minutes = $1; } 588cdf0e10cSrcweir if ( $hours < 10 ) { $hours = "0" . $hours; } 589cdf0e10cSrcweir if ( $minutes < 10 ) { $minutes = "0" . $minutes; } 590cdf0e10cSrcweir if ( $seconds < 10 ) { $seconds = "0" . $seconds; } 591cdf0e10cSrcweir $timestring = "$hours\:$minutes\:$seconds hours"; 592cdf0e10cSrcweir } 593cdf0e10cSrcweir 594cdf0e10cSrcweir return $timestring; 595cdf0e10cSrcweir} 596cdf0e10cSrcweir 597cdf0e10cSrcweir############################################################### 598cdf0e10cSrcweir# Returning time string for logging 599cdf0e10cSrcweir############################################################### 600cdf0e10cSrcweir 601cdf0e10cSrcweirsub get_time_string 602cdf0e10cSrcweir{ 603cdf0e10cSrcweir my $currenttime = time(); 604cdf0e10cSrcweir $currenttime = $currenttime - $installer::globals::starttime; 605cdf0e10cSrcweir $currenttime = convert_timestring($currenttime); 606cdf0e10cSrcweir $currenttime = localtime() . " \(" . $currenttime . "\)\n"; 607cdf0e10cSrcweir return $currenttime; 608cdf0e10cSrcweir} 609cdf0e10cSrcweir 610cdf0e10cSrcweir############################################################### 611cdf0e10cSrcweir# Returning the age of a file (in seconds) 612cdf0e10cSrcweir############################################################### 613cdf0e10cSrcweir 614cdf0e10cSrcweirsub get_file_age 615cdf0e10cSrcweir{ 616cdf0e10cSrcweir my ( $filename ) = @_; 617cdf0e10cSrcweir 618cdf0e10cSrcweir my $filetime = (stat($filename))[9]; 619cdf0e10cSrcweir my $timediff = time() - $filetime; 620cdf0e10cSrcweir return $timediff; 621cdf0e10cSrcweir} 622cdf0e10cSrcweir 623cdf0e10cSrcweir############################################################### 624cdf0e10cSrcweir# Stopping the time 625cdf0e10cSrcweir############################################################### 626cdf0e10cSrcweir 627cdf0e10cSrcweirsub stoptime 628cdf0e10cSrcweir{ 629b274bc22SAndre Fischer my $localtime = localtime(); 630b274bc22SAndre Fischer $Info->printf("stopping log at %s\n", $localtime); 631cdf0e10cSrcweir} 632cdf0e10cSrcweir 633cdf0e10cSrcweir############################################################### 634cdf0e10cSrcweir# Set date string, format: yymmdd 635cdf0e10cSrcweir############################################################### 636cdf0e10cSrcweir 637cdf0e10cSrcweirsub set_installation_date 638cdf0e10cSrcweir{ 639cdf0e10cSrcweir my $datestring = ""; 640cdf0e10cSrcweir 641cdf0e10cSrcweir my @timearray = localtime(time); 642cdf0e10cSrcweir 643cdf0e10cSrcweir my $day = $timearray[3]; 644cdf0e10cSrcweir my $month = $timearray[4] + 1; 645cdf0e10cSrcweir my $year = $timearray[5] - 100; 646cdf0e10cSrcweir 647cdf0e10cSrcweir if ( $year < 10 ) { $year = "0" . $year; } 648cdf0e10cSrcweir if ( $month < 10 ) { $month = "0" . $month; } 649cdf0e10cSrcweir if ( $day < 10 ) { $day = "0" . $day; } 650cdf0e10cSrcweir 651cdf0e10cSrcweir $datestring = $year . $month . $day; 652cdf0e10cSrcweir 653cdf0e10cSrcweir return $datestring; 654cdf0e10cSrcweir} 655cdf0e10cSrcweir 656cdf0e10cSrcweir############################################################### 657cdf0e10cSrcweir# Console output: messages 658cdf0e10cSrcweir############################################################### 659cdf0e10cSrcweir 660cdf0e10cSrcweirsub print_message 661cdf0e10cSrcweir{ 6620374af79SAndre Fischer Die "print_message is deprecated"; 663b274bc22SAndre Fischer 664cdf0e10cSrcweir my $message = shift; 665cdf0e10cSrcweir chomp $message; 666cdf0e10cSrcweir my $force = shift || 0; 667cdf0e10cSrcweir print "$message\n" if ( $force || ! $installer::globals::quiet ); 668cdf0e10cSrcweir return; 669cdf0e10cSrcweir} 670cdf0e10cSrcweir 671cdf0e10cSrcweirsub print_message_without_newline 672cdf0e10cSrcweir{ 673cdf0e10cSrcweir my $message = shift; 674cdf0e10cSrcweir chomp $message; 675cdf0e10cSrcweir print "$message" if ( ! $installer::globals::quiet ); 676cdf0e10cSrcweir return; 677cdf0e10cSrcweir} 678cdf0e10cSrcweir 679cdf0e10cSrcweir############################################################### 680cdf0e10cSrcweir# Console output: warnings 681cdf0e10cSrcweir############################################################### 682cdf0e10cSrcweir 683cdf0e10cSrcweirsub print_warning 684cdf0e10cSrcweir{ 685cdf0e10cSrcweir my $message = shift; 686cdf0e10cSrcweir chomp $message; 687cdf0e10cSrcweir print STDERR "WARNING: $message"; 688cdf0e10cSrcweir return; 689cdf0e10cSrcweir} 690cdf0e10cSrcweir 691cdf0e10cSrcweir############################################################### 692cdf0e10cSrcweir# Console output: errors 693cdf0e10cSrcweir############################################################### 694cdf0e10cSrcweir 695cdf0e10cSrcweirsub print_error 696cdf0e10cSrcweir{ 697cdf0e10cSrcweir my $message = shift; 698cdf0e10cSrcweir chomp $message; 6992f2de527SAndre Fischer 7002f2de527SAndre Fischer PrintError($message); 7012f2de527SAndre Fischer 702b274bc22SAndre Fischer print STDERR "\n"; 703b274bc22SAndre Fischer print STDERR "**************************************************\n"; 704cdf0e10cSrcweir print STDERR "ERROR: $message"; 705b274bc22SAndre Fischer print STDERR "\n"; 706b274bc22SAndre Fischer print STDERR "**************************************************\n"; 707cdf0e10cSrcweir return; 708cdf0e10cSrcweir} 709cdf0e10cSrcweir 71087d0bf7aSAndre Fischer 7112f2de527SAndre Fischer 7122f2de527SAndre Fischer 7132f2de527SAndre Fischersub PrintError ($@) 7142f2de527SAndre Fischer{ 7152f2de527SAndre Fischer my ($format, @arguments) = @_; 7162f2de527SAndre Fischer 7172f2de527SAndre Fischer $Info->printf("Error: ".$format, @arguments); 7182f2de527SAndre Fischer} 7192f2de527SAndre Fischer 7202f2de527SAndre Fischer 7212f2de527SAndre Fischer 7222f2de527SAndre Fischer 72387d0bf7aSAndre Fischer=head2 PrintStackTrace() 72487d0bf7aSAndre Fischer This is for debugging the print and printf methods of the logger class and their use. 72587d0bf7aSAndre Fischer Therefore we use the Perl print/printf directly and not the logger methods to avoid loops in case of errors. 72687d0bf7aSAndre Fischer=cut 72787d0bf7aSAndre Fischersub PrintStackTrace () 72887d0bf7aSAndre Fischer{ 72987d0bf7aSAndre Fischer print "Stack Trace:\n"; 73087d0bf7aSAndre Fischer my $i = 1; 73187d0bf7aSAndre Fischer while ((my @call_details = (caller($i++)))) 73287d0bf7aSAndre Fischer { 73387d0bf7aSAndre Fischer printf("%s:%s in function %s\n", $call_details[1], $call_details[2], $call_details[3]); 73487d0bf7aSAndre Fischer } 73587d0bf7aSAndre Fischer} 73687d0bf7aSAndre Fischer 7379daef0aeSAndre Fischer 73887d0bf7aSAndre Fischersub Die ($) 73987d0bf7aSAndre Fischer{ 74087d0bf7aSAndre Fischer my ($message) = @_; 74187d0bf7aSAndre Fischer PrintStackTrace(); 74287d0bf7aSAndre Fischer die $message; 74387d0bf7aSAndre Fischer} 74487d0bf7aSAndre Fischer 74587d0bf7aSAndre Fischer 74687d0bf7aSAndre Fischer 747cdf0e10cSrcweir1; 748