-
Notifications
You must be signed in to change notification settings - Fork 1
/
Erik.pm
1282 lines (867 loc) · 29.9 KB
/
Erik.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package Erik;
use strict;
no warnings;
=head1 Name
Erik - Erik's debugging buddy
=head1 Description
Quick methods for debugging Perl.
Why use my name??? I found the I've never used my name as a variable; therefore I could create safe guards to make sure the 'Erik::' never makes it into production. It should go without saying that debugging code (thus the Erik 'module') should never be found in production.
Erik has been designed to only require Perl. However, if you use the 'dump' subroutine or specify 'logger' in the require statement supporting modules are loaded.
=head2 Caveat Emptor
Erik was originally developed to troubleshoot a CGI script; hence the text|html flag. It attempts to guess if you are in browser and behave accordingly. It's been a long time since I've used Erik in this manner. If you're trying to use it in browser and it no longer works please let me know.
=head1 Usage
use Erik qw(off html);
Erik::dump('name', $ref_to_dump); # uses Data::Dumper;
Erik::log(); # output: file_name [line_num]
Erik::log('message'); # output: file_name [line_num]: message
Erik::stack_trace(); # full blow stack trace telling you how you got there
Erik::vars('name', $name); # just prints out that variable
Erik::vars({ name1 => $name1, name2 => $name2 }); # print one line sep by tabs
Erik::module_location(); # sometime you just need to know they are in the right place
Erik::append("a");
Erik::append("b");
Erik::append("b");
Erik::publish(); # output: a :: b :: b
# compact output for loops
Erik::min($_) for 1..100; # output: 1, 2, 3, 4, 5, ..., 100
Erik::spacer(5); # add 5 lines of blank space - if you need some space
Erik::print_file('file_name'); # if you need to see what is in the file right now
sub some_method {
Erik::method(); # output: file_name [line_num]: some_method
}
=head1 Require Flags
When requiring Erik several variables can be passed in.
Unless specified the default value is off/false.
=over 4
=item epoch
pre-pend log lines with the epoch timestamp
=item force_html_header
Print an HTML style header as soon as possible.
The header printed depends on what mode it is in:
=over 4
=item text - Content-type: text/plain\n\n
=item html - Content-type: text/html\n\n
=back
=item disable_header
When Erik starts a new 'session' it will print a 'New Log' header. This can be disabled by setting 'disable_header' to true.
NOTE: This has nothing to do with the 'force_html_header' flag.
=item line
print line/program info before all non 'log' outputs
=item log
print everything to a log file
By default this is /tmp/erik.out. The output can be configured using the 'log_filename' setting.
This will also force the mode into text. Passing 'html' will not work - it'll be ignored.
=item log_filename
name of the file that logs will be printed to
This argument takes the form: log_filename=/tmp/output_file_name
Everything after the '=' will be used as the filename. It is HIGHLY RECOMMENDED to ALWAYS use a full path.
No guarantee is made for partial or relative paths!
=item logger
use Log::Log4perl to write all information as 'debug'
=item on|off
initial state of debugging output on/off - Default: on
=item pid
print Process ID to each line
=item report
print report when process is done
Currently, this is just a summary of the methods that were called with a count.
NOTE: only subroutine where Erik::method is called are included.
=item stderr
print everything to STDERR instead of STDOUT
=item text|html
the expected output format - Default: text
If neither is provided then it will attempt to guess by checking %ENV for any keys starting with HTTP_.
=item time
pre-pend log lines with a human readable timestamp
=item time_stats
pre-pend log lines with timing stats: seconds since last log line - seconds since start of program
This is not a replacement for benchmarking. It is a simple way to try to find sections of the code that take a long time to execute.
=back
=head1 Environmental Variables
=over 4
=item ERIK_OFF
Same as passing 'off' when loading the module. Found that sometimes it's easier
to do this than find the right 'use Erik' and turn it off. Also, enable/disable
calls are honored.
=item ERIK_DISABLE
Totally disables Erik's print method so nothing will show up.
=back
=head1 .erikrc
If .erikrc is found in your home directory ($ENV{HOME}/.erikrc, /etc/.erikrc).
The first one found will be loaded and those settings will be applied.
NOTE: settings are overwritten by what you specify while using Erik.
In an attempt to keep Erik light weight the config needs to be in a data
structure that can be eval'ed.
Example:
{
# any settings in the import method can be set
on => 1,
log => 1,
mode => 'text',
# Setting for Data::Dumper: https://metacpan.org/pod/Data::Dumper#Configuration-Variables-or-Methods
# These will be only applied if Erik::dump is used
dumper => {
Indent => 2,
Maxdepth => 3,
Sortkeys => 1,
}
}
=cut
my %_settings = (
line => 0, # 1 - auto print line/program info before most prints
log => 0, # 1 - print everything to /tmp/erik.out (or what is set by log_filename)
logger => 0, # 1 - send prints also to Log::Log4perl's logger
log_filename => '/tmp/erik.out', # location of log output
mode => 0, # text|html
pid => 0, # 1 - print the process id and order id
report => 0, # 1 - print a general report when done - right now just a method call count
state => 1, # 1 - on, 0 - off, -1 - single command off
stderr => 0, # 1 - print everything to STDERR else to STDOUT
_header_printed => 1, # since only printed once a value of 0 means print
_logger => undef, # only get the Log::Log4perl's logger once
_publish_separator => ' :: ', # separator used by the publish method
_stack_trace_limit => 1, # num of stack traces to print out from a given subroutine - use stack_trace_limit to change this
_time_last => time,
_time_start => time,
);
my %_default_settings = %_settings;
my %class_restrictions = ( none => 1 ); # if enable/disable called for specific name spaces
my %_subroutine_report = ();
END {
print("\n") if $_settings{_min_mode};
if (keys %_subroutine_report && $_settings{report}) {
my $report = "\nSubroutine Call Report\n**********************\n";
$report .= sprintf("%10d :: %s\n", $_subroutine_report{$_}, $_)
for sort {$a cmp $b} keys %_subroutine_report;
$report .= "\n";
_print($report);
}
}
=head1 METHODS
=head2 append
=over 4
=item Description
Erik::append("First piece of info");
Adds information to an internal store that will get printed with the 'publish' method.
Each piece of information will be seperate by 'publish_seperator'.
=back
=cut
sub append {
my $string = shift || return '';
push(@{$_settings{_publish}}, $string);
}
=head2 counter
=over 4
=item Description
Erik::counter();
Erik::counter('My Counter');
Print out a counter that is incremented by one each time.
If a name is provided then it will increment and print every time it sees that name.
If no name is provided then a name will be constructed from the file name and line number.
All counters start at 1.
=back
=cut
my %_counters;
sub counter {
my $name = shift || '';
my $display_name = $name;
if (!$name) {
$display_name = 'Counter';
my @called_from = caller;
$name = $called_from[1] . '--' . $called_from[2];
};
&log("$display_name: " . ++$_counters{$name});
}
=head2 disable
=over 4
=item Description
Erik::disable();
Erik::disable('Module::Name::A', 'Module::Name::B');
Turn off the debugger.
If a list of modules name(s) is provided then only disable debugging in those. Calling it again without a list will disable debugging everywhere.
=back
=cut
sub disable {
my @modules = @_;
if (@modules) {
%class_restrictions = ( disable => \@modules );
}
else {
%class_restrictions = ( none => 1 );
}
$_settings{state} = 0;
}
=head2 dump
=over 4
=item Description
Erik::dump('name', $ref_to_dump);
Erik::dump(name => $ref_to_dump);
Erik::dump(name => $ref_to_dump, maxdepth => 3);
Erik::dump(name => $ref_to_dump, 3);
Erik::dump($ref_to_dump);
Erik::dump($ref_to_dump, maxdepth => 3); # WILL NOT WORK!!!!!!!!!!!!!!!!
Erik::dump($ref_to_dump, 3); # WILL NOT WORK!!!!!!!!!!!!!!!!
This will 'dump' the content of the variable reference that is passed. The name is simply what is displayed above and below it.
It will attempt to use Data::Dumper. If it is not installed then it just blows up.
maxdepth (or a simple number as the 3rd arg) will limit the depth of the dump. (Used to set $Data::Dumper::Maxdepth) No argument or 0 assumes unlimited.
If you only provide a reference to a variable it will dump that out. There is no ability to set maxdepth with this. Infact, using maxdepth at that point will not work!!!!
=back
=cut
sub dump {
my $name = shift;
my $var = shift;
if (!defined $var) {
if (ref $name) {
$var = $name;
$name = 'No Name Provided';
}
else {
my @called_from = caller;
warn("dump called improperly ("
. $called_from[1] . ' [' . $called_from[2]
. "]): USAGE: Erik::dump(title => \\\%var);\n");
}
}
my $max_depth_label = shift;
my $max_depth = shift;
$max_depth = $max_depth_label if $max_depth_label =~ /^\d+$/;
require Data::Dumper;
Erik::dump_setting($_, $_settings{_rc_settings}{dumper}{$_}, 1)
for keys %{$_settings{_rc_settings}{dumper}};
Erik::dump_setting(Maxdepth => $max_depth, 1) if defined $max_depth;
my $dump = Data::Dumper->Dump([$var]);
Erik::dump_setting(Maxdepth => (exists $_settings{_rc_settings}{Maxdepth} ? $_settings{_rc_settings}{Maxdepth} : 0))
if defined $max_depth; # reset so it doesn't effect the next call
_print(_header($name) . $dump . _header("END: $name"));
}
=head2 dump_setting
=over 4
=item Description
Erik::dump_setting(Indent => 1);
Erik::dump_setting(Pad => 'ERIK');
Set any of the config/method variable that are available.
See Data::Dumper man page.
=back
=cut
sub dump_setting {
my $method = shift || die("No method provided to dump_setting\n");
my $value = shift;
my $internal = shift || 0; # internal use so that setting max depth isn't permanent
die("No value provided to dump_setting for $method\n") unless defined $value;
delete $_settings{_rc_settings}{dumper}{$method}
if exists $_settings{_rc_settings}{dumper}{$method} && !$internal;
require Data::Dumper;
{
no strict;
${"Data::Dumper::$method"} = $value;
}
}
=head2 enable
=over 4
=item Description
Erik::enable();
Erik::enable('Module::Name::A', 'Module::Name::B');
Toggles the on state of the debugger.
If a list of modules name(s) is provided then only enable debugging in those. Calling it again without a list will enable debugging everywhere.
=back
=cut
sub enable {
my @modules = @_;
if (@modules) {
%class_restrictions = ( enable => \@modules );
}
else {
%class_restrictions = ( none => 1 );
}
$_settings{state} = 1;
}
=head2 evaluate
=over 4
=item Description
Erik::evaluate(sub { $line_of_code });
If you believe that something is going wrong, but somewhere the error is being
thrown away this method attempts to show you that errror first.
It will most likely disrupt the running of the rest of your script, but if you
are resorting to using it then your script is already crippled.
=back
=cut
sub evaluate {
my $sub = shift;
eval { &$sub; };
if ($@) {
log("Eval produced error: $@");
}
else {
log("no errors during eval");
}
}
=head2 log
=over 4
=item Description
Erik::log();
Erik::log('message');
Simply print a line with the following format:
*** file_name [line_num]: message if provided *********************************
=back
=cut
sub log {
my $string = shift;
chomp($string);
my @data = caller;
# need to get out of the Erik namespace to find the line that we are really talking about
my $stack_level = 1;
while ($data[0] eq 'Erik') {
@data = caller $stack_level++;
}
_print(_header("$data[1] [$data[2]]" . (defined($string) ? ": $string" : '')));
}
=head2 method
=over 4
=item Description
Erik::method();
Simply print a line with the following format:
*** file_name [line_num]: Subroutine_Name **************************************
also adds the method to the method report
=back
=cut
sub method {
my @data = caller;
my $string = "$data[1] [$data[2]]: ";
@data = caller 1;
my ($subroutine) = $data[3] =~ /([^:]+)$/;
$_subroutine_report{$subroutine}++;
$string .= $subroutine;
_print(_header($string));
}
=head2 min
=over 4
=item Description
Erik::min('message');
A compact way of printing things out. Ideally used in a loop so that you don't
burn through the scrollback buffer.
=back
=cut
sub min {
my $string = shift;
if ($_settings{_min_mode}) {
$string = ", $string";
}
else {
$_settings{_min_mode} = 1;
}
_print($string);
}
=head2 module_location
=over 4
=item Description
Erik::module_location();
Erik::module_location('carp');
Erik::module_location('ssl');
Will print out all loaded modules (that match case-insensitive to the provided string) and the locations of their code.
=back
=cut
sub module_location {
my $search_arg = shift || '';
my $name = 'Module Location';
_print(_header($name));
my $found = 0;
KEY: foreach my $key (sort {uc($a) cmp uc($b)} keys %INC) {
next KEY if $search_arg && $key !~ /$search_arg/i;
_print($key . ' => ' . $INC{$key} . "\n");
$found = 1;
}
_print("Search arg ($search_arg) no found in \%INC\n") unless $found;
_print(_header("END: $name"));
}
=head2 print_file
=over 4
=item Description
Erik::print_file($filename);
Print out the content of the file.
=back
=cut
sub print_file {
my $filename = shift;
die("$filename does not exists") unless -e $filename;
die("$filename is not a file") unless -f $filename;
my $contents;
open(my $fh, '<', $filename) || die("Unable to open $filename for read: $!\n");
{
$/ = undef;
$contents = <$fh>;
}
close($fh);
_print(_header("BEGIN: $filename"));
_print($contents);
_print(_header("END: $filename"));
}
=head2 publish
=over 4
=item Description
Erik::publish("Optional last message");
Prints out everything that has been 'append'ed.
It will then reset the internal store.
Each piece of information will be seperate by 'publish_seperator'.
=back
=cut
sub publish {
my $string = shift || '';
append($string);
&log(join($_settings{_publish_separator}, @{$_settings{_publish}}));
delete $_settings{_publish};
}
=head2 publish_separator
=over 4
=item Description
Erik::publish_separator("|");
Set the separator that 'publish' will use when printing everything out.
Default: ' :: ';
=back
=cut
sub publish_separator {
my $separator = shift || return '';
$_settings{_publish_separator} = $separator;
}
=head2 single_off
=over 4
=item Description
Erik::single_off();
Turns off debugging for the next command then it's turned back on again. If debugging is off already then it does nothing.
=back
=cut
sub single_off { $_settings{state} = -1 if $_settings{state}; }
=head2 spacer
=over 4
=item Description
Erik::spacer;
Erik::spacer(3);
Enter 1 or more new lines to help break up the output
=back
=cut
sub spacer {
my $count = shift || 1;
my $tmp_line = $_settings{line};
my $tmp_pid = $_settings{pid};
$_settings{line} = 0;
$_settings{pid} = 0;
_print("\n" x $count);
$_settings{line} = $tmp_line;
$_settings{pid} = $tmp_pid;
}
=head2 stack_trace
=over 4
=item Description
Erik::stack_trace_limit(9999); # known bug - without this line stack_trace will not print
Erik::stack_trace();
Erik::stack_trace(1);
Erik::stack_trace(5);
Full blown stack trace telling you how you got there. This will only happen once per subroutine - so it's safe to call it in a loop. This can be changed by calling stack_trace_limit.
The argument will limit the number of 'levels' that are displayed for a stack trace. Using a '1' is simply asking what/who has called the current method.
=back
=cut
my %stack_trace_limit = ();
sub stack_trace {
my $display_level = shift || 999999; # # of level's to show in a stack trace
my $level = 1; # level counter
my $limit_reached; # signal that we reached the max # of stack traces for a method
my $output = _header('stack trace');
CALLER: while (my @data = caller($level)) {
$limit_reached = 1, last CALLER if ++$stack_trace_limit{$data[3]} > $_settings{_stack_trace_limit};
last unless $display_level > 0;
if ($level == 1 && $display_level == 1) { # we only want to see what called this instead of full stack trace
$output = 'Caller: ' . join(' - ', @data[0..3]) . "\n";
}
else {
$output .= "Level $level: " . join(' - ', @data[0..3]) . "\n";
}
$display_level--;
$level++;
}
# I'm sure there's a more effecient way of doing this, but I can't think of it right now
$output .= "WARNING: called from main - no stack trace available\n"
if $level == 1;
$output .= _header('end of stack trace') unless $level == 2 && $display_level == 0;
$output = '' if $limit_reached;
_print($output);
}
=head2 stack_trace_limit
=over 4
=item Description
my $limit = Erik::stack_trace_limit();
my $limit = Erik::stack_trace_limit(5);
Set/Get the number of times a stack trace will be printed for a subroutine.
Default is set to 1 so that you don't get a ton of output if a subroutine is called mulitple times.
=back
=cut
sub stack_trace_limit {
my $new_setting = shift || 0;
$_settings{_stack_trace_limit} = $new_setting if $new_setting;
return $_settings{_stack_trace_limit};
}
=head2 toggle
=over 4
=item Description
Erik::toggle();
Toggles the enable/disable state of the debugger. If it's off nothing gets printed.
=back
=cut
sub toggle { $_settings{state} = !$_settings{state}; }
=head2 vars
=over 4
=item Description
Erik::vars('name', $name);
Erik::vars(name => $name);
Erik::vars(name1 => $name1, name2 => $name2);
Print out the line number and then name/value seperated by ':' if more than one pair is given.
=back
=cut
sub vars {
my $args = _prep_args(@_);
my @data = caller;
_print(_noticable(" $data[2] - "
. join("\t", map({"$_: " . _is_defined($args->{$_})} sort {$a cmp $b} keys %$args))));
}
=head2 yell
=over 4
=item Description
Erik::yell('Some information to display');
This will print the information between two lines of '*****'s.
=back
=cut
sub yell {
_print('*'x80, shift, '*'x80 . "\n");
}
=head1 Alias Methods
There are many commands for Erik; these are their aliases.
Over time different method names have been used for the same command per user's requests.
There is no plan on deprecating them; feel free to use these as they suite your mood.
=head2 info
=over 4
=item Description
Erik::info();
An alias for Erik::log(). See log documentation above.
=back
=cut
sub info { goto &log; }
=head2 sanity
=over 4
=item Description
Erik::sanity();
An alias for Erik::log(). See log documentation above.
=back
=cut
sub sanity { goto &log; }
=head2 subroutine
=over 4
=item Description
Erik::subroutine();
An alias for Erik::method(). See method documentation above.
=back
=cut
sub subroutine { goto &method; }
=head2 warn
=over 4
=item Description
Erik::warn();
An alias for Erik::log(). See log documentation above.
=back
=cut
sub warn { goto &log; }
sub _is_defined {
my $var = shift;
$var = '[UNDEF]' unless defined($var);
return $var;
}
sub _header {
return _noticable(shift);
}
sub _noticable {
my $string = shift || return 'nothing passed';
return '*'x3 . " $string " . '*'x(75 - length($string)) . "\n";
}
# Bad name - inspired by The IT Crowd - Season 2 Episode 1
# "I'm Disabled" - Roy
sub _im_disabled {
my $disabled = 1;
return $disabled if $ENV{ERIK_DISABLE};
if (exists $class_restrictions{none}) {
if ($_settings{state} == 1) {
$disabled = 0;
}
elsif ($_settings{state} == -1) {
$_settings{state} = 1;
}
}
else {
my $calling_namespace = '';
my $level = 1;
CALLER: while (my @data = caller($level++)) {
next CALLER if $data[0] eq 'Erik';
$calling_namespace = $data[0];
last CALLER;
}
if (exists $class_restrictions{disable}) {
$disabled = 0 unless grep { $calling_namespace eq $_ } @{$class_restrictions{disable}};
}
else {
$disabled = 0 if grep { $calling_namespace eq $_ } @{$class_restrictions{enable}};
}
}
return $disabled;
}
sub _print {
return if _im_disabled();
if ($_settings{_min_mode} && (caller(1))[3] ne 'Erik::min') {
$_settings{_min_mode} = 0;
_print("\n");
}
if (!$_settings{_header_printed}) {
if ($_settings{stderr}) {
print(STDERR _get_header());
}
elsif ($_settings{log}) {
open(LOG, '>>', $_settings{log_filename}) || die("Can't open file ($_settings{log_filename}): $!\n");
print(LOG _get_header());
close(LOG);
}
else {
print(_get_header());
}
$_settings{_header_printed} = 1;
}
my $output = join("\n", @_);
if ($_settings{line} && (caller(1))[3] ne 'Erik::log') {
my @data = caller(1);
$output = _header("$data[1] [$data[2]]") . $output;
}
if ($_settings{pid}) {
$output = "[$$." . ++$_settings{pid_counters}{$$} . '] ' . $output;
}
my $time_current = time;
my $total_time = $time_current - $_settings{_time_start};
my $diff_time = $time_current - $_settings{_time_last};
$_settings{_time_last} = $time_current;
if ($_settings{epoch} || $_settings{time}) {
$time_current = localtime if $_settings{time};
$time_current .= " - $diff_time - $total_time" if $_settings{time_stats};
$output = "[$time_current] " . $output;
}
elsif ($_settings{time_stats}) {
$output = "[$diff_time - $total_time] " . $output;
}
$output = _html_friendly($output) if $_settings{mode} eq 'html';
if ($_settings{logger}) {
require Log::Log4perl;
$_settings{_logger} ||= Log::Log4perl->get_logger;
$_settings{_logger}->debug($output);
}
if ($_settings{stderr}) {
print(STDERR $output);
}
elsif ($_settings{log}) {