source: main/trunk/greenstone2/perllib/cpan/Mojo/Log.pm

Last change on this file was 32205, checked in by ak19, 6 years ago

First set of commits to do with implementing the new 'paged_html' output option of PDFPlugin that uses using xpdftools' new pdftohtml. So far tested only on Linux (64 bit), but things work there so I'm optimistically committing the changes since they work. 2. Committing the pre-built Linux binaries of XPDFtools for both 32 and 64 bit built by the XPDF group. 2. To use the correct bitness variant of xpdftools, setup.bash now exports the BITNESS env var, consulted by gsConvert.pl. 3. All the perl code changes to do with using xpdf tools' pdftohtml to generate paged_html and feed it in the desired form into GS(3): gsConvert.pl, PDFPlugin.pm and its parent ConvertBinaryPFile.pm have been modified to make it all work. xpdftools' pdftohtml generates a folder containing an html file and a screenshot for each page in a PDF (as well as an index.html linking to each page's html). However, we want a single html file that contains each individual 'page' html's content in a div, and need to do some further HTML style, attribute and structure modifications to massage the xpdftool output to what we want for GS. In order to parse and manipulate the HTML 'DOM' to do this, we're using the Mojo::DOM package that Dr Bainbridge found and which he's compiled up. Mojo::DOM is therefore also committed in this revision. Some further changes and some display fixes are required, but need to check with the others about that.

File size: 5.7 KB
Line 
1package Mojo::Log;
2use Mojo::Base 'Mojo::EventEmitter';
3
4use Carp 'croak';
5use Fcntl ':flock';
6use Mojo::File;
7use Mojo::Util 'encode';
8
9has format => sub { shift->short ? \&_short : \&_default };
10has handle => sub {
11
12 # STDERR
13 return \*STDERR unless my $path = shift->path;
14
15 # File
16 return Mojo::File->new($path)->open('>>');
17};
18has history => sub { [] };
19has level => 'debug';
20has max_history_size => 10;
21has 'path';
22has short => sub { $ENV{MOJO_LOG_SHORT} };
23
24# Supported log levels
25my %LEVEL = (debug => 1, info => 2, warn => 3, error => 4, fatal => 5);
26
27# Systemd magic numbers
28my %MAGIC = (debug => 7, info => 6, warn => 4, error => 3, fatal => 2);
29
30sub append {
31 my ($self, $msg) = @_;
32
33 return unless my $handle = $self->handle;
34 flock $handle, LOCK_EX;
35 $handle->print(encode('UTF-8', $msg)) or croak "Can't write to log: $!";
36 flock $handle, LOCK_UN;
37}
38
39sub debug { shift->_log(debug => @_) }
40sub error { shift->_log(error => @_) }
41sub fatal { shift->_log(fatal => @_) }
42sub info { shift->_log(info => @_) }
43
44sub is_level { $LEVEL{pop()} >= $LEVEL{$ENV{MOJO_LOG_LEVEL} || shift->level} }
45
46sub new {
47 my $self = shift->SUPER::new(@_);
48 $self->on(message => \&_message);
49 return $self;
50}
51
52sub warn { shift->_log(warn => @_) }
53
54sub _default {
55 '[' . localtime(shift) . '] [' . shift() . '] ' . join "\n", @_, '';
56}
57
58sub _log { shift->emit('message', shift, @_) }
59
60sub _message {
61 my ($self, $level) = (shift, shift);
62
63 return unless $self->is_level($level);
64
65 my $max = $self->max_history_size;
66 my $history = $self->history;
67 push @$history, my $msg = [time, $level, @_];
68 shift @$history while @$history > $max;
69
70 $self->append($self->format->(@$msg));
71}
72
73sub _short {
74 my ($time, $level) = (shift, shift);
75 my ($magic, $short) = ("<$MAGIC{$level}>", substr($level, 0, 1));
76 return "${magic}[$short] " . join("\n$magic", @_) . "\n";
77}
78
791;
80
81=encoding utf8
82
83=head1 NAME
84
85Mojo::Log - Simple logger
86
87=head1 SYNOPSIS
88
89 use Mojo::Log;
90
91 # Log to STDERR
92 my $log = Mojo::Log->new;
93
94 # Customize log file location and minimum log level
95 my $log = Mojo::Log->new(path => '/var/log/mojo.log', level => 'warn');
96
97 # Log messages
98 $log->debug('Not sure what is happening here');
99 $log->info('FYI: it happened again');
100 $log->warn('This might be a problem');
101 $log->error('Garden variety error');
102 $log->fatal('Boom');
103
104=head1 DESCRIPTION
105
106L<Mojo::Log> is a simple logger for L<Mojo> projects.
107
108=head1 EVENTS
109
110L<Mojo::Log> inherits all events from L<Mojo::EventEmitter> and can emit the
111following new ones.
112
113=head2 message
114
115 $log->on(message => sub {
116 my ($log, $level, @lines) = @_;
117 ...
118 });
119
120Emitted when a new message gets logged.
121
122 $log->on(message => sub {
123 my ($log, $level, @lines) = @_;
124 say "$level: ", @lines;
125 });
126
127=head1 ATTRIBUTES
128
129L<Mojo::Log> implements the following attributes.
130
131=head2 format
132
133 my $cb = $log->format;
134 $log = $log->format(sub {...});
135
136A callback for formatting log messages.
137
138 $log->format(sub {
139 my ($time, $level, @lines) = @_;
140 return "[Thu May 15 17:47:04 2014] [info] I ♥ Mojolicious\n";
141 });
142
143=head2 handle
144
145 my $handle = $log->handle;
146 $log = $log->handle(IO::Handle->new);
147
148Log filehandle used by default L</"message"> event, defaults to opening
149L</"path"> or C<STDERR>.
150
151=head2 history
152
153 my $history = $log->history;
154 $log = $log->history([[time, 'debug', 'That went wrong']]);
155
156The last few logged messages.
157
158=head2 level
159
160 my $level = $log->level;
161 $log = $log->level('debug');
162
163Active log level, defaults to C<debug>. Available log levels are C<debug>,
164C<info>, C<warn>, C<error> and C<fatal>, in that order. Note that the
165C<MOJO_LOG_LEVEL> environment variable can override this value.
166
167=head2 max_history_size
168
169 my $size = $log->max_history_size;
170 $log = $log->max_history_size(5);
171
172Maximum number of logged messages to store in L</"history">, defaults to C<10>.
173
174=head2 path
175
176 my $path = $log->path
177 $log = $log->path('/var/log/mojo.log');
178
179Log file path used by L</"handle">.
180
181=head2 short
182
183 my $bool = $log->short;
184 $log = $log->short($bool);
185
186Generate short log messages without a timestamp, suitable for systemd, defaults
187to the value of the C<MOJO_LOG_SHORT> environment variables.
188
189=head1 METHODS
190
191L<Mojo::Log> inherits all methods from L<Mojo::EventEmitter> and implements the
192following new ones.
193
194=head2 append
195
196 $log->append("[Thu May 15 17:47:04 2014] [info] I ♥ Mojolicious\n");
197
198Append message to L</"handle">.
199
200=head2 debug
201
202 $log = $log->debug('You screwed up, but that is ok');
203 $log = $log->debug('All', 'cool');
204
205Emit L</"message"> event and log C<debug> message.
206
207=head2 error
208
209 $log = $log->error('You really screwed up this time');
210 $log = $log->error('Wow', 'seriously');
211
212Emit L</"message"> event and log C<error> message.
213
214=head2 fatal
215
216 $log = $log->fatal('Its over...');
217 $log = $log->fatal('Bye', 'bye');
218
219Emit L</"message"> event and log C<fatal> message.
220
221=head2 info
222
223 $log = $log->info('You are bad, but you prolly know already');
224 $log = $log->info('Ok', 'then');
225
226Emit L</"message"> event and log C<info> message.
227
228=head2 is_level
229
230 my $bool = $log->is_level('debug');
231
232Check active log L</"level">.
233
234 # True
235 $log->level('debug')->is_level('debug');
236 $log->level('debug')->is_level('info');
237
238 # False
239 $log->level('info')->is_level('debug');
240 $log->level('fatal')->is_level('warn');
241
242=head2 new
243
244 my $log = Mojo::Log->new;
245 my $log = Mojo::Log->new(level => 'warn');
246 my $log = Mojo::Log->new({level => 'warn'});
247
248Construct a new L<Mojo::Log> object and subscribe to L</"message"> event with
249default logger.
250
251=head2 warn
252
253 $log = $log->warn('Dont do that Dave...');
254 $log = $log->warn('No', 'really');
255
256Emit L</"message"> event and log C<warn> message.
257
258=head1 SEE ALSO
259
260L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
261
262=cut
Note: See TracBrowser for help on using the repository browser.