source: perl/lib/BarnOwl/Logging.pm @ 6e764aa

Last change on this file since 6e764aa was 6e764aa, checked in by Jason Gross <jgross@mit.edu>, 8 years ago
Moved the checking of whether or not to log messages to perl
  • Property mode set to 100644
File size: 4.2 KB
Line 
1use strict;
2use warnings;
3
4package BarnOwl::Logging;
5
6=head1 BarnOwl::Logging
7
8=head1 DESCRIPTION
9
10C<BarnOwl::Logging> implements the internals of logging.  All customizations
11to logging should be done in the appropriate subclass of L<BarnOwl::Message>.
12
13=head2 USAGE
14
15Modules wishing to customize how messages are logged should override the
16relevant subroutines in the appropriate subclass of L<BarnOwl::Message>.
17
18Modules wishing to log errors sending outgoing messages should call
19L<BarnOwl::Logging::log_outgoing_error> with the message that failed
20to be sent.
21
22=head2 EXPORTS
23
24None by default.
25
26=cut
27
28use Exporter;
29
30our @EXPORT_OK = qw();
31
32our %EXPORT_TAGS = (all => [@EXPORT_OK]);
33
34use File::Spec;
35
36=head2 sanitize_filename BASE_PATH FILENAME
37
38Sanitizes C<FILENAME> and concatenates it with C<BASE_PATH>.
39
40In any filename, C<"/"> and any control characters (characters which
41match C<[:cntrl:]> get replaced by underscores.  If the resulting
42filename is empty or equal to C<"."> or C<"..">, it is replaced with
43C<"weird">.
44
45=cut
46
47sub sanitize_filename {
48    my $base_path = BarnOwl::Internal::makepath(shift);
49    my $filename = shift;
50    $filename =~ s/[[:cntrl:]\/]/_/g;
51    if ($filename eq '' || $filename eq '.' || $filename eq '..') {
52        $filename = 'weird';
53    }
54    # The original C code also removed characters less than '!' and
55    # greater than or equal to '~', marked file names beginning with a
56    # non-alphanumeric or non-ASCII character as 'weird', and rejected
57    # filenames longer than 35 characters.
58    return File::Spec->catfile($base_path, $filename);
59}
60
61=head2 get_filenames MESSAGE
62
63Returns a list of filenames in which to log the passed message.
64
65This method calls C<log_filenames> on C<MESSAGE> to determine the list
66of filenames to which C<MESSAGE> gets logged.  All filenames are
67relative to C<MESSAGE->log_base_path>.  If C<MESSAGE->log_to_all_file>
68returns true, then the filename C<"all"> is appended to the list of
69filenames.
70
71Filenames are sanitized by C<sanitize_filename>.
72
73=cut
74
75sub get_filenames {
76    my ($m) = @_;
77    my @filenames = $m->log_filenames;
78    push @filenames, 'all' if $m->log_to_all_file;
79    return map { sanitize_filename($m->log_base_path, $_) } @filenames;
80}
81
82=head2 should_log_message MESSAGE
83
84Determines whether or not the passed message should be logged.
85
86To customize the behavior of this method, override
87L<BarnOwl::Message::should_log>.
88
89=cut
90
91sub should_log_message {
92    my ($m) = @_;
93    # If there's a logfilter and this message matches it, log.
94    # pass quiet=1, because we don't care if the filter doesn't exist
95    return 1 if BarnOwl::message_matches_filter($m, BarnOwl::getvar('logfilter'), 1);
96    # otherwise we do things based on the logging variables
97    # skip login/logout messages if appropriate
98    return 0 if $m->is_loginout && BarnOwl::getvar('loglogins') eq 'off';
99    # check direction
100    return 0 if $m->is_outgoing && BarnOwl::getvar('loggingdirection') eq 'in';
101    return 0 if $m->is_incoming && BarnOwl::getvar('loggingdirection') eq 'out';
102    return $m->should_log;
103}
104
105=head2 log MESSAGE
106
107Call this method to (potentially) log a message.
108
109To customize the behavior of this method for your messages, override
110L<BarnOwl::Message::log>, L<BarnOwl::Message::should_log>,
111L<BarnOwl::Message::log_base_path>, and/or
112L<BarnOwl::Message::log_filenames>.
113
114=cut
115
116sub log {
117    my ($m) = @_;
118    return unless BarnOwl::Logging::should_log_message($m);
119    my $log_text = $m->log;
120    foreach my $filename (BarnOwl::Logging::get_filenames($m)) {
121        BarnOwl::Logging::enqueue_text($log_text, $filename);
122    }
123}
124
125=head2 log_outgoing_error MESSAGE
126
127Call this method to (potentially) log an error in sending an
128outgoing message.  Errors get logged to the same file(s) as
129successful messages.
130
131To customize the behavior of this method for your messages, override
132L<BarnOwl::Message::log_outgoing_error>,
133L<BarnOwl::Message::should_log>,
134L<BarnOwl::Message::log_base_path>, and/or
135L<BarnOwl::Message::log_filenames>.
136
137=cut
138
139sub log_outgoing_error {
140    my ($m) = @_;
141    return unless BarnOwl::Logging::should_log_message($m);
142    my $log_text = $m->log_outgoing_error;
143    foreach my $filename (BarnOwl::Logging::get_filenames($m)) {
144        BarnOwl::Logging::enqueue_text($log_text, $filename);
145    }
146}
147
1481;
Note: See TracBrowser for help on using the repository browser.