Changes in / [48848ae:048b1ff]


Ignore:
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • commands.c

    rf809cad r39a531d  
    490490              "\n"
    491491              "SEE ALSO: aaway, zaway"),
    492 
    493   OWLCMD_VOID("flush-logs", owl_command_flushlogs, OWL_CTX_ANY,
    494                "flush the queue of messages waiting to be logged",
    495                "",
    496                "If BarnOwl failed to log a file, this command tells\n"
    497                "BarnOwl to try logging the messages that have since\n"
    498                "come in, and to resume logging normally."),
    499492
    500493  OWLCMD_ARGS("load-subs", owl_command_loadsubs, OWL_CTX_ANY,
     
    14431436{
    14441437  owl_function_unsuball();
    1445 }
    1446 
    1447 void owl_command_flushlogs(void)
    1448 {
    1449   owl_log_flush_logs();
    14501438}
    14511439
  • logging.c

    rf809cad r0792d99  
    1111static GMainLoop *log_loop;
    1212static GThread *logging_thread;
    13 bool defer_logs;
    14 static GQueue *deferred_entry_queue;
    1513
    1614/* This is now the one function that should be called to log a
     
    5755  /* skip login/logout messages if appropriate */
    5856  if (!owl_global_is_loglogins(&g) && owl_message_is_loginout(m)) return(0);
    59 
     57     
    6058  /* check direction */
    6159  if ((owl_global_get_loggingdirection(&g)==OWL_LOGGING_DIRECTION_IN) && owl_message_is_direction_out(m)) {
     
    8583    buffer = g_string_new("");
    8684    tmp = short_zuser(owl_message_get_sender(m));
    87     g_string_append_printf(buffer, "Class: %s Instance: %s",
    88                            owl_message_get_class(m),
     85    g_string_append_printf(buffer, "Class: %s Instance: %s", 
     86                           owl_message_get_class(m), 
    8987                           owl_message_get_instance(m));
    9088    if (strcmp(owl_message_get_opcode(m), "")) {
    91       g_string_append_printf(buffer, " Opcode: %s",
     89      g_string_append_printf(buffer, " Opcode: %s", 
    9290                             owl_message_get_opcode(m));
    9391    }
    9492    g_string_append_printf(buffer, "\n");
    95     g_string_append_printf(buffer, "Time: %s Host: %s\n",
    96                            owl_message_get_timestr(m),
     93    g_string_append_printf(buffer, "Time: %s Host: %s\n", 
     94                           owl_message_get_timestr(m), 
    9795                           owl_message_get_hostname(m));
    98     g_string_append_printf(buffer, "From: %s <%s>\n\n",
     96    g_string_append_printf(buffer, "From: %s <%s>\n\n", 
    9997                           owl_message_get_zsig(m), tmp);
    10098    g_string_append_printf(buffer, "%s\n\n", owl_message_get_body(m));
     
    107105    GString *buffer = NULL;
    108106    buffer = g_string_new("");
    109     g_string_append_printf(buffer, "From: <%s> To: <%s>\n",
     107    g_string_append_printf(buffer, "From: <%s> To: <%s>\n", 
    110108                           owl_message_get_sender(m), owl_message_get_recipient(m));
    111     g_string_append_printf(buffer, "Time: %s\n\n",
     109    g_string_append_printf(buffer, "Time: %s\n\n", 
    112110                           owl_message_get_timestr(m));
    113111    if (owl_message_is_login(m)) {
     
    126124    buffer = g_string_new("");
    127125    g_string_append_printf(buffer, "From: <%s> To: <%s>\n",
    128                            owl_message_get_sender(m),
     126                           owl_message_get_sender(m), 
    129127                           owl_message_get_recipient(m));
    130     g_string_append_printf(buffer, "Time: %s\n\n",
     128    g_string_append_printf(buffer, "Time: %s\n\n", 
    131129                           owl_message_get_timestr(m));
    132130    g_string_append_printf(buffer, "%s\n\n", owl_message_get_body(m));
     
    138136    GString *buffer;
    139137    buffer = g_string_new("");
    140     g_string_append_printf(buffer, "From: <%s> To: <%s>\n",
    141                            owl_message_get_sender(m),
     138    g_string_append_printf(buffer, "From: <%s> To: <%s>\n", 
     139                           owl_message_get_sender(m), 
    142140                           owl_message_get_recipient(m));
    143     g_string_append_printf(buffer, "Time: %s\n\n",
     141    g_string_append_printf(buffer, "Time: %s\n\n", 
    144142                           owl_message_get_timestr(m));
    145     g_string_append_printf(buffer, "%s\n\n",
     143    g_string_append_printf(buffer, "%s\n\n", 
    146144                           owl_message_get_body(m));
    147145    return g_string_free(buffer, FALSE);
     
    160158}
    161159
    162 static CALLER_OWN owl_log_entry *owl_log_new_entry(const char *buffer, const char *filename)
    163 {
    164   owl_log_entry *log_msg = g_new(owl_log_entry, 1);
    165   log_msg->message = g_strdup(buffer);
    166   log_msg->filename = g_strdup(filename);
    167   return log_msg;
    168 }
    169 
    170 static void owl_log_deferred_enqueue_message(const char *buffer, const char *filename)
    171 {
    172   g_queue_push_tail(deferred_entry_queue, owl_log_new_entry(buffer, filename));
    173 }
    174 
    175 /* write out the entry if possible
    176  * return 0 on success, errno on failure to open
    177  */
    178 static int owl_log_try_write_entry(owl_log_entry *msg)
    179 {
     160static void owl_log_write_entry(gpointer data)
     161{
     162  owl_log_entry *msg = (owl_log_entry*)data;
    180163  FILE *file = NULL;
    181164  file = fopen(msg->filename, "a");
    182165  if (!file) {
    183     return errno;
     166    owl_log_error("Unable to open file for logging");
     167    return;
    184168  }
    185169  fprintf(file, "%s", msg->message);
    186170  fclose(file);
    187   return 0;
    188171}
    189172
     
    198181}
    199182
    200 static void owl_log_entry_free_gfunc(gpointer data, gpointer user_data)
    201 {
    202   owl_log_entry_free(data);
    203 }
    204 
    205 /* If we are deferring log messages, enqueue this entry for
    206  * writing.  Otherwise, try to write this log message, and,
    207  * if it fails with EPERM or EACCES, go into deferred logging
    208  * mode and queue an admin message.  If it fails with anything
    209  * else, display an error message, but do not go into deferred
    210  * logging mode. */
    211 static void owl_log_eventually_write_entry(gpointer data)
    212 {
    213   int ret;
    214   gchar *errmsg;
    215   owl_log_entry *msg = (owl_log_entry*)data;
    216   if (defer_logs) {
    217     owl_log_deferred_enqueue_message(msg->message, msg->filename);
    218   } else {
    219     ret = owl_log_try_write_entry(msg);
    220     if (ret == EPERM || ret == EACCES) {
    221       defer_logs = true;
    222       owl_log_error("Unable to open file for logging; you do not have \n"
    223                     "permission.  Consider renewing your tickets.  \n"
    224                     "Logging has been suspended, and your messages \n"
    225                     "will be saved.  To resume logging, use the \n"
    226                     "command :flush-logs.\n\n");
    227       owl_log_deferred_enqueue_message(msg->message, msg->filename);
    228     } else if (ret != 0) {
    229       errmsg = g_strdup_printf("Unable to open file for logging: %s", g_strerror(ret));
    230       owl_log_error(errmsg);
    231       g_free(errmsg);
    232     }
    233   }
    234 }
    235 
    236 /* tries to the deferred log entries */
    237 static void owl_log_write_deferred_entries(gpointer data)
    238 {
    239   owl_log_entry *entry;
    240 
    241   defer_logs = false;
    242   while (!g_queue_is_empty(deferred_entry_queue) && !defer_logs) {
    243     entry = (owl_log_entry*)g_queue_pop_head(deferred_entry_queue);
    244     owl_log_eventually_write_entry(entry);
    245     owl_log_entry_free(entry);
    246   }
    247 }
    248 
    249 void owl_log_flush_logs(void)
    250 {
    251   owl_select_post_task(owl_log_write_deferred_entries, NULL, NULL, log_context);
    252 }
    253 
    254183void owl_log_enqueue_message(const char *buffer, const char *filename)
    255184{
    256   owl_log_entry *log_msg = owl_log_new_entry(buffer, filename);
    257   owl_select_post_task(owl_log_eventually_write_entry, log_msg,
     185  owl_log_entry *log_msg = NULL;
     186  log_msg = g_new(owl_log_entry,1);
     187  log_msg->message = g_strdup(buffer);
     188  log_msg->filename = g_strdup(filename);
     189  owl_select_post_task(owl_log_write_entry, log_msg,
    258190                       owl_log_entry_free, log_context);
    259191}
     
    421353  } else if (owl_message_is_type_jabber(m)) {
    422354    if (personal) {
    423       from=frombuff=g_strdup_printf("jabber:%s",
     355      from=frombuff=g_strdup_printf("jabber:%s", 
    424356                                    owl_message_get_sender(m));
    425357    } else {
    426       from=frombuff=g_strdup_printf("jabber:%s",
     358      from=frombuff=g_strdup_printf("jabber:%s", 
    427359                                    owl_message_get_recipient(m));
    428360    }
     
    430362    from=frombuff=g_strdup("unknown");
    431363  }
    432 
     364 
    433365  /* check for malicious sender formats */
    434366  len=strlen(frombuff);
     
    497429static gpointer owl_log_thread_func(gpointer data)
    498430{
    499   log_context = g_main_context_new();
    500431  log_loop = g_main_loop_new(log_context, FALSE);
    501432  g_main_loop_run(log_loop);
     
    503434}
    504435
    505 void owl_log_init(void)
     436void owl_log_init(void) 
    506437{
    507438  log_context = g_main_context_new();
     
    523454  }
    524455#endif
    525 
    526   deferred_entry_queue = g_queue_new();
     456 
    527457}
    528458
    529459static void owl_log_quit_func(gpointer data)
    530460{
    531   /* flush the deferred logs queue, trying to write the
    532    * entries to the disk one last time */
    533   owl_log_write_deferred_entries(NULL);
    534 #if GLIB_CHECK_VERSION(2, 32, 0)
    535   g_queue_free_full(deferred_entry_queue, owl_log_entry_free_gfunc);
    536 #else
    537   g_queue_foreach(deferred_entry_queue, owl_log_entry_free_gfunc, NULL);
    538   g_queue_free(deferred_entry_queue);
    539 #endif
    540 
    541461  g_main_loop_quit(log_loop);
    542462}
Note: See TracChangeset for help on using the changeset viewer.