source: logging.c @ bf61c61

Last change on this file since bf61c61 was bf61c61, checked in by Jason Gross <jgross@mit.edu>, 8 years ago
Moved notification of message logging to perl
  • Property mode set to 100644
File size: 2.7 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;
13
14static void owl_log_error_main_thread(gpointer data)
15{
16  owl_function_error("%s", (const char*)data);
17}
18
19static void G_GNUC_PRINTF(1, 2) owl_log_error(const char *fmt, ...)
20{
21  va_list ap;
22  char *data;
23
24  va_start(ap, fmt);
25  data = g_strdup_vprintf(fmt, ap);
26  va_end(ap);
27
28  owl_select_post_task(owl_log_error_main_thread,
29                       data, g_free, g_main_context_default());
30}
31
32static void owl_log_write_entry(gpointer data)
33{
34  owl_log_entry *msg = (owl_log_entry*)data;
35  FILE *file = NULL;
36  file = fopen(msg->filename, "a");
37  if (!file) {
38    owl_log_error("Unable to open file for logging (%s)", msg->filename);
39    return;
40  }
41  fprintf(file, "%s", msg->message);
42  fclose(file);
43}
44
45static void owl_log_entry_free(void *data)
46{
47  owl_log_entry *msg = (owl_log_entry*)data;
48  if (msg) {
49    g_free(msg->message);
50    g_free(msg->filename);
51    g_slice_free(owl_log_entry, msg);
52  }
53}
54
55void owl_log_enqueue_message(const char *buffer, const char *filename)
56{
57  owl_log_entry *log_msg = NULL; 
58  log_msg = g_slice_new(owl_log_entry);
59  log_msg->message = g_strdup(buffer);
60  log_msg->filename = g_strdup(filename);
61  owl_select_post_task(owl_log_write_entry, log_msg, 
62                       owl_log_entry_free, log_context);
63}
64
65void owl_log_outgoing_zephyr_error(const owl_zwrite *zw, const char *text)
66{
67  owl_message *m = g_slice_new(owl_message);
68  /* recip_index = 0 because there can only be one recipient anyway */
69  owl_message_create_from_zwrite(m, zw, text, 0);
70  g_free(owl_perlconfig_call_with_message("BarnOwl::Logging::log_outgoing_error", m));
71  owl_message_delete(m);
72}
73
74static gpointer owl_log_thread_func(gpointer data)
75{
76  log_loop = g_main_loop_new(log_context, FALSE);
77  g_main_loop_run(log_loop);
78  return NULL;
79}
80
81void owl_log_init(void) 
82{
83  log_context = g_main_context_new();
84#if GLIB_CHECK_VERSION(2, 31, 0)
85  logging_thread = g_thread_new("logging",
86                                owl_log_thread_func,
87                                NULL);
88#else
89  GError *error = NULL;
90  logging_thread = g_thread_create(owl_log_thread_func,
91                                   NULL,
92                                   TRUE,
93                                   &error);
94  if (error) {
95    endwin();
96    fprintf(stderr, "Error spawning logging thread: %s\n", error->message);
97    fflush(stderr);
98    exit(1);
99  }
100#endif
101 
102}
103
104static void owl_log_quit_func(gpointer data)
105{
106  g_main_loop_quit(log_loop);
107}
108
109void owl_log_shutdown(void)
110{
111  owl_select_post_task(owl_log_quit_func, NULL,
112                       NULL, log_context);
113  g_thread_join(logging_thread);
114}
Note: See TracBrowser for help on using the repository browser.