| 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 | =head2 EXPORTS |
|---|
| 19 | |
|---|
| 20 | None by default. |
|---|
| 21 | |
|---|
| 22 | =cut |
|---|
| 23 | |
|---|
| 24 | use Exporter; |
|---|
| 25 | |
|---|
| 26 | our @EXPORT_OK = qw(); |
|---|
| 27 | |
|---|
| 28 | our %EXPORT_TAGS = (all => [@EXPORT_OK]); |
|---|
| 29 | |
|---|
| 30 | use File::Spec; |
|---|
| 31 | |
|---|
| 32 | =head2 sanitize_filename BASE_PATH FILENAME |
|---|
| 33 | |
|---|
| 34 | Sanitizes C<FILENAME> and concatenates it with C<BASE_PATH>. |
|---|
| 35 | |
|---|
| 36 | In any filename, C<"/"> and any control characters (characters which |
|---|
| 37 | match C<[:cntrl:]> get replaced by underscores. If the resulting |
|---|
| 38 | filename is empty or equal to C<"."> or C<"..">, it is replaced with |
|---|
| 39 | C<"weird">. |
|---|
| 40 | |
|---|
| 41 | =cut |
|---|
| 42 | |
|---|
| 43 | sub sanitize_filename { |
|---|
| 44 | my $base_path = BarnOwl::Internal::makepath(shift); |
|---|
| 45 | my $filename = shift; |
|---|
| 46 | $filename =~ s/[[:cntrl:]\/]/_/g; |
|---|
| 47 | if ($filename eq '' || $filename eq '.' || $filename eq '..') { |
|---|
| 48 | $filename = 'weird'; |
|---|
| 49 | } |
|---|
| 50 | # The original C code also removed characters less than '!' and |
|---|
| 51 | # greater than or equal to '~', marked file names beginning with a |
|---|
| 52 | # non-alphanumeric or non-ASCII character as 'weird', and rejected |
|---|
| 53 | # filenames longer than 35 characters. |
|---|
| 54 | return File::Spec->catfile($base_path, $filename); |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | =head2 get_filenames MESSAGE |
|---|
| 58 | |
|---|
| 59 | Returns a list of filenames in which to log the passed message. |
|---|
| 60 | |
|---|
| 61 | This method calls C<log_filenames> on C<MESSAGE> to determine the list |
|---|
| 62 | of filenames to which C<MESSAGE> gets logged. All filenames are |
|---|
| 63 | relative to C<MESSAGE->log_base_path>. If C<MESSAGE->log_to_all_file> |
|---|
| 64 | returns true, then the filename C<"all"> is appended to the list of |
|---|
| 65 | filenames. |
|---|
| 66 | |
|---|
| 67 | Filenames are sanitized by C<sanitize_filename>. |
|---|
| 68 | |
|---|
| 69 | =cut |
|---|
| 70 | |
|---|
| 71 | sub get_filenames { |
|---|
| 72 | my ($m) = @_; |
|---|
| 73 | my @filenames = $m->log_filenames; |
|---|
| 74 | push @filenames, 'all' if $m->log_to_all_file; |
|---|
| 75 | return map { sanitize_filename($m->log_base_path, $_) } @filenames; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | # For ease of use in C |
|---|
| 79 | sub get_filenames_as_string { |
|---|
| 80 | my @rtn; |
|---|
| 81 | foreach my $filename (BarnOwl::Logging::get_filenames(@_)) { |
|---|
| 82 | $filename =~ s/\n/_/g; |
|---|
| 83 | push @rtn, $filename; |
|---|
| 84 | } |
|---|
| 85 | return join("\n", @rtn); |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | 1; |
|---|