forked from os-autoinst/os-autoinst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.pm
68 lines (54 loc) · 1.64 KB
/
log.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
# Copyright SUSE LLC
# SPDX-License-Identifier: GPL-2.0-or-later
package log;
use Mojo::Base -strict, -signatures;
use Carp;
use Mojo::File qw(path);
use Mojo::Log;
use POSIX 'strftime';
use Time::HiRes qw(gettimeofday);
use Time::Moment;
use Term::ANSIColor;
use Exporter 'import';
our @EXPORT_OK = qw(logger init_logger diag fctres fctinfo fctwarn modstate);
our $logger;
our $direct_output;
sub logger () { $logger //= Mojo::Log->new(level => 'debug', format => \&log_format_callback) }
sub init_logger () { logger->path(path('testresults', 'autoinst-log.txt')) unless $direct_output }
sub log_format_callback ($time, $level, @items) {
my $lines = join("\n", @items, '');
# ensure indentation for multi-line output
$lines =~ s/(?<!\A)^/ /gm;
return '[' . Time::Moment->now . "] [$level] [pid:$$] $lines";
}
sub diag (@args) {
confess "missing input" unless @args;
logger->append(color('white'));
$args[-1] .= color('reset');
logger->debug(@args);
return;
}
sub fctres ($text, $fname = undef) {
$fname //= (caller(1))[3];
logger->append(color('green'));
logger->debug(">>> $fname: $text" . color('reset'));
return;
}
sub fctinfo ($text, $fname = undef) {
$fname //= (caller(1))[3];
logger->append(color('yellow'));
logger->info("::: $fname: $text" . color('reset'));
return;
}
sub fctwarn ($text, $fname = undef) {
$fname //= (caller(1))[3];
logger->append(color('red'));
logger->warn("!!! $fname: $text" . color('reset'));
return;
}
sub modstate (@text) {
logger->append(color('bold blue'));
logger->debug("||| @{[join(' ', @text)]}" . color('reset'));
return;
}
1;