1# Device::Modem::Log::File - Text files logging plugin for Device::Modem class
2#
3# Copyright (C) 2002-2004 Cosimo Streppone, cosimo@cpan.org
4#
5# This program is free software; you can redistribute it and/or
6# modify it under the same terms as Perl itself.
7#
8# Additionally, this is ALPHA software, still needs extensive
9# testing and support for generic AT commads, so use it at your own risk,
10# and without ANY warranty! Have fun.
11#
12# $Id$
13#
14package Device::Modem::Log::File;
15our $VERSION = '2.11';
16$VERSION = eval $VERSION;
17
18use strict;
19use File::Path     ();
20use File::Basename ();
21use IO::Handle;
22
23# Define log levels like syslog service
24our %levels = ( debug => 7, info => 6, notice => 5, warning => 4, err => 3, error => 3, crit => 2, alert => 1, emerg => 0 );
25
26sub new {
27	my( $class, $package, $filename ) = @_;
28
29	# Get a decent default if no file available
30	$filename ||= default_filename();
31
32	my %obj = (
33		file => $filename,
34		loglevel => 'info'
35	);
36
37	my $self = bless \%obj, 'Device::Modem::Log::File';
38
39	# Open file at the start and save reference
40	my $LOGFILE = new IO::Handle;
41	if( open( $LOGFILE, '>>'.$self->{'file'} ) ) {
42
43		$self->{'fh'} = $LOGFILE;
44
45		# Unbuffer writes to logfile
46		my $oldfh = select $self->{'fh'};
47		$| = 1;
48		select $oldfh;
49
50	} else {
51		warn('Could not open '.$self->{'file'}.' to start logging');
52	}
53
54	return $self;
55}
56
57# Provide a suitable filename default
58sub default_filename () {
59	my $dir = '/tmp';
60
61	# If this is windows, use the temp/tmp dirs
62	if (exists $ENV{'TEMP'} || exists $ENV{'TMP'}) {
63		$dir = $ENV{'TEMP'} || $ENV{'TMP'};
64	}
65
66	return "$dir/modem.log";
67}
68
69sub filename {
70	my $self = shift();
71	$self->{'file'} ||= $self->default_filename();
72
73	if( ! -d File::Basename::dirname($self->{'file'}) ) {
74		File::Path::mkpath( File::Basename::dirname($self->{'file'}), 0, 0755 );
75	}
76
77	return $self->{'file'};
78}
79
80
81sub loglevel {
82	my($self, $newlevel) = @_;
83
84	if( defined $newlevel ) {
85		$newlevel = lc $newlevel;
86		if( ! exists $levels{$newlevel} ) {
87			$newlevel = 'warning';
88		}
89		$self->{'loglevel'} = $newlevel;
90	} else {
91		return $self->{'loglevel'};
92	}
93}
94
95sub write($$) {
96
97	my($self, $level, @msg) = @_;
98
99	# If log level mask allows it, log given message
100	#warn('message level='.$level.' ('.$levels{$level}.')    loglevel='.$self->{loglevel}.' ('.$levels{$self->{loglevel}}.')');
101	if( $levels{$level} <= $levels{$self->{'loglevel'}} ) {
102
103		if( my $fh = $self->fh() ) {
104			map { tr/\r\n/^M/s } @msg;
105			print $fh join("\t", scalar localtime, $0, $level, @msg), "\n";
106		} else {
107			warn('cannot log '.$level.' '.join("\t",@msg).' to file: '.$! );
108		}
109
110	}
111
112}
113
114sub fh {
115	my $self = shift;
116	return $self->{'fh'};
117}
118
119# Closes log file opened in new()
120sub close {
121	my $self = shift;
122	my $fh = $self->{'fh'};
123	close $fh;
124	undef $self->{'fh'};
125}
126
1271;
128
129
130
131__END__
132
133
134
135=head1 NAME
136
137Device::Modem::Log::File - Text files logging plugin for Device::Modem class
138
139=head1 SYNOPSIS
140
141  use Device::Modem;
142
143  my $box = Device::Modem->new( log => 'file', ... );
144  my $box = Device::Modem->new( log => 'file,name=/tmp/mymodem.log', ... );
145  ...
146
147=head1 DESCRIPTION
148
149This is meant for an example log class to be hooked to C<Device::Modem>
150to provide one's favourite logging mechanism.
151You just have to implement your own C<new()>, C<write()> and C<close()> methods.
152
153Default text file is C</tmp/modem.log>. On Windows platforms, this
154goes into C<%TEMP%/modem.log> or C<%TMP%/modem.log>, whichever is defined.
155By default, if the folder of the log file does not exist, it is created.
156
157This class is loaded automatically by C<Device::Modem> class when an object
158is instantiated, and it is the B<default> logging mechanism for
159C<Device::Modem> class.
160
161Normally, you should B<not> need to use this class directly, because there
162are many other zillions of modules that do logging better than this.
163
164Also, it should be pondered whether to replace C<Device::Modem::Log::File>
165and mates with those better classes in a somewhat distant future.
166
167=head2 REQUIRES
168
169Device::Modem
170
171=head2 EXPORTS
172
173None
174
175=head1 AUTHOR
176
177Cosimo Streppone, cosimo@cpan.org
178
179=head1 COPYRIGHT
180
181(C) 2002 Cosimo Streppone, <cosimo@cpan.org>
182
183This library is free software; you can only redistribute it and/or
184modify it under the same terms as Perl itself.
185
186=head1 SEE ALSO
187
188L<Device::Modem>
189L<Device::Modem::Log::Syslog>
190
191=cut
192