File: File.pm

package info (click to toggle)
liblog-handler-perl 0.90-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 576 kB
  • sloc: perl: 2,758; makefile: 4
file content (495 lines) | stat: -rw-r----- 11,475 bytes parent folder | download | duplicates (3)
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
=head1 NAME

Log::Handler::Output::File - Log messages to a file.

=head1 SYNOPSIS

    use Log::Handler::Output::File;

    my $log = Log::Handler::Output::File->new(
        filename    => "file.log",
        filelock    => 1,
        fileopen    => 1,
        reopen      => 1,
        mode        => "append",
        autoflush   => 1,
        permissions => "0664",
        utf8        => 0,
    );

    $log->log(message => $message);

=head1 DESCRIPTION

Log messages to a file.

=head1 METHODS

=head2 new()

Call C<new()> to create a new Log::Handler::Output::File object.

The following options are possible:

=over 4

=item B<filename>

With C<filename> you can set a file name as a string or as a array reference.
If you set a array reference then the parts will be concat with C<catfile> from
C<File::Spec>.

Set a file name:

    my $log = Log::Handler::Output::File->new( filename => "file.log"  );

Set a array reference:

    my $log = Log::Handler::Output::File->new(

        # foo/bar/baz.log
        filename => [ "foo", "bar", "baz.log" ],

        # /foo/bar/baz.log
        filename => [ "", "foo", "bar", "baz.log" ],

    );

=item B<filelock>

Maybe it's desirable to lock the log file by each write operation because a lot
of processes write at the same time to the log file. You can set the option
C<filelock> to 0 or 1.

    0 - no file lock
    1 - exclusive lock (LOCK_EX) and unlock (LOCK_UN) by each write operation (default)

=item B<fileopen>

Open a log file transient or permanent.

    0 - open and close the logfile by each write operation
    1 - open the logfile if C<new()> called and try to reopen the
        file if C<reopen> is set to 1 and the inode of the file has changed (default)

=item B<reopen>

This option works only if option C<fileopen> is set to 1.

    0 - deactivated
    1 - try to reopen the log file if the inode changed (default)

=item How to use B<fileopen> and B<reopen>

Please note that it's better to set C<reopen> and C<fileopen> to 0 on Windows
because Windows unfortunately haven't the faintest idea of inodes.

To write your code independent you should control it:

    my $os_is_win = $^O =~ /win/i ? 0 : 1;

    my $log = Log::Handler::Output::File->new(
       filename => "file.log",
       mode     => "append",
       fileopen => $os_is_win
    );

If you set C<fileopen> to 0 then it implies that C<reopen> has no importance.

=item B<mode>

There are three possible modes to open a log file.

    append - O_WRONLY | O_APPEND | O_CREAT (default)
    excl   - O_WRONLY | O_EXCL   | O_CREAT
    trunc  - O_WRONLY | O_TRUNC  | O_CREAT

C<append> would open the log file in any case and appends the messages at
the end of the log file.

C<excl> would fail by open the log file if the log file already exists.

C<trunc> would truncate the complete log file if it exists. Please take care
to use this option.

Take a look to the documentation of C<sysopen()> to get more information.

=item B<autoflush>

    0 - autoflush off
    1 - autoflush on (default)

=item B<permissions>

The option C<permissions> sets the permission of the file if it creates and
must be set as a octal value. The permission need to be in octal and are
modified by your process's current "umask".

That means that you have to use the unix style permissions such as C<chmod>.
C<0640> is the default permission for this option. That means that the owner
got read and write permissions and users in the same group got only read
permissions. All other users got no access.

Take a look to the documentation of C<sysopen()> to get more information.

=item B<utf8>, B<utf-8>

    utf8   =  binmode, $fh, ":utf8";
    utf-8  =  binmode, $fh, "encoding(utf-8)"; 

Yes, there is a difference.

L<http://perldoc.perl.org/perldiag.html#Malformed-UTF-8-character-(%25s)>

L<http://perldoc.perl.org/Encode.html#UTF-8-vs.-utf8-vs.-UTF8>

=item B<dateext>

It's possible to set a pattern in the filename that is replaced with a date.
If the date - and the filename - changed the file is closed and reopened with
the new filename. The filename is converted with C<POSIX::strftime>.

Example:

    my $log = Log::Handler::Output::File->new(
        filename  => "file-%Y-%m-%d.log",
        dateext => 1
    );

In this example the file C<file-2015-06-12.log> is created. At the next day the filename
changed, the log file C<file-2015-06-12.log> is closed and C<file-2015-06-13.log> is opened.

This feature is a small improvement for systems where no logrotate is available like Windows
systems. On this way you have the chance to delete old log files without to stop/start a
daemon.

=back

=head2 log()

Call C<log()> if you want to log messages to the log file.

Example:

    $log->log(message => "this message goes to the logfile");

=head2 flush()

Call C<flush()> if you want to re-open the log file.

This is useful if you don't want to use option S<"reopen">. As example
if a rotate mechanism moves the logfile and you want to re-open a new
one.

=head2 validate()

Validate a configuration.

=head2 reload()

Reload with a new configuration.

=head2 errstr()

Call C<errstr()> to get the last error message.

=head2 close()

Call C<close()> to close the log file yourself - normally you don't need
to use it, because the log file will be opened and closed automatically.

=head1 PREREQUISITES

    Carp
    Fcntl
    File::Spec
    Params::Validate

=head1 EXPORTS

No exports.

=head1 REPORT BUGS

Please report all bugs to <jschulz.cpan(at)bloonix.de>.

If you send me a mail then add Log::Handler into the subject.

=head1 AUTHOR

Jonny Schulz <jschulz.cpan(at)bloonix.de>.

=head1 COPYRIGHT

Copyright (C) 2007-2009 by Jonny Schulz. All rights reserved.

This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

=cut

package Log::Handler::Output::File;

use strict;
use warnings;
use Carp;
use Fcntl qw( :flock O_WRONLY O_APPEND O_TRUNC O_EXCL O_CREAT );
use File::Spec;
use Params::Validate qw();
use POSIX;

our $VERSION = "0.08";
our $ERRSTR  = "";

sub new {
    my $class = shift;
    my $opts = $class->_validate(@_);
    my $self = bless $opts, $class;

    # open the log file permanent
    if ($self->{dateext}) {
        $self->_check_dateext
            or return undef;
    } elsif ($self->{fileopen}) {
        $self->_open
            or croak $self->errstr;
    }

    return $self;
}

sub log {
    my $self = shift;
    my $message = @_ > 1 ? {@_} : shift;

    if ($self->{dateext}) {
        $self->_check_dateext or return undef;
    }

    if (!$self->{fileopen}) {
        $self->_open or return undef;
    } elsif ($self->{reopen}) {
        $self->_checkino or return undef;
    }

    if ($self->{filelock}) {
        flock($self->{fh}, LOCK_EX)
            or return $self->_raise_error("unable to lock logfile $self->{filename}: $!");
    }

    print {$self->{fh}} $message->{message} or
        return $self->_raise_error("unable to print to logfile: $!");

    if ($self->{filelock}) {
        flock($self->{fh}, LOCK_UN)
            or return $self->_raise_error("unable to unlock logfile $self->{filename}: $!");
    }

    if (!$self->{fileopen}) {
        $self->close or return undef;
    }

    return 1;
}

sub flush {
    my $self = shift;

    if ($self->{fileopen}) {
        $self->close or return undef;
        $self->_open  or return undef;
    }

    return 1;
}

sub close {
    my $self = shift;

    if ($self->{fh}) {
        CORE::close($self->{fh})
            or return $self->_raise_error("unable to close logfile $self->{filename}: $!");
        delete $self->{fh};
    }

    return 1;
}

sub validate {
    my $self = shift;
    my $opts = ();

    eval { $opts = $self->_validate(@_) };

    if ($@) {
        return $self->_raise_error($@);
    }

    return $opts;
}

sub reload {
    my $self = shift;
    my $opts = $self->validate(@_);

    $self->close;

    foreach my $key (keys %$opts) {
        $self->{$key} = $opts->{$key};
    }

    if ($self->{fileopen}) {
        $self->_open
            or croak $self->errstr;
    }

    return 1;
}

sub errstr {
    return $ERRSTR;
}

sub DESTROY {
    my $self = shift;

    if ($self->{fh}) {
        CORE::close($self->{fh});
    }
}

#
# private stuff
#

sub _open {
    my $self = shift;

    sysopen(my $fh, $self->{filename}, $self->{mode}, $self->{permissions})
        or return $self->_raise_error("unable to open logfile $self->{filename}: $!");

    if ($self->{autoflush}) {
        my $oldfh = select $fh;
        $| = $self->{autoflush};
        select $oldfh;
    }

    if ($self->{utf8}) {
        binmode $fh, ":utf8";
    } elsif ($self->{"utf-8"}) {
        binmode $fh, "encoding(utf-8)";
    }

    if ($self->{reopen}) {
        $self->{inode} = (stat($self->{filename}))[1];
    }

    $self->{fh} = $fh;
    return 1;
}

sub _check_dateext {
    my $self = shift;

    my $filename = POSIX::strftime($self->{filename_pattern}, localtime);

    if ($self->{filename} ne $filename) {
        $self->{filename} = $filename;
        if ($self->{fileopen}) {
            $self->close or return undef;
            $self->_open or return undef;
        }
    }

    return 1;
}

sub _checkino {
    my $self = shift;

    if (!-e $self->{filename} || $self->{inode} != (stat($self->{filename}))[1]) {
        $self->close or return undef;
        $self->_open or return undef;
    }

    return 1;
}

sub _validate {
    my $class   = shift;
    my $bool_rx = qr/^[10]\z/;

    my %opts = Params::Validate::validate(@_, {
        filename => {
            type => Params::Validate::SCALAR | Params::Validate::ARRAYREF,
        },
        filelock => {
            type => Params::Validate::SCALAR,
            regex => $bool_rx,
            default => 1,
        },
        fileopen => {
            type => Params::Validate::SCALAR,
            regex => $bool_rx,
            default => 1,
        },
        reopen => {
            type  => Params::Validate::SCALAR,
            regex => $bool_rx,
            default => 1,
        },
        mode => {
            type => Params::Validate::SCALAR,
            regex => qr/^(append|excl|trunc)\z/,
            default => "append",
        },
        autoflush => {
            type => Params::Validate::SCALAR,
            regex => $bool_rx,
            default => 1,
        },
        permissions => {
            type => Params::Validate::SCALAR,
            regex => qr/^[0-7]{3,4}\z/,
            default => "0640",
        },
        utf8 => {
            type => Params::Validate::SCALAR,
            regex => $bool_rx,
            default => 0,
        },
        "utf-8" => {
            type => Params::Validate::SCALAR,
            regex => $bool_rx,
            default => 0,
        },
        dateext => {
            type => Params::Validate::SCALAR,
            optional => 1
        }
    });

    if (ref($opts{filename}) eq "ARRAY") {
        $opts{filename} = File::Spec->catfile(@{$opts{filename}});
    }

    if ($opts{mode} eq "append") {
        $opts{mode} = O_WRONLY | O_APPEND | O_CREAT;
    } elsif ($opts{mode} eq "excl") {
        $opts{mode} = O_WRONLY | O_EXCL | O_CREAT;
    } elsif ($opts{mode} eq "trunc") {
        $opts{mode} = O_WRONLY | O_TRUNC | O_CREAT;
    }

    $opts{permissions} = oct($opts{permissions});
    $opts{filename_pattern} = $opts{filename};

    return \%opts;
}

sub _raise_error {
    $ERRSTR = $_[1];
    return undef;
}

1;