source: perl/lib/BarnOwl/Logging.pm @ 1210b54

Last change on this file since 1210b54 was 1210b54, checked in by Jason Gross <jgross@mit.edu>, 8 years ago
Moved the last of the logging-related variables to perl We seem to have picked logpath as the string variable we test in tester.c. Since this is no longer in C, I've replaced it with personalbell (no particular reason for that choice).
  • Property mode set to 100644
File size: 7.7 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$BarnOwl::Hooks::newMessage->add("BarnOwl::Logging::log");
37$BarnOwl::Hooks::startup->add("BarnOwl::Logging::_register_variables");
38
39sub _register_variables {
40    BarnOwl::new_variable_bool('logging',
41        {
42            default     => 0,
43            summary     => 'turn personal logging on or off',
44            description => "If this is set to on, personal messages are\n"
45                         . "logged in the directory specified\n"
46                         . "by the 'logpath' variable.  The filename in that\n"
47                         . "directory is derived from the sender of the message."
48        });
49
50    BarnOwl::new_variable_bool('classlogging',
51        {
52            default     => 0,
53            summary     => 'turn class logging on or off',
54            description => "If this is set to on, class messages are\n"
55                         . "logged in the directory specified by the\n"
56                         . "'classpath' variable.  The filename in that\n"
57                         . "directory is derived from the class to which\n"
58                         . "the message was sent."
59        });
60
61    BarnOwl::new_variable_string('logfilter',
62        {
63            default     => '',
64            summary     => 'name of a filter controlling which messages to log',
65            description => "If non empty, any messages matching the given filter will be logged.\n"
66                         . "This is a completely separate mechanism from the other logging\n"
67                         . "variables like logging, classlogging, loglogins, loggingdirection,\n"
68                         . "etc.  If you want this variable to control all logging, make sure\n"
69                         . "all other logging variables are in their default state."
70        });
71
72    BarnOwl::new_variable_bool('loglogins',
73        {
74            default     => 0,
75            summary     => 'enable logging of login notifications',
76            description => "When this is enabled, BarnOwl will log login and logout notifications\n"
77                         . "for AIM, zephyr, or other protocols.  If disabled BarnOwl will not print\n"
78                         . "login or logout notifications."
79        });
80
81    BarnOwl::new_variable_enum('loggingdirection',
82        {
83            default       => 'both',
84            validsettings => [qw(in out both)],
85            summary       => "specifies which kind of messages should be logged",
86            description   => "Can be one of 'both', 'in', or 'out'.  If 'in' is\n"
87                           . "selected, only incoming messages are logged, if 'out'\n"
88                           . "is selected only outgoing messages are logged.  If 'both'\n"
89                           . "is selected both incoming and outgoing messages are\n"
90                           . "logged."
91        });
92
93    BarnOwl::new_variable_string('logpath',
94        {
95            default       => '~/zlog/people',
96            validsettings => '<path>',
97            summary       => 'path for logging personal messages',
98            description   => "Specifies a directory which must exist.\n"
99                            . "Files will be created in the directory for each sender."
100        });
101
102    BarnOwl::new_variable_string('classlogpath',
103        {
104            default       => '~/zlog/class',
105            validsettings => '<path>',
106            summary       => 'path for logging class zephyrs',
107            description   => "Specifies a directory which must exist.\n"
108                           . "Files will be created in the directory for each class."
109        });
110}
111
112=head2 sanitize_filename BASE_PATH FILENAME
113
114Sanitizes C<FILENAME> and concatenates it with C<BASE_PATH>.
115
116In any filename, C<"/"> and any control characters (characters which
117match C<[:cntrl:]> get replaced by underscores.  If the resulting
118filename is empty or equal to C<"."> or C<"..">, it is replaced with
119C<"weird">.
120
121=cut
122
123sub sanitize_filename {
124    my $base_path = BarnOwl::Internal::makepath(shift);
125    my $filename = shift;
126    $filename =~ s/[[:cntrl:]\/]/_/g;
127    if ($filename eq '' || $filename eq '.' || $filename eq '..') {
128        $filename = 'weird';
129    }
130    # The original C code also removed characters less than '!' and
131    # greater than or equal to '~', marked file names beginning with a
132    # non-alphanumeric or non-ASCII character as 'weird', and rejected
133    # filenames longer than 35 characters.
134    return File::Spec->catfile($base_path, $filename);
135}
136
137=head2 get_filenames MESSAGE
138
139Returns a list of filenames in which to log the passed message.
140
141This method calls C<log_filenames> on C<MESSAGE> to determine the list
142of filenames to which C<MESSAGE> gets logged.  All filenames are
143relative to C<MESSAGE->log_base_path>.  If C<MESSAGE->log_to_all_file>
144returns true, then the filename C<"all"> is appended to the list of
145filenames.
146
147Filenames are sanitized by C<sanitize_filename>.
148
149=cut
150
151sub get_filenames {
152    my ($m) = @_;
153    my @filenames = $m->log_filenames;
154    push @filenames, 'all' if $m->log_to_all_file;
155    return map { sanitize_filename($m->log_base_path, $_) } @filenames;
156}
157
158=head2 should_log_message MESSAGE
159
160Determines whether or not the passed message should be logged.
161
162To customize the behavior of this method, override
163L<BarnOwl::Message::should_log>.
164
165=cut
166
167sub should_log_message {
168    my ($m) = @_;
169    # If there's a logfilter and this message matches it, log.
170    # pass quiet=1, because we don't care if the filter doesn't exist
171    return 1 if BarnOwl::message_matches_filter($m, BarnOwl::getvar('logfilter'), 1);
172    # otherwise we do things based on the logging variables
173    # skip login/logout messages if appropriate
174    return 0 if $m->is_loginout && BarnOwl::getvar('loglogins') eq 'off';
175    # check direction
176    return 0 if $m->is_outgoing && BarnOwl::getvar('loggingdirection') eq 'in';
177    return 0 if $m->is_incoming && BarnOwl::getvar('loggingdirection') eq 'out';
178    return $m->should_log;
179}
180
181=head2 log MESSAGE
182
183Call this method to (potentially) log a message.
184
185To customize the behavior of this method for your messages, override
186L<BarnOwl::Message::log>, L<BarnOwl::Message::should_log>,
187L<BarnOwl::Message::log_base_path>, and/or
188L<BarnOwl::Message::log_filenames>.
189
190=cut
191
192sub log {
193    my ($m) = @_;
194    return unless defined $m;
195    return unless BarnOwl::Logging::should_log_message($m);
196    my $log_text = $m->log;
197    foreach my $filename (BarnOwl::Logging::get_filenames($m)) {
198        BarnOwl::Logging::enqueue_text($log_text, $filename);
199    }
200}
201
202=head2 log_outgoing_error MESSAGE
203
204Call this method to (potentially) log an error in sending an
205outgoing message.  Errors get logged to the same file(s) as
206successful messages.
207
208To customize the behavior of this method for your messages, override
209L<BarnOwl::Message::log_outgoing_error>,
210L<BarnOwl::Message::should_log>,
211L<BarnOwl::Message::log_base_path>, and/or
212L<BarnOwl::Message::log_filenames>.
213
214=cut
215
216sub log_outgoing_error {
217    my ($m) = @_;
218    return unless BarnOwl::Logging::should_log_message($m);
219    my $log_text = $m->log_outgoing_error;
220    foreach my $filename (BarnOwl::Logging::get_filenames($m)) {
221        BarnOwl::Logging::enqueue_text($log_text, $filename);
222    }
223}
224
2251;
Note: See TracBrowser for help on using the repository browser.