1 | use strict; |
---|
2 | use warnings; |
---|
3 | |
---|
4 | package BarnOwl::Logging; |
---|
5 | |
---|
6 | =head1 BarnOwl::Logging |
---|
7 | |
---|
8 | =head1 DESCRIPTION |
---|
9 | |
---|
10 | C<BarnOwl::Logging> implements the internals of logging. All customizations |
---|
11 | to logging should be done in the appropriate subclass of L<BarnOwl::Message>. |
---|
12 | |
---|
13 | =head2 USAGE |
---|
14 | |
---|
15 | Modules wishing to customize how messages are logged should override the |
---|
16 | relevant subroutines in the appropriate subclass of L<BarnOwl::Message>. |
---|
17 | |
---|
18 | Modules wishing to log errors sending outgoing messages should call |
---|
19 | L<BarnOwl::Logging::log_outgoing_error> with the message that failed |
---|
20 | to be sent. |
---|
21 | |
---|
22 | =head2 EXPORTS |
---|
23 | |
---|
24 | None by default. |
---|
25 | |
---|
26 | =cut |
---|
27 | |
---|
28 | use Exporter; |
---|
29 | |
---|
30 | our @EXPORT_OK = qw(); |
---|
31 | |
---|
32 | our %EXPORT_TAGS = (all => [@EXPORT_OK]); |
---|
33 | |
---|
34 | use File::Spec; |
---|
35 | |
---|
36 | $BarnOwl::Hooks::newMessage->add("BarnOwl::Logging::log"); |
---|
37 | $BarnOwl::Hooks::startup->add("BarnOwl::Logging::_register_variables"); |
---|
38 | |
---|
39 | sub _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 | |
---|
82 | =head2 sanitize_filename BASE_PATH FILENAME |
---|
83 | |
---|
84 | Sanitizes C<FILENAME> and concatenates it with C<BASE_PATH>. |
---|
85 | |
---|
86 | In any filename, C<"/"> and any control characters (characters which |
---|
87 | match C<[:cntrl:]> get replaced by underscores. If the resulting |
---|
88 | filename is empty or equal to C<"."> or C<"..">, it is replaced with |
---|
89 | C<"weird">. |
---|
90 | |
---|
91 | =cut |
---|
92 | |
---|
93 | sub sanitize_filename { |
---|
94 | my $base_path = BarnOwl::Internal::makepath(shift); |
---|
95 | my $filename = shift; |
---|
96 | $filename =~ s/[[:cntrl:]\/]/_/g; |
---|
97 | if ($filename eq '' || $filename eq '.' || $filename eq '..') { |
---|
98 | $filename = 'weird'; |
---|
99 | } |
---|
100 | # The original C code also removed characters less than '!' and |
---|
101 | # greater than or equal to '~', marked file names beginning with a |
---|
102 | # non-alphanumeric or non-ASCII character as 'weird', and rejected |
---|
103 | # filenames longer than 35 characters. |
---|
104 | return File::Spec->catfile($base_path, $filename); |
---|
105 | } |
---|
106 | |
---|
107 | =head2 get_filenames MESSAGE |
---|
108 | |
---|
109 | Returns a list of filenames in which to log the passed message. |
---|
110 | |
---|
111 | This method calls C<log_filenames> on C<MESSAGE> to determine the list |
---|
112 | of filenames to which C<MESSAGE> gets logged. All filenames are |
---|
113 | relative to C<MESSAGE->log_base_path>. If C<MESSAGE->log_to_all_file> |
---|
114 | returns true, then the filename C<"all"> is appended to the list of |
---|
115 | filenames. |
---|
116 | |
---|
117 | Filenames are sanitized by C<sanitize_filename>. |
---|
118 | |
---|
119 | =cut |
---|
120 | |
---|
121 | sub get_filenames { |
---|
122 | my ($m) = @_; |
---|
123 | my @filenames = $m->log_filenames; |
---|
124 | push @filenames, 'all' if $m->log_to_all_file; |
---|
125 | return map { sanitize_filename($m->log_base_path, $_) } @filenames; |
---|
126 | } |
---|
127 | |
---|
128 | =head2 should_log_message MESSAGE |
---|
129 | |
---|
130 | Determines whether or not the passed message should be logged. |
---|
131 | |
---|
132 | To customize the behavior of this method, override |
---|
133 | L<BarnOwl::Message::should_log>. |
---|
134 | |
---|
135 | =cut |
---|
136 | |
---|
137 | sub should_log_message { |
---|
138 | my ($m) = @_; |
---|
139 | # If there's a logfilter and this message matches it, log. |
---|
140 | # pass quiet=1, because we don't care if the filter doesn't exist |
---|
141 | return 1 if BarnOwl::message_matches_filter($m, BarnOwl::getvar('logfilter'), 1); |
---|
142 | # otherwise we do things based on the logging variables |
---|
143 | # skip login/logout messages if appropriate |
---|
144 | return 0 if $m->is_loginout && BarnOwl::getvar('loglogins') eq 'off'; |
---|
145 | # check direction |
---|
146 | return 0 if $m->is_outgoing && BarnOwl::getvar('loggingdirection') eq 'in'; |
---|
147 | return 0 if $m->is_incoming && BarnOwl::getvar('loggingdirection') eq 'out'; |
---|
148 | return $m->should_log; |
---|
149 | } |
---|
150 | |
---|
151 | =head2 log MESSAGE |
---|
152 | |
---|
153 | Call this method to (potentially) log a message. |
---|
154 | |
---|
155 | To customize the behavior of this method for your messages, override |
---|
156 | L<BarnOwl::Message::log>, L<BarnOwl::Message::should_log>, |
---|
157 | L<BarnOwl::Message::log_base_path>, and/or |
---|
158 | L<BarnOwl::Message::log_filenames>. |
---|
159 | |
---|
160 | =cut |
---|
161 | |
---|
162 | sub log { |
---|
163 | my ($m) = @_; |
---|
164 | return unless defined $m; |
---|
165 | return unless BarnOwl::Logging::should_log_message($m); |
---|
166 | my $log_text = $m->log; |
---|
167 | foreach my $filename (BarnOwl::Logging::get_filenames($m)) { |
---|
168 | BarnOwl::Logging::enqueue_text($log_text, $filename); |
---|
169 | } |
---|
170 | } |
---|
171 | |
---|
172 | =head2 log_outgoing_error MESSAGE |
---|
173 | |
---|
174 | Call this method to (potentially) log an error in sending an |
---|
175 | outgoing message. Errors get logged to the same file(s) as |
---|
176 | successful messages. |
---|
177 | |
---|
178 | To customize the behavior of this method for your messages, override |
---|
179 | L<BarnOwl::Message::log_outgoing_error>, |
---|
180 | L<BarnOwl::Message::should_log>, |
---|
181 | L<BarnOwl::Message::log_base_path>, and/or |
---|
182 | L<BarnOwl::Message::log_filenames>. |
---|
183 | |
---|
184 | =cut |
---|
185 | |
---|
186 | sub log_outgoing_error { |
---|
187 | my ($m) = @_; |
---|
188 | return unless BarnOwl::Logging::should_log_message($m); |
---|
189 | my $log_text = $m->log_outgoing_error; |
---|
190 | foreach my $filename (BarnOwl::Logging::get_filenames($m)) { |
---|
191 | BarnOwl::Logging::enqueue_text($log_text, $filename); |
---|
192 | } |
---|
193 | } |
---|
194 | |
---|
195 | 1; |
---|