source: logging.c @ bcde7926

release-1.10release-1.8release-1.9
Last change on this file since bcde7926 was cc305b5, checked in by Adam Glasgall <glasgall@mit.edu>, 13 years ago
Move log-writing onto a background thread. Currently, Barnowl writes message logs on the same thread as the UI and other I/O. This means that if writing log files is very slow (e.g. if logpath or classlogpath point to somewhere in AFS and the fileserver that volume relies on is very slow to respond), barnowl freezes up until the write completes. This patch creates a background logging thread as part of initialization and runs a main loop on that thread. Logging is then accomplished by posting tasks that write to log files to that thread, a la how other threads can post tasks to the UI thread's main loop. Post a quit task to the logging thread's mainloop instead of quitting directly Calling g_main_quit() on the main thread to quit the logging thread could resultin log tasks still in its queue getting lost. Instead, after we've shut down everything that could log messages, post a quit task to the log thread so that it'll only get run after everything outstanding has been processed.
  • Property mode set to 100644
File size: 13.2 KB
RevLine 
[7d4fbcd]1#include "owl.h"
2#include <stdlib.h>
3#include <string.h>
4#include <ctype.h>
5#include <sys/param.h>
6
[cc305b5]7typedef struct _owl_log_entry { /* noproto */
8  char *filename;
9  char *message;
10} owl_log_entry;
11
12
13static GMainContext *log_context;
14static GMainLoop *log_loop;
15static GThread *logging_thread;
16
[15b34fd]17/* This is now the one function that should be called to log a
18 * message.  It will do all the work necessary by calling the other
19 * functions in this file as necessary.
20 */
[c08c70a]21void owl_log_message(const owl_message *m) {
[15b34fd]22  owl_function_debugmsg("owl_log_message: entering");
23
[3c7d086a]24  if (m == NULL) {
25    owl_function_debugmsg("owl_log_message: passed null message");
26    return;
27  }
28
[15b34fd]29  /* should we be logging this message? */
30  if (!owl_log_shouldlog_message(m)) {
31    owl_function_debugmsg("owl_log_message: not logging message");
32    return;
33  }
34
35  /* handle incmoing messages */
36  if (owl_message_is_direction_in(m)) {
37    owl_log_incoming(m);
38    owl_function_debugmsg("owl_log_message: leaving");
39    return;
40  }
41
42  /* handle outgoing messages */
[42947f1]43  owl_log_outgoing(m);
44
[15b34fd]45  owl_function_debugmsg("owl_log_message: leaving");
46}
[7d4fbcd]47
[15b34fd]48/* Return 1 if we should log the given message, otherwise return 0 */
[c08c70a]49int owl_log_shouldlog_message(const owl_message *m) {
[4542047]50  const owl_filter *f;
[12c35df]51
[15b34fd]52  /* If there's a logfilter and this message matches it, log */
53  f=owl_global_get_filter(&g, owl_global_get_logfilter(&g));
54  if (f && owl_filter_message_match(f, m)) return(1);
[7d4fbcd]55
[15b34fd]56  /* otherwise we do things based on the logging variables */
57
58  /* skip login/logout messages if appropriate */
59  if (!owl_global_is_loglogins(&g) && owl_message_is_loginout(m)) return(0);
60     
61  /* check direction */
62  if ((owl_global_get_loggingdirection(&g)==OWL_LOGGING_DIRECTION_IN) && owl_message_is_direction_out(m)) {
63    return(0);
64  }
65  if ((owl_global_get_loggingdirection(&g)==OWL_LOGGING_DIRECTION_OUT) && owl_message_is_direction_in(m)) {
66    return(0);
67  }
68
69  if (owl_message_is_type_zephyr(m)) {
70    if (owl_message_is_personal(m) && !owl_global_is_logging(&g)) return(0);
71    if (!owl_message_is_personal(m) && !owl_global_is_classlogging(&g)) return(0);
72  } else {
73    if (owl_message_is_private(m) || owl_message_is_loginout(m)) {
74      if (!owl_global_is_logging(&g)) return(0);
75    } else {
76      if (!owl_global_is_classlogging(&g)) return(0);
77    }
[7d4fbcd]78  }
[15b34fd]79  return(1);
80}
81
[cc305b5]82char *owl_log_zephyr(const owl_message *m) {
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));
[ddbbcffa]101    g_free(tmp);
[cc305b5]102    return g_string_free(buffer, FALSE);
103}
104
105char *owl_log_aim(const owl_message *m) {
106    GString *buffer = NULL;
107    buffer = g_string_new("");
108    g_string_append_printf(buffer, "From: <%s> To: <%s>\n", 
109                           owl_message_get_sender(m), owl_message_get_recipient(m));
110    g_string_append_printf(buffer, "Time: %s\n\n", 
111                           owl_message_get_timestr(m));
112    if (owl_message_is_login(m)) {
113        g_string_append_printf(buffer, "LOGIN\n\n");
114    } else if (owl_message_is_logout(m)) {
115        g_string_append_printf(buffer, "LOGOUT\n\n");
116    } else {
117        g_string_append_printf(buffer, "%s\n\n", owl_message_get_body(m));
118    }
119    return g_string_free(buffer, FALSE);
120}
121
122char *owl_log_jabber(const owl_message *m) {
123    GString *buffer = NULL;
124    buffer = g_string_new("");
125    g_string_append_printf(buffer, "From: <%s> To: <%s>\n",
126                           owl_message_get_sender(m), 
127                           owl_message_get_recipient(m));
128    g_string_append_printf(buffer, "Time: %s\n\n", 
129                           owl_message_get_timestr(m));
130    g_string_append_printf(buffer, "%s\n\n", owl_message_get_body(m));
131    return g_string_free(buffer, FALSE);
132}
133
134char *owl_log_generic(const owl_message *m) {
135    GString *buffer;
136    buffer = g_string_new("");
137    g_string_append_printf(buffer, "From: <%s> To: <%s>\n", 
138                           owl_message_get_sender(m), 
139                           owl_message_get_recipient(m));
140    g_string_append_printf(buffer, "Time: %s\n\n", 
141                           owl_message_get_timestr(m));
142    g_string_append_printf(buffer, "%s\n\n", 
143                           owl_message_get_body(m));
144    return g_string_free(buffer, FALSE);
145}
146
147static void owl_log_error_main_thread(gpointer data)
148{
149  owl_function_error("%s", (const char*)data);
[42947f1]150}
151
[cc305b5]152static void owl_log_error(const char *message)
153{
154  char *data = g_strdup(message);
155  owl_select_post_task(owl_log_error_main_thread,
156                       data, g_free, g_main_context_default());
[42947f1]157}
158
[cc305b5]159static void owl_log_write_entry(gpointer data)
160{
161  owl_log_entry *msg = (owl_log_entry*)data;
162  FILE *file = NULL;
163  file = fopen(msg->filename, "a");
164  if (!file) {
165    owl_log_error("Unable to open file for logging");
166    return;
167  }
168  fprintf(file, "%s", msg->message);
169  fclose(file);
[42947f1]170}
171
[cc305b5]172static void owl_log_entry_free(void *data)
173{
174  owl_log_entry *msg = (owl_log_entry*)data;
175  if (msg) {
176    g_free(msg->message);
177    g_free(msg->filename);
178    g_free(msg);
179  }
180}
181
182void owl_log_enqueue_message(const char *buffer, const char *filename)
183{
184  owl_log_entry *log_msg = NULL; 
185  log_msg = g_new(owl_log_entry,1);
186  log_msg->message = g_strdup(buffer);
187  log_msg->filename = g_strdup(filename);
188  owl_select_post_task(owl_log_write_entry, log_msg, 
189                       owl_log_entry_free, log_context);
[42947f1]190}
191
[c08c70a]192void owl_log_append(const owl_message *m, const char *filename) {
[cc305b5]193  char *buffer = NULL;
194  if (owl_message_is_type_zephyr(m)) {
195    buffer = owl_log_zephyr(m);
196  } else if (owl_message_is_type_jabber(m)) {
197    buffer = owl_log_jabber(m);
198  } else if (owl_message_is_type_aim(m)) {
199    buffer = owl_log_aim(m);
200  } else {
201    buffer = owl_log_generic(m);
202  }
203  owl_log_enqueue_message(buffer, filename);
204  g_free(buffer);
[42947f1]205}
206
[c08c70a]207void owl_log_outgoing(const owl_message *m)
[15b34fd]208{
[e3a75ed]209  char *filename, *logpath;
[9c590d4]210  char *to, *temp;
[839697d]211  GList *cc;
[9c590d4]212
213  /* expand ~ in path names */
[60d7935]214  logpath = owl_util_makepath(owl_global_get_logpath(&g));
[15b34fd]215
[42947f1]216  /* Figure out what path to log to */
217  if (owl_message_is_type_zephyr(m)) {
[af1920fd]218    /* If this has CC's, do all but the "recipient" which we'll do below */
[839697d]219    cc = owl_message_get_cc_without_recipient(m);
220    while (cc != NULL) {
221      temp = short_zuser(cc->data);
[e3a75ed]222      filename = g_strdup_printf("%s/%s", logpath, temp);
[839697d]223      owl_log_append(m, filename);
224
[e3a75ed]225      g_free(filename);
[ddbbcffa]226      g_free(temp);
227      g_free(cc->data);
[839697d]228      cc = g_list_delete_link(cc, cc);
[9c590d4]229    }
[839697d]230
[9c590d4]231    to = short_zuser(owl_message_get_recipient(m));
[42947f1]232  } else if (owl_message_is_type_jabber(m)) {
[3472845]233    to = g_strdup_printf("jabber:%s", owl_message_get_recipient(m));
[7865479]234    g_strdelimit(to, "/", '_');
[42947f1]235  } else if (owl_message_is_type_aim(m)) {
[28ee32b]236    char *temp2;
[9c590d4]237    temp = owl_aim_normalize_screenname(owl_message_get_recipient(m));
[28ee32b]238    temp2 = g_utf8_strdown(temp,-1);
[3472845]239    to = g_strdup_printf("aim:%s", temp2);
[ddbbcffa]240    g_free(temp2);
241    g_free(temp);
[42947f1]242  } else {
[d4927a7]243    to = g_strdup("loopback");
[42947f1]244  }
[7d4fbcd]245
[e3a75ed]246  filename = g_strdup_printf("%s/%s", logpath, to);
[42947f1]247  owl_log_append(m, filename);
[ddbbcffa]248  g_free(to);
[e3a75ed]249  g_free(filename);
[7d4fbcd]250
[e3a75ed]251  filename = g_strdup_printf("%s/all", logpath);
[42947f1]252  owl_log_append(m, filename);
[ddbbcffa]253  g_free(logpath);
[e3a75ed]254  g_free(filename);
[7d4fbcd]255}
256
[42947f1]257
[24ccc01]258void owl_log_outgoing_zephyr_error(const owl_zwrite *zw, const char *text)
[2b86d14]259{
[e3a75ed]260  char *filename, *logpath;
[fe3b017]261  char *tobuff, *recip;
[180cd15]262  owl_message *m;
[cc305b5]263  GString *msgbuf;
[180cd15]264  /* create a present message so we can pass it to
[c79a047]265   * owl_log_shouldlog_message(void)
[180cd15]266   */
[96828e4]267  m = g_new(owl_message, 1);
[e5da3fe]268  /* recip_index = 0 because there can only be one recipient anyway */
269  owl_message_create_from_zwrite(m, zw, text, 0);
[180cd15]270  if (!owl_log_shouldlog_message(m)) {
[91634ec]271    owl_message_delete(m);
[180cd15]272    return;
273  }
[91634ec]274  owl_message_delete(m);
[2b86d14]275
[180cd15]276  /* chop off a local realm */
[fe3b017]277  recip = owl_zwrite_get_recip_n_with_realm(zw, 0);
278  tobuff = short_zuser(recip);
279  g_free(recip);
[2b86d14]280
281  /* expand ~ in path names */
[60d7935]282  logpath = owl_util_makepath(owl_global_get_logpath(&g));
[e3a75ed]283  filename = g_strdup_printf("%s/%s", logpath, tobuff);
[cc305b5]284  msgbuf = g_string_new("");
285  g_string_printf(msgbuf, "ERROR (owl): %s\n%s\n", tobuff, text);
286  if (text[strlen(text)-1] != '\n') {
287    g_string_append_printf(msgbuf, "\n");
[2b86d14]288  }
[cc305b5]289  owl_log_enqueue_message(msgbuf->str, filename);
290  g_string_free(msgbuf, TRUE);
[2b86d14]291
[e3a75ed]292  filename = g_strdup_printf("%s/all", logpath);
[ddbbcffa]293  g_free(logpath);
[cc305b5]294  msgbuf = g_string_new("");
295  g_string_printf(msgbuf, "ERROR (owl): %s\n%s\n", tobuff, text);
296  if (text[strlen(text)-1] != '\n') {
297    g_string_append_printf(msgbuf, "\n");
[2b86d14]298  }
[cc305b5]299  owl_log_enqueue_message(msgbuf->str, filename);
300  g_string_free(msgbuf, TRUE);
[2b86d14]301
[ddbbcffa]302  g_free(tobuff);
[2b86d14]303}
304
[c08c70a]305void owl_log_incoming(const owl_message *m)
[15283bb]306{
[e3a75ed]307  char *filename, *allfilename, *logpath;
[e19eb97]308  const char *from=NULL;
[65b2173]309  char *frombuff=NULL;
[7d4fbcd]310  int len, ch, i, personal;
[12c35df]311
[15b34fd]312  /* figure out if it's a "personal" message or not */
[aac889a]313  if (owl_message_is_type_zephyr(m)) {
314    if (owl_message_is_personal(m)) {
[42947f1]315      personal = 1;
[aac889a]316    } else {
[42947f1]317      personal = 0;
[aac889a]318    }
[2182be3]319  } else if (owl_message_is_type_jabber(m)) {
[42947f1]320    /* This needs to be fixed to handle groupchat */
[e19eb97]321    const char* msgtype = owl_message_get_attribute_value(m,"jtype");
[42947f1]322    if (msgtype && !strcmp(msgtype,"groupchat")) {
323      personal = 0;
324    } else {
325      personal = 1;
326    }
[7d4fbcd]327  } else {
[79a0e82]328    if (owl_message_is_private(m) || owl_message_is_loginout(m)) {
[42947f1]329      personal = 1;
[aac889a]330    } else {
[42947f1]331      personal = 0;
[aac889a]332    }
[7d4fbcd]333  }
334
[2182be3]335
[aac889a]336  if (owl_message_is_type_zephyr(m)) {
337    if (personal) {
[3066d23]338      from=frombuff=short_zuser(owl_message_get_sender(m));
[aac889a]339    } else {
[d4927a7]340      from=frombuff=g_strdup(owl_message_get_class(m));
[7d4fbcd]341    }
[aac889a]342  } else if (owl_message_is_type_aim(m)) {
343    /* we do not yet handle chat rooms */
[28ee32b]344    char *normalto, *temp;
345    temp = owl_aim_normalize_screenname(owl_message_get_sender(m));
346    normalto = g_utf8_strdown(temp, -1);
[3472845]347    from=frombuff=g_strdup_printf("aim:%s", normalto);
[ddbbcffa]348    g_free(normalto);
349    g_free(temp);
[37eab7f]350  } else if (owl_message_is_type_loopback(m)) {
[d4927a7]351    from=frombuff=g_strdup("loopback");
[2182be3]352  } else if (owl_message_is_type_jabber(m)) {
[5c30091]353    if (personal) {
[cc305b5]354      from=frombuff=g_strdup_printf("jabber:%s", 
355                                    owl_message_get_sender(m));
[5c30091]356    } else {
[cc305b5]357      from=frombuff=g_strdup_printf("jabber:%s", 
358                                    owl_message_get_recipient(m));
[5c30091]359    }
[e6449bc]360  } else {
[d4927a7]361    from=frombuff=g_strdup("unknown");
[7d4fbcd]362  }
363 
364  /* check for malicious sender formats */
365  len=strlen(frombuff);
366  if (len<1 || len>35) from="weird";
367  if (strchr(frombuff, '/')) from="weird";
368
369  ch=frombuff[0];
[28ee32b]370  if (!g_ascii_isalnum(ch)) from="weird";
[7d4fbcd]371
372  for (i=0; i<len; i++) {
[e1c4636]373    if (frombuff[i]<'!' || frombuff[i]>='~') from="weird";
[7d4fbcd]374  }
375
376  if (!strcmp(frombuff, ".") || !strcasecmp(frombuff, "..")) from="weird";
377
378  if (!personal) {
[28ee32b]379    if (strcmp(from, "weird")) {
380      char* temp = g_utf8_strdown(frombuff, -1);
381      if (temp) {
[ddbbcffa]382        g_free(frombuff);
[28ee32b]383        from = frombuff = temp;
384      }
385    }
[7d4fbcd]386  }
387
[e1c4636]388  /* create the filename (expanding ~ in path names) */
[7d4fbcd]389  if (personal) {
[60d7935]390    logpath = owl_util_makepath(owl_global_get_logpath(&g));
[e3a75ed]391    filename = g_strdup_printf("%s/%s", logpath, from);
392    allfilename = g_strdup_printf("%s/all", logpath);
[d0961fe]393    owl_log_append(m, allfilename);
[e3a75ed]394    g_free(allfilename);
[7d4fbcd]395  } else {
[60d7935]396    logpath = owl_util_makepath(owl_global_get_classlogpath(&g));
[e3a75ed]397    filename = g_strdup_printf("%s/%s", logpath, from);
[7d4fbcd]398  }
[37eab7f]399
[42947f1]400  owl_log_append(m, filename);
[e3a75ed]401  g_free(filename);
[7d4fbcd]402
[d0961fe]403  if (personal && owl_message_is_type_zephyr(m)) {
[af1920fd]404    /* We want to log to all of the CC'd people who were not us, or
405     * the sender, as well.
406     */
[839697d]407    char *temp;
408    GList *cc;
[d0961fe]409    cc = owl_message_get_cc_without_recipient(m);
[839697d]410    while (cc != NULL) {
411      temp = short_zuser(cc->data);
412      if (strcasecmp(temp, frombuff) != 0) {
[e3a75ed]413        filename = g_strdup_printf("%s/%s", logpath, temp);
[839697d]414        owl_log_append(m, filename);
[e3a75ed]415        g_free(filename);
[d0961fe]416      }
[839697d]417
[ddbbcffa]418      g_free(temp);
419      g_free(cc->data);
[839697d]420      cc = g_list_delete_link(cc, cc);
[d0961fe]421    }
422  }
[7d4fbcd]423
[ddbbcffa]424  g_free(frombuff);
425  g_free(logpath);
[7d4fbcd]426}
[cc305b5]427
428static gpointer owl_log_thread_func(gpointer data)
429{
430  log_context = g_main_context_new();
431  log_loop = g_main_loop_new(log_context, FALSE);
432  g_main_loop_run(log_loop);
433  return NULL;
434}
435
436void owl_log_init(void) 
437{
438  GError *error = NULL;
439  logging_thread = g_thread_create(owl_log_thread_func,
440                                   NULL,
441                                   TRUE,
442                                   &error);
443  if (error) {
444    endwin();
445    fprintf(stderr, "Error spawning logging thread: %s\n", error->message);
446    fflush(stderr);
447    exit(1);
448  }
449 
450}
451
452static void owl_log_quit_func(gpointer data)
453{
454  g_main_loop_quit(log_loop);
455}
456
457void owl_log_shutdown(void)
458{
459  owl_select_post_task(owl_log_quit_func, NULL,
460                       NULL, log_context);
461  g_thread_join(logging_thread);
462}
Note: See TracBrowser for help on using the repository browser.