Changeset 6e764aa


Ignore:
Timestamp:
Jan 7, 2014, 6:02:20 PM (8 years ago)
Author:
Jason Gross <jgross@mit.edu>
Children:
bf61c61
Parents:
f511c1e
git-author:
Jason Gross <jgross@mit.edu> (07/12/11 16:19:57)
git-committer:
Jason Gross <jgross@mit.edu> (01/07/14 18:02:20)
Message:
Moved the checking of whether or not to log messages to perl
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • logging.c

    rf511c1e r6e764aa  
    2424  }
    2525
    26   /* should we be logging this message? */
    27   if (!owl_log_shouldlog_message(m)) {
    28     owl_function_debugmsg("owl_log_message: not logging message");
    29     return;
    30   }
    31 
    32   owl_log_perl(m);
     26  g_free(owl_perlconfig_call_with_message("BarnOwl::Logging::log", m));
    3327
    3428  owl_function_debugmsg("owl_log_message: leaving");
    35 }
    36 
    37 /* Return 1 if we should log the given message, otherwise return 0 */
    38 int owl_log_shouldlog_message(const owl_message *m) {
    39   const owl_filter *f;
    40 
    41   /* If there's a logfilter and this message matches it, log */
    42   f=owl_global_get_filter(&g, owl_global_get_logfilter(&g));
    43   if (f && owl_filter_message_match(f, m)) return(1);
    44 
    45   /* otherwise we do things based on the logging variables */
    46 
    47   /* skip login/logout messages if appropriate */
    48   if (!owl_global_is_loglogins(&g) && owl_message_is_loginout(m)) return(0);
    49      
    50   /* check direction */
    51   if ((owl_global_get_loggingdirection(&g)==OWL_LOGGING_DIRECTION_IN) && owl_message_is_direction_out(m)) {
    52     return(0);
    53   }
    54   if ((owl_global_get_loggingdirection(&g)==OWL_LOGGING_DIRECTION_OUT) && owl_message_is_direction_in(m)) {
    55     return(0);
    56   }
    57 
    58   if (owl_message_is_type_zephyr(m)) {
    59     if (owl_message_is_personal(m) && !owl_global_is_logging(&g)) return(0);
    60     if (!owl_message_is_personal(m) && !owl_global_is_classlogging(&g)) return(0);
    61   } else {
    62     if (owl_message_is_private(m) || owl_message_is_loginout(m)) {
    63       if (!owl_global_is_logging(&g)) return(0);
    64     } else {
    65       if (!owl_global_is_classlogging(&g)) return(0);
    66     }
    67   }
    68   return(1);
    6929}
    7030
     
    12080}
    12181
    122 void owl_log_append(const owl_message *m, const char *filename) {
    123   char *buffer = owl_perlconfig_message_call_method(m, "log", 0, NULL);
    124   owl_log_enqueue_message(buffer, filename);
    125   g_free(buffer);
    126 }
    127 
    12882void owl_log_outgoing_zephyr_error(const owl_zwrite *zw, const char *text)
    12983{
    130   owl_message *m;
    131   /* create a present message so we can pass it to
    132    * owl_log_shouldlog_message(void)
    133    */
    134   m = g_slice_new(owl_message);
     84  owl_message *m = g_slice_new(owl_message);
    13585  /* recip_index = 0 because there can only be one recipient anyway */
    13686  owl_message_create_from_zwrite(m, zw, text, 0);
    137   if (!owl_log_shouldlog_message(m)) {
    138     owl_message_delete(m);
    139     return;
    140   }
    141   char *buffer = owl_perlconfig_message_call_method(m, "log_outgoing_error", 0, NULL);
    142   char *filenames_string = owl_perlconfig_call_with_message("BarnOwl::Logging::get_filenames_as_string", m);
    143   char **filenames = g_strsplit(filenames_string, " ", 0);
    144   char **filename_ptr;
    145 
    146   for (filename_ptr = filenames; *filename_ptr != NULL; filename_ptr++) {
    147     owl_log_enqueue_message(buffer, *filename_ptr);
    148   }
    149 
    150   g_free(filenames_string);
    151   g_strfreev(filenames);
     87  g_free(owl_perlconfig_call_with_message("BarnOwl::Logging::log_outgoing_error", m));
    15288  owl_message_delete(m);
    153 }
    154 
    155 void owl_log_perl(const owl_message *m)
    156 {
    157   char *filenames_string = owl_perlconfig_call_with_message("BarnOwl::Logging::get_filenames_as_string", m);
    158   char **filenames = g_strsplit(filenames_string, "\n", 0);
    159   char **filename_ptr;
    160   g_free(filenames_string);
    161 
    162   for (filename_ptr = filenames; *filename_ptr != NULL; filename_ptr++) {
    163     owl_log_append(m, *filename_ptr);
    164   }
    165 
    166   g_strfreev(filenames);
    16789}
    16890
  • perl/lib/BarnOwl/Logging.pm

    rdca6255 r6e764aa  
    1515Modules wishing to customize how messages are logged should override the
    1616relevant 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.
    1721
    1822=head2 EXPORTS
     
    7680}
    7781
    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;
     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);
    84122    }
    85     return join("\n", @rtn);
     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    }
    86146}
    87147
  • perl/lib/BarnOwl/Message.pm

    rf511c1e r6e764aa  
    225225    chomp $body;
    226226    return "ERROR (BarnOwl): $recipient\n$body\n\n";
     227}
     228
     229=head2 should_log MESSAGE
     230
     231Returns true if we should log C<MESSAGE>.  This does not override
     232user settings; if the BarnOwl variable C<loggingdirection> is in,
     233and C<MESSAGE> is outgoing and does not match the C<logfilter>, it
     234will not get logged regardless of what this method returns.
     235
     236Note that this method I<does> override the BarnOwl C<logging>
     237variable; if a derived class overrides this method and does not
     238provide an alternative BarnOwl variable (such as C<classlogging>),
     239the overriding method should check the BarnOwl C<logging> variable.
     240
     241Defaults to returning the value of the BarnOwl variable C<logging>.
     242
     243=cut
     244
     245sub should_log {
     246    return BarnOwl::getvar('logging') eq 'on';
    227247}
    228248
  • perl/lib/BarnOwl/Message/Zephyr.pm

    rdca6255 r6e764aa  
    281281}
    282282
     283sub should_log {
     284    my ($m) = @_;
     285    if ($m->log_to_class_file) {
     286        return BarnOwl::getvar('classlogging') eq 'on';
     287    } else {
     288        return BarnOwl::getvar('logging') eq 'on';
     289    }
     290}
     291
    2832921;
  • perlglue.xs

    re7ab1d7 r6e764aa  
    655655        OUTPUT:
    656656                RETVAL
     657
     658MODULE = BarnOwl                PACKAGE = BarnOwl::Logging
     659
     660void
     661enqueue_text(log_text, filename)
     662        const char * log_text
     663        const char * filename
     664        CODE:
     665                owl_log_enqueue_message(log_text, filename);
Note: See TracChangeset for help on using the changeset viewer.