source: logging.c @ 6cd3a0f

Last change on this file since 6cd3a0f was 6cd3a0f, checked in by Jason Gross <jgross@mit.edu>, 7 years ago
Add comments to logging.c We note that defer_logs is only accessed on the disk-writing thread.
  • Property mode set to 100644
File size: 18.6 KB
RevLine 
[7d4fbcd]1#include "owl.h"
[f271129]2#include <stdio.h>
[7d4fbcd]3
[cc305b5]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;
[6cd3a0f]13static bool defer_logs; /* to be accessed only on the disk-writing thread */
[8f3eac3]14static GQueue *deferred_entry_queue;
[cc305b5]15
[15b34fd]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 */
[c08c70a]20void owl_log_message(const owl_message *m) {
[15b34fd]21  owl_function_debugmsg("owl_log_message: entering");
22
[3c7d086a]23  if (m == NULL) {
24    owl_function_debugmsg("owl_log_message: passed null message");
25    return;
26  }
27
[15b34fd]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 */
[42947f1]42  owl_log_outgoing(m);
43
[15b34fd]44  owl_function_debugmsg("owl_log_message: leaving");
45}
[7d4fbcd]46
[15b34fd]47/* Return 1 if we should log the given message, otherwise return 0 */
[c08c70a]48int owl_log_shouldlog_message(const owl_message *m) {
[4542047]49  const owl_filter *f;
[12c35df]50
[15b34fd]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);
[7d4fbcd]54
[15b34fd]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    }
[7d4fbcd]77  }
[15b34fd]78  return(1);
79}
80
[6829afc]81CALLER_OWN char *owl_log_zephyr(const owl_message *m)
[d427f08]82{
[cc305b5]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
[6829afc]105CALLER_OWN char *owl_log_aim(const owl_message *m)
[d427f08]106{
[cc305b5]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
[6829afc]123CALLER_OWN char *owl_log_jabber(const owl_message *m)
[d427f08]124{
[cc305b5]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
[6829afc]136CALLER_OWN char *owl_log_generic(const owl_message *m)
[d427f08]137{
[cc305b5]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);
[42947f1]153}
154
[9f90937]155static void owl_log_adminmsg_main_thread(gpointer data)
156{
157  owl_function_adminmsg("Logging", (const char*)data);
158}
159
[5ad24f1]160static void G_GNUC_PRINTF(1, 2) owl_log_error(const char *fmt, ...)
[cc305b5]161{
[5ad24f1]162  va_list ap;
163  char *data;
164
165  va_start(ap, fmt);
166  data = g_strdup_vprintf(fmt, ap);
167  va_end(ap);
168
[cc305b5]169  owl_select_post_task(owl_log_error_main_thread,
[5ad24f1]170                       data, g_free, g_main_context_default());
[42947f1]171}
172
[9f90937]173static void G_GNUC_PRINTF(1, 2) owl_log_adminmsg(const char *fmt, ...)
174{
175  va_list ap;
176  char *data;
177
178  va_start(ap, fmt);
179  data = g_strdup_vprintf(fmt, ap);
180  va_end(ap);
181
182  owl_select_post_task(owl_log_adminmsg_main_thread,
183                       data, g_free, g_main_context_default());
184}
185
[8f3eac3]186static CALLER_OWN owl_log_entry *owl_log_new_entry(const char *buffer, const char *filename)
187{
188  owl_log_entry *log_msg = g_slice_new(owl_log_entry);
189  log_msg->message = g_strdup(buffer);
190  log_msg->filename = g_strdup(filename);
191  return log_msg;
192}
193
194static void owl_log_deferred_enqueue_message(const char *buffer, const char *filename)
195{
196  g_queue_push_tail(deferred_entry_queue, owl_log_new_entry(buffer, filename));
197}
198
[979dc4f]199static void owl_log_deferred_enqueue_first_message(const char *buffer, const char *filename)
200{
201  g_queue_push_head(deferred_entry_queue, owl_log_new_entry(buffer, filename));
202}
203
[8f3eac3]204/* write out the entry if possible
205 * return 0 on success, errno on failure to open
206 */
207static int owl_log_try_write_entry(owl_log_entry *msg)
[cc305b5]208{
209  FILE *file = NULL;
210  file = fopen(msg->filename, "a");
211  if (!file) {
[8f3eac3]212    return errno;
[cc305b5]213  }
214  fprintf(file, "%s", msg->message);
215  fclose(file);
[8f3eac3]216  return 0;
[42947f1]217}
218
[cc305b5]219static void owl_log_entry_free(void *data)
220{
221  owl_log_entry *msg = (owl_log_entry*)data;
222  if (msg) {
223    g_free(msg->message);
224    g_free(msg->filename);
[7dcef03]225    g_slice_free(owl_log_entry, msg);
[cc305b5]226  }
227}
228
[8f3eac3]229#if GLIB_CHECK_VERSION(2, 32, 0)
230#else
231static void owl_log_entry_free_gfunc(gpointer data, gpointer user_data)
232{
233  owl_log_entry_free(data);
234}
235#endif
236
[aaee92a]237static void owl_log_file_error(owl_log_entry *msg, int ret)
238{
239  owl_log_error("Unable to open file for logging: %s (file %s)",
240                g_strerror(ret),
241                msg->filename);
242}
243
[8f3eac3]244/* If we are deferring log messages, enqueue this entry for writing.
245 * Otherwise, try to write this log message, and, if it fails with
[6d03e21]246 * EPERM, EACCES, or ETIMEDOUT, go into deferred logging mode and
247 * queue an admin message.  If it fails with anything else, display an
[6cd3a0f]248 * error message, but do not go into deferred logging mode.
249 *
250 * N.B. This function is called only on the disk-writing thread. */
[8f3eac3]251static void owl_log_eventually_write_entry(gpointer data)
252{
253  int ret;
254  owl_log_entry *msg = (owl_log_entry*)data;
255  if (defer_logs) {
256    owl_log_deferred_enqueue_message(msg->message, msg->filename);
257  } else {
258    ret = owl_log_try_write_entry(msg);
[6d03e21]259    if (ret == EPERM || ret == EACCES || ret == ETIMEDOUT) {
[8f3eac3]260      defer_logs = true;
261      owl_log_error("Unable to open file for logging (%s): \n"
262                    "%s.  \n"
263                    "Consider renewing your tickets.  Logging has been \n"
264                    "suspended, and your messages will be saved.  To \n"
265                    "resume logging, use the command :flush-logs.\n\n",
266                    msg->filename,
267                    g_strerror(ret));
[979dc4f]268      /* If we were not in deferred logging mode, either the queue should be
269       * empty, or we are attempting to log a message that we just popped off
270       * the head of the queue.  Either way, we should enqueue this message as
271       * the first message in the queue, rather than the last, so that we
272       * preserve message ordering. */
273      owl_log_deferred_enqueue_first_message(msg->message, msg->filename);
[8f3eac3]274    } else if (ret != 0) {
[aaee92a]275      owl_log_file_error(msg, ret);
[8f3eac3]276    }
277  }
278}
279
[6cd3a0f]280/* tries to write the deferred log entries
281 *
282 * N.B. This function is called only on the disk-writing thread. */
[8f3eac3]283static void owl_log_write_deferred_entries(gpointer data)
284{
285  owl_log_entry *entry;
[aaee92a]286  bool drop_failed_logs = *(bool *)data;
287  int ret;
[0e64661]288  int logged_message_count = 0;
[9f90937]289  bool all_succeeded = true;
[8f3eac3]290
291  defer_logs = false;
292  while (!g_queue_is_empty(deferred_entry_queue) && !defer_logs) {
[0e64661]293    logged_message_count++;
[8f3eac3]294    entry = (owl_log_entry*)g_queue_pop_head(deferred_entry_queue);
[979dc4f]295    if (!drop_failed_logs) {
296      /* Attempt to write the log entry.  If it fails, re-queue the entry at
297       * the head of the queue. */
[aaee92a]298      owl_log_eventually_write_entry(entry);
299    } else {
[979dc4f]300      /* Attempt to write the log entry. If it fails, print an error message,
301       * drop the log, and keep going through the queue. */
[aaee92a]302      ret = owl_log_try_write_entry(entry);
303      if (ret != 0) {
[9f90937]304        all_succeeded = false;
[aaee92a]305        owl_log_file_error(entry, ret);
306      }
307    }
[8f3eac3]308    owl_log_entry_free(entry);
309  }
[0e64661]310  if (all_succeeded && logged_message_count > 0 && !defer_logs) {
[9f90937]311    owl_log_adminmsg("Logs have been flushed and logging has resumed.");
[0e64661]312  } else if (!all_succeeded && logged_message_count > 0 && !defer_logs) {
313    owl_log_adminmsg("Logs have been flushed or dropped and logging has resumed.");
314  } else if (logged_message_count > 0 && defer_logs) {
315    owl_log_error("Attempted to flush %d logs; %u deferred logs remain.",
316                  logged_message_count, g_queue_get_length(deferred_entry_queue));
[9f90937]317  }
[8f3eac3]318}
319
[aaee92a]320void owl_log_flush_logs(bool drop_failed_logs)
[8f3eac3]321{
[aaee92a]322  bool *data = g_new(bool, 1);
323  *data = drop_failed_logs;
324
325  owl_select_post_task(owl_log_write_deferred_entries,
326                       data,
327                       g_free,
328                       log_context);
[8f3eac3]329}
330
[cc305b5]331void owl_log_enqueue_message(const char *buffer, const char *filename)
332{
[8f3eac3]333  owl_log_entry *log_msg = owl_log_new_entry(buffer, filename);
334  owl_select_post_task(owl_log_eventually_write_entry, log_msg,
[cc305b5]335                       owl_log_entry_free, log_context);
[42947f1]336}
337
[c08c70a]338void owl_log_append(const owl_message *m, const char *filename) {
[cc305b5]339  char *buffer = NULL;
340  if (owl_message_is_type_zephyr(m)) {
341    buffer = owl_log_zephyr(m);
342  } else if (owl_message_is_type_jabber(m)) {
343    buffer = owl_log_jabber(m);
344  } else if (owl_message_is_type_aim(m)) {
345    buffer = owl_log_aim(m);
346  } else {
347    buffer = owl_log_generic(m);
348  }
349  owl_log_enqueue_message(buffer, filename);
350  g_free(buffer);
[42947f1]351}
352
[c08c70a]353void owl_log_outgoing(const owl_message *m)
[15b34fd]354{
[e3a75ed]355  char *filename, *logpath;
[9c590d4]356  char *to, *temp;
[839697d]357  GList *cc;
[9c590d4]358
359  /* expand ~ in path names */
[60d7935]360  logpath = owl_util_makepath(owl_global_get_logpath(&g));
[15b34fd]361
[42947f1]362  /* Figure out what path to log to */
363  if (owl_message_is_type_zephyr(m)) {
[af1920fd]364    /* If this has CC's, do all but the "recipient" which we'll do below */
[839697d]365    cc = owl_message_get_cc_without_recipient(m);
366    while (cc != NULL) {
367      temp = short_zuser(cc->data);
[dde1b4d]368      filename = g_build_filename(logpath, temp, NULL);
[839697d]369      owl_log_append(m, filename);
370
[e3a75ed]371      g_free(filename);
[ddbbcffa]372      g_free(temp);
373      g_free(cc->data);
[839697d]374      cc = g_list_delete_link(cc, cc);
[9c590d4]375    }
[839697d]376
[9c590d4]377    to = short_zuser(owl_message_get_recipient(m));
[42947f1]378  } else if (owl_message_is_type_jabber(m)) {
[3472845]379    to = g_strdup_printf("jabber:%s", owl_message_get_recipient(m));
[7865479]380    g_strdelimit(to, "/", '_');
[42947f1]381  } else if (owl_message_is_type_aim(m)) {
[28ee32b]382    char *temp2;
[9c590d4]383    temp = owl_aim_normalize_screenname(owl_message_get_recipient(m));
[28ee32b]384    temp2 = g_utf8_strdown(temp,-1);
[3472845]385    to = g_strdup_printf("aim:%s", temp2);
[ddbbcffa]386    g_free(temp2);
387    g_free(temp);
[42947f1]388  } else {
[d4927a7]389    to = g_strdup("loopback");
[42947f1]390  }
[7d4fbcd]391
[dde1b4d]392  filename = g_build_filename(logpath, to, NULL);
[42947f1]393  owl_log_append(m, filename);
[ddbbcffa]394  g_free(to);
[e3a75ed]395  g_free(filename);
[7d4fbcd]396
[dde1b4d]397  filename = g_build_filename(logpath, "all", NULL);
[42947f1]398  owl_log_append(m, filename);
[ddbbcffa]399  g_free(logpath);
[e3a75ed]400  g_free(filename);
[7d4fbcd]401}
402
[42947f1]403
[24ccc01]404void owl_log_outgoing_zephyr_error(const owl_zwrite *zw, const char *text)
[2b86d14]405{
[e3a75ed]406  char *filename, *logpath;
[fe3b017]407  char *tobuff, *recip;
[180cd15]408  owl_message *m;
[cc305b5]409  GString *msgbuf;
[180cd15]410  /* create a present message so we can pass it to
[c79a047]411   * owl_log_shouldlog_message(void)
[180cd15]412   */
[7dcef03]413  m = g_slice_new(owl_message);
[e5da3fe]414  /* recip_index = 0 because there can only be one recipient anyway */
415  owl_message_create_from_zwrite(m, zw, text, 0);
[180cd15]416  if (!owl_log_shouldlog_message(m)) {
[91634ec]417    owl_message_delete(m);
[180cd15]418    return;
419  }
[91634ec]420  owl_message_delete(m);
[2b86d14]421
[180cd15]422  /* chop off a local realm */
[fe3b017]423  recip = owl_zwrite_get_recip_n_with_realm(zw, 0);
424  tobuff = short_zuser(recip);
425  g_free(recip);
[2b86d14]426
427  /* expand ~ in path names */
[60d7935]428  logpath = owl_util_makepath(owl_global_get_logpath(&g));
[dde1b4d]429  filename = g_build_filename(logpath, tobuff, NULL);
[cc305b5]430  msgbuf = g_string_new("");
431  g_string_printf(msgbuf, "ERROR (owl): %s\n%s\n", tobuff, text);
432  if (text[strlen(text)-1] != '\n') {
433    g_string_append_printf(msgbuf, "\n");
[2b86d14]434  }
[cc305b5]435  owl_log_enqueue_message(msgbuf->str, filename);
436  g_string_free(msgbuf, TRUE);
[2b86d14]437
[dde1b4d]438  filename = g_build_filename(logpath, "all", NULL);
[ddbbcffa]439  g_free(logpath);
[cc305b5]440  msgbuf = g_string_new("");
441  g_string_printf(msgbuf, "ERROR (owl): %s\n%s\n", tobuff, text);
442  if (text[strlen(text)-1] != '\n') {
443    g_string_append_printf(msgbuf, "\n");
[2b86d14]444  }
[cc305b5]445  owl_log_enqueue_message(msgbuf->str, filename);
446  g_string_free(msgbuf, TRUE);
[2b86d14]447
[ddbbcffa]448  g_free(tobuff);
[2b86d14]449}
450
[c08c70a]451void owl_log_incoming(const owl_message *m)
[15283bb]452{
[e3a75ed]453  char *filename, *allfilename, *logpath;
[e19eb97]454  const char *from=NULL;
[65b2173]455  char *frombuff=NULL;
[7d4fbcd]456  int len, ch, i, personal;
[12c35df]457
[15b34fd]458  /* figure out if it's a "personal" message or not */
[aac889a]459  if (owl_message_is_type_zephyr(m)) {
460    if (owl_message_is_personal(m)) {
[42947f1]461      personal = 1;
[aac889a]462    } else {
[42947f1]463      personal = 0;
[aac889a]464    }
[2182be3]465  } else if (owl_message_is_type_jabber(m)) {
[42947f1]466    /* This needs to be fixed to handle groupchat */
[e19eb97]467    const char* msgtype = owl_message_get_attribute_value(m,"jtype");
[42947f1]468    if (msgtype && !strcmp(msgtype,"groupchat")) {
469      personal = 0;
470    } else {
471      personal = 1;
472    }
[7d4fbcd]473  } else {
[79a0e82]474    if (owl_message_is_private(m) || owl_message_is_loginout(m)) {
[42947f1]475      personal = 1;
[aac889a]476    } else {
[42947f1]477      personal = 0;
[aac889a]478    }
[7d4fbcd]479  }
480
[2182be3]481
[aac889a]482  if (owl_message_is_type_zephyr(m)) {
483    if (personal) {
[3066d23]484      from=frombuff=short_zuser(owl_message_get_sender(m));
[aac889a]485    } else {
[d4927a7]486      from=frombuff=g_strdup(owl_message_get_class(m));
[7d4fbcd]487    }
[aac889a]488  } else if (owl_message_is_type_aim(m)) {
489    /* we do not yet handle chat rooms */
[28ee32b]490    char *normalto, *temp;
491    temp = owl_aim_normalize_screenname(owl_message_get_sender(m));
492    normalto = g_utf8_strdown(temp, -1);
[3472845]493    from=frombuff=g_strdup_printf("aim:%s", normalto);
[ddbbcffa]494    g_free(normalto);
495    g_free(temp);
[37eab7f]496  } else if (owl_message_is_type_loopback(m)) {
[d4927a7]497    from=frombuff=g_strdup("loopback");
[2182be3]498  } else if (owl_message_is_type_jabber(m)) {
[5c30091]499    if (personal) {
[cc305b5]500      from=frombuff=g_strdup_printf("jabber:%s", 
501                                    owl_message_get_sender(m));
[5c30091]502    } else {
[cc305b5]503      from=frombuff=g_strdup_printf("jabber:%s", 
504                                    owl_message_get_recipient(m));
[5c30091]505    }
[e6449bc]506  } else {
[d4927a7]507    from=frombuff=g_strdup("unknown");
[7d4fbcd]508  }
509 
510  /* check for malicious sender formats */
511  len=strlen(frombuff);
512  if (len<1 || len>35) from="weird";
513  if (strchr(frombuff, '/')) from="weird";
514
515  ch=frombuff[0];
[28ee32b]516  if (!g_ascii_isalnum(ch)) from="weird";
[7d4fbcd]517
518  for (i=0; i<len; i++) {
[e1c4636]519    if (frombuff[i]<'!' || frombuff[i]>='~') from="weird";
[7d4fbcd]520  }
521
522  if (!strcmp(frombuff, ".") || !strcasecmp(frombuff, "..")) from="weird";
523
524  if (!personal) {
[28ee32b]525    if (strcmp(from, "weird")) {
526      char* temp = g_utf8_strdown(frombuff, -1);
527      if (temp) {
[ddbbcffa]528        g_free(frombuff);
[28ee32b]529        from = frombuff = temp;
530      }
531    }
[7d4fbcd]532  }
533
[e1c4636]534  /* create the filename (expanding ~ in path names) */
[7d4fbcd]535  if (personal) {
[60d7935]536    logpath = owl_util_makepath(owl_global_get_logpath(&g));
[dde1b4d]537    filename = g_build_filename(logpath, from, NULL);
538    allfilename = g_build_filename(logpath, "all", NULL);
[d0961fe]539    owl_log_append(m, allfilename);
[e3a75ed]540    g_free(allfilename);
[7d4fbcd]541  } else {
[60d7935]542    logpath = owl_util_makepath(owl_global_get_classlogpath(&g));
[dde1b4d]543    filename = g_build_filename(logpath, from, NULL);
[7d4fbcd]544  }
[37eab7f]545
[42947f1]546  owl_log_append(m, filename);
[e3a75ed]547  g_free(filename);
[7d4fbcd]548
[d0961fe]549  if (personal && owl_message_is_type_zephyr(m)) {
[af1920fd]550    /* We want to log to all of the CC'd people who were not us, or
551     * the sender, as well.
552     */
[839697d]553    char *temp;
554    GList *cc;
[d0961fe]555    cc = owl_message_get_cc_without_recipient(m);
[839697d]556    while (cc != NULL) {
557      temp = short_zuser(cc->data);
558      if (strcasecmp(temp, frombuff) != 0) {
[dde1b4d]559        filename = g_build_filename(logpath, temp, NULL);
[839697d]560        owl_log_append(m, filename);
[e3a75ed]561        g_free(filename);
[d0961fe]562      }
[839697d]563
[ddbbcffa]564      g_free(temp);
565      g_free(cc->data);
[839697d]566      cc = g_list_delete_link(cc, cc);
[d0961fe]567    }
568  }
[7d4fbcd]569
[ddbbcffa]570  g_free(frombuff);
571  g_free(logpath);
[7d4fbcd]572}
[cc305b5]573
574static gpointer owl_log_thread_func(gpointer data)
575{
[8f3eac3]576  log_context = g_main_context_new();
[cc305b5]577  log_loop = g_main_loop_new(log_context, FALSE);
578  g_main_loop_run(log_loop);
579  return NULL;
580}
581
[8f3eac3]582void owl_log_init(void)
[cc305b5]583{
[0a9ffc5]584  log_context = g_main_context_new();
[0792d99]585#if GLIB_CHECK_VERSION(2, 31, 0)
586  logging_thread = g_thread_new("logging",
587                                owl_log_thread_func,
588                                NULL);
589#else
590  GError *error = NULL;
[cc305b5]591  logging_thread = g_thread_create(owl_log_thread_func,
592                                   NULL,
593                                   TRUE,
594                                   &error);
595  if (error) {
596    endwin();
597    fprintf(stderr, "Error spawning logging thread: %s\n", error->message);
598    fflush(stderr);
599    exit(1);
600  }
[0792d99]601#endif
[8f3eac3]602
603  deferred_entry_queue = g_queue_new();
[cc305b5]604}
605
606static void owl_log_quit_func(gpointer data)
607{
[8f3eac3]608  /* flush the deferred logs queue, trying to write the
[aaee92a]609   * entries to the disk one last time.  Drop any failed
610   * entries */
611  bool bool_true = true;
612  owl_log_write_deferred_entries(&bool_true);
[8f3eac3]613#if GLIB_CHECK_VERSION(2, 32, 0)
614  g_queue_free_full(deferred_entry_queue, owl_log_entry_free);
615#else
616  g_queue_foreach(deferred_entry_queue, owl_log_entry_free_gfunc, NULL);
617  g_queue_free(deferred_entry_queue);
618#endif
619
[cc305b5]620  g_main_loop_quit(log_loop);
621}
622
623void owl_log_shutdown(void)
624{
625  owl_select_post_task(owl_log_quit_func, NULL,
626                       NULL, log_context);
627  g_thread_join(logging_thread);
628}
Note: See TracBrowser for help on using the repository browser.