source: logging.c @ 565a43c

Last change on this file since 565a43c was 565a43c, checked in by Jason Gross <jasongross9@gmail.com>, 7 years ago
Print a message about how many logs are flushed
  • Property mode set to 100644
File size: 19.3 KB
Line 
1#include "owl.h"
2#include <stdio.h>
3
4typedef struct _owl_log_entry { /* noproto */
5  char *filename;
6  char *message;
7} owl_log_entry;
8
9
10static GMainContext *log_context;
11static GMainLoop *log_loop;
12static GThread *logging_thread;
13static bool defer_logs; /* to be accessed only on the disk-writing thread */
14static GQueue *deferred_entry_queue;
15
16/* This is now the one function that should be called to log a
17 * message.  It will do all the work necessary by calling the other
18 * functions in this file as necessary.
19 */
20void owl_log_message(const owl_message *m) {
21  owl_function_debugmsg("owl_log_message: entering");
22
23  if (m == NULL) {
24    owl_function_debugmsg("owl_log_message: passed null message");
25    return;
26  }
27
28  /* should we be logging this message? */
29  if (!owl_log_shouldlog_message(m)) {
30    owl_function_debugmsg("owl_log_message: not logging message");
31    return;
32  }
33
34  /* handle incmoing messages */
35  if (owl_message_is_direction_in(m)) {
36    owl_log_incoming(m);
37    owl_function_debugmsg("owl_log_message: leaving");
38    return;
39  }
40
41  /* handle outgoing messages */
42  owl_log_outgoing(m);
43
44  owl_function_debugmsg("owl_log_message: leaving");
45}
46
47/* Return 1 if we should log the given message, otherwise return 0 */
48int owl_log_shouldlog_message(const owl_message *m) {
49  const owl_filter *f;
50
51  /* If there's a logfilter and this message matches it, log */
52  f=owl_global_get_filter(&g, owl_global_get_logfilter(&g));
53  if (f && owl_filter_message_match(f, m)) return(1);
54
55  /* otherwise we do things based on the logging variables */
56
57  /* skip login/logout messages if appropriate */
58  if (!owl_global_is_loglogins(&g) && owl_message_is_loginout(m)) return(0);
59     
60  /* check direction */
61  if ((owl_global_get_loggingdirection(&g)==OWL_LOGGING_DIRECTION_IN) && owl_message_is_direction_out(m)) {
62    return(0);
63  }
64  if ((owl_global_get_loggingdirection(&g)==OWL_LOGGING_DIRECTION_OUT) && owl_message_is_direction_in(m)) {
65    return(0);
66  }
67
68  if (owl_message_is_type_zephyr(m)) {
69    if (owl_message_is_personal(m) && !owl_global_is_logging(&g)) return(0);
70    if (!owl_message_is_personal(m) && !owl_global_is_classlogging(&g)) return(0);
71  } else {
72    if (owl_message_is_private(m) || owl_message_is_loginout(m)) {
73      if (!owl_global_is_logging(&g)) return(0);
74    } else {
75      if (!owl_global_is_classlogging(&g)) return(0);
76    }
77  }
78  return(1);
79}
80
81CALLER_OWN char *owl_log_zephyr(const owl_message *m)
82{
83    char *tmp = NULL;
84    GString *buffer = NULL;
85    buffer = g_string_new("");
86    tmp = short_zuser(owl_message_get_sender(m));
87    g_string_append_printf(buffer, "Class: %s Instance: %s", 
88                           owl_message_get_class(m), 
89                           owl_message_get_instance(m));
90    if (strcmp(owl_message_get_opcode(m), "")) {
91      g_string_append_printf(buffer, " Opcode: %s", 
92                             owl_message_get_opcode(m));
93    }
94    g_string_append_printf(buffer, "\n");
95    g_string_append_printf(buffer, "Time: %s Host: %s\n", 
96                           owl_message_get_timestr(m), 
97                           owl_message_get_hostname(m));
98    g_string_append_printf(buffer, "From: %s <%s>\n\n", 
99                           owl_message_get_zsig(m), tmp);
100    g_string_append_printf(buffer, "%s\n\n", owl_message_get_body(m));
101    g_free(tmp);
102    return g_string_free(buffer, FALSE);
103}
104
105CALLER_OWN char *owl_log_aim(const owl_message *m)
106{
107    GString *buffer = NULL;
108    buffer = g_string_new("");
109    g_string_append_printf(buffer, "From: <%s> To: <%s>\n", 
110                           owl_message_get_sender(m), owl_message_get_recipient(m));
111    g_string_append_printf(buffer, "Time: %s\n\n", 
112                           owl_message_get_timestr(m));
113    if (owl_message_is_login(m)) {
114        g_string_append_printf(buffer, "LOGIN\n\n");
115    } else if (owl_message_is_logout(m)) {
116        g_string_append_printf(buffer, "LOGOUT\n\n");
117    } else {
118        g_string_append_printf(buffer, "%s\n\n", owl_message_get_body(m));
119    }
120    return g_string_free(buffer, FALSE);
121}
122
123CALLER_OWN char *owl_log_jabber(const owl_message *m)
124{
125    GString *buffer = NULL;
126    buffer = g_string_new("");
127    g_string_append_printf(buffer, "From: <%s> To: <%s>\n",
128                           owl_message_get_sender(m), 
129                           owl_message_get_recipient(m));
130    g_string_append_printf(buffer, "Time: %s\n\n", 
131                           owl_message_get_timestr(m));
132    g_string_append_printf(buffer, "%s\n\n", owl_message_get_body(m));
133    return g_string_free(buffer, FALSE);
134}
135
136CALLER_OWN char *owl_log_generic(const owl_message *m)
137{
138    GString *buffer;
139    buffer = g_string_new("");
140    g_string_append_printf(buffer, "From: <%s> To: <%s>\n", 
141                           owl_message_get_sender(m), 
142                           owl_message_get_recipient(m));
143    g_string_append_printf(buffer, "Time: %s\n\n", 
144                           owl_message_get_timestr(m));
145    g_string_append_printf(buffer, "%s\n\n", 
146                           owl_message_get_body(m));
147    return g_string_free(buffer, FALSE);
148}
149
150static void owl_log_error_main_thread(gpointer data)
151{
152  owl_function_error("%s", (const char*)data);
153}
154
155static void owl_log_adminmsg_main_thread(gpointer data)
156{
157  owl_function_adminmsg("Logging", (const char*)data);
158}
159
160static void owl_log_makemsg_main_thread(gpointer data)
161{
162  owl_function_makemsg((const char*)data);
163}
164
165static void G_GNUC_PRINTF(1, 2) owl_log_error(const char *fmt, ...)
166{
167  va_list ap;
168  char *data;
169
170  va_start(ap, fmt);
171  data = g_strdup_vprintf(fmt, ap);
172  va_end(ap);
173
174  owl_select_post_task(owl_log_error_main_thread,
175                       data, g_free, g_main_context_default());
176}
177
178static void G_GNUC_PRINTF(1, 2) owl_log_adminmsg(const char *fmt, ...)
179{
180  va_list ap;
181  char *data;
182
183  va_start(ap, fmt);
184  data = g_strdup_vprintf(fmt, ap);
185  va_end(ap);
186
187  owl_select_post_task(owl_log_adminmsg_main_thread,
188                       data, g_free, g_main_context_default());
189}
190
191static void G_GNUC_PRINTF(1, 2) owl_log_makemsg(const char *fmt, ...)
192{
193  va_list ap;
194  char *data;
195
196  va_start(ap, fmt);
197  data = g_strdup_vprintf(fmt, ap);
198  va_end(ap);
199
200  owl_select_post_task(owl_log_makemsg_main_thread,
201                       data, g_free, g_main_context_default());
202}
203
204static CALLER_OWN owl_log_entry *owl_log_new_entry(const char *buffer, const char *filename)
205{
206  owl_log_entry *log_msg = g_slice_new(owl_log_entry);
207  log_msg->message = g_strdup(buffer);
208  log_msg->filename = g_strdup(filename);
209  return log_msg;
210}
211
212static void owl_log_deferred_enqueue_message(const char *buffer, const char *filename)
213{
214  g_queue_push_tail(deferred_entry_queue, owl_log_new_entry(buffer, filename));
215}
216
217static void owl_log_deferred_enqueue_first_message(const char *buffer, const char *filename)
218{
219  g_queue_push_head(deferred_entry_queue, owl_log_new_entry(buffer, filename));
220}
221
222/* write out the entry if possible
223 * return 0 on success, errno on failure to open
224 */
225static int owl_log_try_write_entry(owl_log_entry *msg)
226{
227  FILE *file = NULL;
228  file = fopen(msg->filename, "a");
229  if (!file) {
230    return errno;
231  }
232  fprintf(file, "%s", msg->message);
233  fclose(file);
234  return 0;
235}
236
237static void owl_log_entry_free(void *data)
238{
239  owl_log_entry *msg = (owl_log_entry*)data;
240  if (msg) {
241    g_free(msg->message);
242    g_free(msg->filename);
243    g_slice_free(owl_log_entry, msg);
244  }
245}
246
247#if GLIB_CHECK_VERSION(2, 32, 0)
248#else
249static void owl_log_entry_free_gfunc(gpointer data, gpointer user_data)
250{
251  owl_log_entry_free(data);
252}
253#endif
254
255static void owl_log_file_error(owl_log_entry *msg, int ret)
256{
257  owl_log_error("Unable to open file for logging: %s (file %s)",
258                g_strerror(ret),
259                msg->filename);
260}
261
262/* If we are deferring log messages, enqueue this entry for writing.
263 * Otherwise, try to write this log message, and, if it fails with
264 * EPERM, EACCES, or ETIMEDOUT, go into deferred logging mode and
265 * queue an admin message.  If it fails with anything else, display an
266 * error message, but do not go into deferred logging mode.
267 *
268 * N.B. This function is called only on the disk-writing thread. */
269static void owl_log_eventually_write_entry(gpointer data)
270{
271  int ret;
272  owl_log_entry *msg = (owl_log_entry*)data;
273  if (defer_logs) {
274    owl_log_deferred_enqueue_message(msg->message, msg->filename);
275  } else {
276    ret = owl_log_try_write_entry(msg);
277    if (ret == EPERM || ret == EACCES || ret == ETIMEDOUT) {
278      defer_logs = true;
279      owl_log_error("Unable to open file for logging (%s): \n"
280                    "%s.  \n"
281                    "Consider renewing your tickets.  Logging has been \n"
282                    "suspended, and your messages will be saved.  To \n"
283                    "resume logging, use the command :flush-logs.\n\n",
284                    msg->filename,
285                    g_strerror(ret));
286      /* If we were not in deferred logging mode, either the queue should be
287       * empty, or we are attempting to log a message that we just popped off
288       * the head of the queue.  Either way, we should enqueue this message as
289       * the first message in the queue, rather than the last, so that we
290       * preserve message ordering. */
291      owl_log_deferred_enqueue_first_message(msg->message, msg->filename);
292    } else if (ret != 0) {
293      owl_log_file_error(msg, ret);
294    }
295  }
296}
297
298/* tries to write the deferred log entries
299 *
300 * N.B. This function is called only on the disk-writing thread. */
301static void owl_log_write_deferred_entries(gpointer data)
302{
303  owl_log_entry *entry;
304  bool drop_failed_logs = *(bool *)data;
305  int ret;
306  int logged_message_count = 0;
307  bool all_succeeded = true;
308
309  if (g_queue_is_empty(deferred_entry_queue)) {
310    owl_log_makemsg("There are no logs to flush.");
311  } else {
312    owl_log_makemsg("Attempting to flush %u logs...",
313                     g_queue_get_length(deferred_entry_queue));
314  }
315
316  defer_logs = false;
317  while (!g_queue_is_empty(deferred_entry_queue) && !defer_logs) {
318    logged_message_count++;
319    entry = (owl_log_entry*)g_queue_pop_head(deferred_entry_queue);
320    if (!drop_failed_logs) {
321      /* Attempt to write the log entry.  If it fails, re-queue the entry at
322       * the head of the queue. */
323      owl_log_eventually_write_entry(entry);
324    } else {
325      /* Attempt to write the log entry. If it fails, print an error message,
326       * drop the log, and keep going through the queue. */
327      ret = owl_log_try_write_entry(entry);
328      if (ret != 0) {
329        all_succeeded = false;
330        owl_log_file_error(entry, ret);
331      }
332    }
333    owl_log_entry_free(entry);
334  }
335  if (logged_message_count > 0) {
336    /* first clear the "attempting to flush" message from the status bar */
337    owl_log_makemsg("");
338    if (!defer_logs) {
339      if (all_succeeded) {
340        owl_log_adminmsg("Flushed %d logs and resumed logging.",
341                         logged_message_count);
342      } else {
343        owl_log_adminmsg("Flushed or dropped %d logs and resumed logging.",
344                         logged_message_count);
345      }
346    } else {
347      owl_log_error("Attempted to flush %d logs; %u deferred logs remain.",
348                    logged_message_count, g_queue_get_length(deferred_entry_queue));
349    }
350  }
351}
352
353void owl_log_flush_logs(bool drop_failed_logs)
354{
355  bool *data = g_new(bool, 1);
356  *data = drop_failed_logs;
357
358  owl_select_post_task(owl_log_write_deferred_entries,
359                       data,
360                       g_free,
361                       log_context);
362}
363
364void owl_log_enqueue_message(const char *buffer, const char *filename)
365{
366  owl_log_entry *log_msg = owl_log_new_entry(buffer, filename);
367  owl_select_post_task(owl_log_eventually_write_entry, log_msg,
368                       owl_log_entry_free, log_context);
369}
370
371void owl_log_append(const owl_message *m, const char *filename) {
372  char *buffer = NULL;
373  if (owl_message_is_type_zephyr(m)) {
374    buffer = owl_log_zephyr(m);
375  } else if (owl_message_is_type_jabber(m)) {
376    buffer = owl_log_jabber(m);
377  } else if (owl_message_is_type_aim(m)) {
378    buffer = owl_log_aim(m);
379  } else {
380    buffer = owl_log_generic(m);
381  }
382  owl_log_enqueue_message(buffer, filename);
383  g_free(buffer);
384}
385
386void owl_log_outgoing(const owl_message *m)
387{
388  char *filename, *logpath;
389  char *to, *temp;
390  GList *cc;
391
392  /* expand ~ in path names */
393  logpath = owl_util_makepath(owl_global_get_logpath(&g));
394
395  /* Figure out what path to log to */
396  if (owl_message_is_type_zephyr(m)) {
397    /* If this has CC's, do all but the "recipient" which we'll do below */
398    cc = owl_message_get_cc_without_recipient(m);
399    while (cc != NULL) {
400      temp = short_zuser(cc->data);
401      filename = g_build_filename(logpath, temp, NULL);
402      owl_log_append(m, filename);
403
404      g_free(filename);
405      g_free(temp);
406      g_free(cc->data);
407      cc = g_list_delete_link(cc, cc);
408    }
409
410    to = short_zuser(owl_message_get_recipient(m));
411  } else if (owl_message_is_type_jabber(m)) {
412    to = g_strdup_printf("jabber:%s", owl_message_get_recipient(m));
413    g_strdelimit(to, "/", '_');
414  } else if (owl_message_is_type_aim(m)) {
415    char *temp2;
416    temp = owl_aim_normalize_screenname(owl_message_get_recipient(m));
417    temp2 = g_utf8_strdown(temp,-1);
418    to = g_strdup_printf("aim:%s", temp2);
419    g_free(temp2);
420    g_free(temp);
421  } else {
422    to = g_strdup("loopback");
423  }
424
425  filename = g_build_filename(logpath, to, NULL);
426  owl_log_append(m, filename);
427  g_free(to);
428  g_free(filename);
429
430  filename = g_build_filename(logpath, "all", NULL);
431  owl_log_append(m, filename);
432  g_free(logpath);
433  g_free(filename);
434}
435
436
437void owl_log_outgoing_zephyr_error(const owl_zwrite *zw, const char *text)
438{
439  char *filename, *logpath;
440  char *tobuff, *recip;
441  owl_message *m;
442  GString *msgbuf;
443  /* create a present message so we can pass it to
444   * owl_log_shouldlog_message(void)
445   */
446  m = g_slice_new(owl_message);
447  /* recip_index = 0 because there can only be one recipient anyway */
448  owl_message_create_from_zwrite(m, zw, text, 0);
449  if (!owl_log_shouldlog_message(m)) {
450    owl_message_delete(m);
451    return;
452  }
453  owl_message_delete(m);
454
455  /* chop off a local realm */
456  recip = owl_zwrite_get_recip_n_with_realm(zw, 0);
457  tobuff = short_zuser(recip);
458  g_free(recip);
459
460  /* expand ~ in path names */
461  logpath = owl_util_makepath(owl_global_get_logpath(&g));
462  filename = g_build_filename(logpath, tobuff, NULL);
463  msgbuf = g_string_new("");
464  g_string_printf(msgbuf, "ERROR (owl): %s\n%s\n", tobuff, text);
465  if (text[strlen(text)-1] != '\n') {
466    g_string_append_printf(msgbuf, "\n");
467  }
468  owl_log_enqueue_message(msgbuf->str, filename);
469  g_string_free(msgbuf, TRUE);
470
471  filename = g_build_filename(logpath, "all", NULL);
472  g_free(logpath);
473  msgbuf = g_string_new("");
474  g_string_printf(msgbuf, "ERROR (owl): %s\n%s\n", tobuff, text);
475  if (text[strlen(text)-1] != '\n') {
476    g_string_append_printf(msgbuf, "\n");
477  }
478  owl_log_enqueue_message(msgbuf->str, filename);
479  g_string_free(msgbuf, TRUE);
480
481  g_free(tobuff);
482}
483
484void owl_log_incoming(const owl_message *m)
485{
486  char *filename, *allfilename, *logpath;
487  const char *from=NULL;
488  char *frombuff=NULL;
489  int len, ch, i, personal;
490
491  /* figure out if it's a "personal" message or not */
492  if (owl_message_is_type_zephyr(m)) {
493    if (owl_message_is_personal(m)) {
494      personal = 1;
495    } else {
496      personal = 0;
497    }
498  } else if (owl_message_is_type_jabber(m)) {
499    /* This needs to be fixed to handle groupchat */
500    const char* msgtype = owl_message_get_attribute_value(m,"jtype");
501    if (msgtype && !strcmp(msgtype,"groupchat")) {
502      personal = 0;
503    } else {
504      personal = 1;
505    }
506  } else {
507    if (owl_message_is_private(m) || owl_message_is_loginout(m)) {
508      personal = 1;
509    } else {
510      personal = 0;
511    }
512  }
513
514
515  if (owl_message_is_type_zephyr(m)) {
516    if (personal) {
517      from=frombuff=short_zuser(owl_message_get_sender(m));
518    } else {
519      from=frombuff=g_strdup(owl_message_get_class(m));
520    }
521  } else if (owl_message_is_type_aim(m)) {
522    /* we do not yet handle chat rooms */
523    char *normalto, *temp;
524    temp = owl_aim_normalize_screenname(owl_message_get_sender(m));
525    normalto = g_utf8_strdown(temp, -1);
526    from=frombuff=g_strdup_printf("aim:%s", normalto);
527    g_free(normalto);
528    g_free(temp);
529  } else if (owl_message_is_type_loopback(m)) {
530    from=frombuff=g_strdup("loopback");
531  } else if (owl_message_is_type_jabber(m)) {
532    if (personal) {
533      from=frombuff=g_strdup_printf("jabber:%s", 
534                                    owl_message_get_sender(m));
535    } else {
536      from=frombuff=g_strdup_printf("jabber:%s", 
537                                    owl_message_get_recipient(m));
538    }
539  } else {
540    from=frombuff=g_strdup("unknown");
541  }
542 
543  /* check for malicious sender formats */
544  len=strlen(frombuff);
545  if (len<1 || len>35) from="weird";
546  if (strchr(frombuff, '/')) from="weird";
547
548  ch=frombuff[0];
549  if (!g_ascii_isalnum(ch)) from="weird";
550
551  for (i=0; i<len; i++) {
552    if (frombuff[i]<'!' || frombuff[i]>='~') from="weird";
553  }
554
555  if (!strcmp(frombuff, ".") || !strcasecmp(frombuff, "..")) from="weird";
556
557  if (!personal) {
558    if (strcmp(from, "weird")) {
559      char* temp = g_utf8_strdown(frombuff, -1);
560      if (temp) {
561        g_free(frombuff);
562        from = frombuff = temp;
563      }
564    }
565  }
566
567  /* create the filename (expanding ~ in path names) */
568  if (personal) {
569    logpath = owl_util_makepath(owl_global_get_logpath(&g));
570    filename = g_build_filename(logpath, from, NULL);
571    allfilename = g_build_filename(logpath, "all", NULL);
572    owl_log_append(m, allfilename);
573    g_free(allfilename);
574  } else {
575    logpath = owl_util_makepath(owl_global_get_classlogpath(&g));
576    filename = g_build_filename(logpath, from, NULL);
577  }
578
579  owl_log_append(m, filename);
580  g_free(filename);
581
582  if (personal && owl_message_is_type_zephyr(m)) {
583    /* We want to log to all of the CC'd people who were not us, or
584     * the sender, as well.
585     */
586    char *temp;
587    GList *cc;
588    cc = owl_message_get_cc_without_recipient(m);
589    while (cc != NULL) {
590      temp = short_zuser(cc->data);
591      if (strcasecmp(temp, frombuff) != 0) {
592        filename = g_build_filename(logpath, temp, NULL);
593        owl_log_append(m, filename);
594        g_free(filename);
595      }
596
597      g_free(temp);
598      g_free(cc->data);
599      cc = g_list_delete_link(cc, cc);
600    }
601  }
602
603  g_free(frombuff);
604  g_free(logpath);
605}
606
607static gpointer owl_log_thread_func(gpointer data)
608{
609  log_context = g_main_context_new();
610  log_loop = g_main_loop_new(log_context, FALSE);
611  g_main_loop_run(log_loop);
612  return NULL;
613}
614
615void owl_log_init(void)
616{
617  log_context = g_main_context_new();
618#if GLIB_CHECK_VERSION(2, 31, 0)
619  logging_thread = g_thread_new("logging",
620                                owl_log_thread_func,
621                                NULL);
622#else
623  GError *error = NULL;
624  logging_thread = g_thread_create(owl_log_thread_func,
625                                   NULL,
626                                   TRUE,
627                                   &error);
628  if (error) {
629    endwin();
630    fprintf(stderr, "Error spawning logging thread: %s\n", error->message);
631    fflush(stderr);
632    exit(1);
633  }
634#endif
635
636  deferred_entry_queue = g_queue_new();
637}
638
639static void owl_log_quit_func(gpointer data)
640{
641  /* flush the deferred logs queue, trying to write the
642   * entries to the disk one last time.  Drop any failed
643   * entries */
644  bool bool_true = true;
645  owl_log_write_deferred_entries(&bool_true);
646#if GLIB_CHECK_VERSION(2, 32, 0)
647  g_queue_free_full(deferred_entry_queue, owl_log_entry_free);
648#else
649  g_queue_foreach(deferred_entry_queue, owl_log_entry_free_gfunc, NULL);
650  g_queue_free(deferred_entry_queue);
651#endif
652
653  g_main_loop_quit(log_loop);
654}
655
656void owl_log_shutdown(void)
657{
658  owl_select_post_task(owl_log_quit_func, NULL,
659                       NULL, log_context);
660  g_thread_join(logging_thread);
661}
Note: See TracBrowser for help on using the repository browser.