source: logging.c @ 6e764aa

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