File: test.pl

package info (click to toggle)
liblog-loglite-perl 0.82-8
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 112 kB
  • sloc: perl: 327; makefile: 2
file content (114 lines) | stat: -rw-r--r-- 2,535 bytes parent folder | download | duplicates (8)
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
# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl test.pl'

BEGIN { $| = 1; print "1..6\n"; }
END {print "not ok 1\n" unless $loaded;}
use Log::LogLite;
use Log::NullLogLite;
$loaded = 1;
print "ok 1\n";

# create a log file with default level 5
my $log = new Log::LogLite("test.log");
$log->write("message number 1"); # should be in the log
$log->write("message number 2", 4); # should be in the log
$log->write("message number 3", 5); # should be in the log
$log->write("message number 4", 6); # should not be in the log

$log = undef; # close the log

# read the log file and check it.
open(LOG, "test.log");
my @lines = <LOG>;
close(LOG);

if ($lines[0] =~ /\[[^\]]+\] <\-> message number 1/ &&
    $lines[1] =~ /\[[^\]]+\] <4> message number 2/ &&
    $lines[2] =~ /\[[^\]]+\] <5> message number 3/) {
    print "ok 2\n";
}
else {
    print "not ok 2\n";
}


# remove the log file 
unlink("test.log");

# create a new log with 6 as default level
$log = new Log::LogLite("test.log", 6);
$log->write("message number 5", 6); # should be in the log
$log->write("message number 6", 7); # should not be in the log

$log = undef; # close the log

# read the log file and check it
open(LOG, "test.log");
@lines = <LOG>;
close(LOG);

if ($lines[0] =~ /\[[^\]]+\] <6> message number 5/) {
    print "ok 3\n";
}
else {
    print "not ok 3\n";
}

# remove the log file
unlink("test.log");

# create a new log 
$log = new Log::LogLite("test.log");

# change the default message
$log->default_message("message "); 
$log->write("number 7"); # should be in the log

$log = undef; # close the log

# read the log file and check it
open(LOG, "test.log");
@lines = <LOG>;
close(LOG);

if ($lines[0] =~ /\[[^\]]+\] <\-> message number 7/) {
    print "ok 4\n";
}
else {
    print "not ok 4\n";
}

# remove the log file
unlink("test.log");

# create a new log 
$log = new Log::LogLite("test.log");

# change the template
$log->template("<level>:[<date>]: <default_message><message>\n");
$log->write("message number 8"); # should be in the log

$log = undef; # close the log

# read the log file and check it
open(LOG, "test.log");
@lines = <LOG>;
close(LOG);

if ($lines[0] =~ /\-:\[[^\]]+\]: message number 8/) {
    print "ok 5\n";
}
else {
    print "not ok 5\n";
}

# remove the log file
unlink("test.log");

# create a null log 
$log = new Log::NullLogLite();
$log->write("this message will never be written");
print "ok 6\n"; # if we are here, it must be ok.