summaryrefslogtreecommitdiff
path: root/fml/lib/FML/Log.pm
blob: 98d238a8bdac4b391f41bcd6bfef766e4590d5d7 (plain)
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
#-*- perl -*-
#
# Copyright (C) 2000 Ken'ichi Fukamachi
#
# $Id$
# $FML$
#

package FML::Log;
require Exporter;

@ISA       = qw(Exporter);
@EXPORT    = qw(Log);
@EXPORT_OK = qw(Log);

use strict;
use Carp;
use FML::Config;
use FML::Date;
use FML::Credential;

#  usage: &Log( message, { log_file => $log_file } );
# return: none
#
sub Log
{
    my ($mesg, $args) = @_;
    my $config = new FML::Config;

    # parse arguments
    my $log_file = $args->{ log_file };
    my $facility = $args->{ facility };
    my $level    = $args->{ level };

    # invalid calling
    $mesg || return undef ;

    # reference to "date" object
    my $rdate = new FML::Date;

    # open the $file by using FileHandle.pm
    use FileHandle;

    # When the second argument is not defined, use the default log_file.
    my $file   = $log_file || $config->{ log_file } || '/dev/stderr';
    my $fh     = new FileHandle ">> $file";
    my $sender = FML::Credential->sender;

    if (defined $fh) {
	print $fh $rdate->{'log_file_style'}, " ", $mesg;
	print $fh " ($sender)" if defined $sender;
	print $fh "\n";
    }
    else {
	croak "Error: cannot open $file\n";
    }
}



=head1 NAME

FML::Log.pm - several interfaces to open several files


=head1 SYNOPSIS

To import Log(),

   use FML::Log qw(Log);
   &Log( $log_message );

or specify arguments in the hash reference

   use FML::Log qw(Log);
   &Log( $log_message , { 
       log_file => $log_file,
       facility => $facility,
       level    => $level,
   });


=head1 DESCRIPTION

FML::Log.pm contains several interfaces for several files,
for example, log files, syslog() (not yet implemented).

=item Log( $message )

The argument is the message to log.



=head1 SEE ALSO

L<FML::Date>, 
L<FML::Config>,
L<FML::BaseSystem>,
L<FileHandle>

=head1 AUTHOR

Ken'ichi Fukamachi <F<fukachan@fml.org>>


=head1 COPYRIGHT

Copyright (C) 2000 Ken'ichi Fukamachi

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


=head1 HISTORY

FML::Log.pm appeared in fml5.


=cut

1;