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
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;
13
14static void owl_log_error_main_thread(gpointer data)
15{
16  owl_function_error("%s", (const char*)data);
[42947f1]17}
18
[db1a2f8b]19static void G_GNUC_PRINTF(1, 2) owl_log_error(const char *fmt, ...)
[cc305b5]20{
[db1a2f8b]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
[cc305b5]28  owl_select_post_task(owl_log_error_main_thread,
[db1a2f8b]29                       data, g_free, g_main_context_default());
[42947f1]30}
31
[cc305b5]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) {
[db1a2f8b]38    owl_log_error("Unable to open file for logging (%s)", msg->filename);
[cc305b5]39    return;
40  }
41  fprintf(file, "%s", msg->message);
42  fclose(file);
[42947f1]43}
44
[cc305b5]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);
[7dcef03]51    g_slice_free(owl_log_entry, msg);
[cc305b5]52  }
53}
54
55void owl_log_enqueue_message(const char *buffer, const char *filename)
56{
57  owl_log_entry *log_msg = NULL; 
[7dcef03]58  log_msg = g_slice_new(owl_log_entry);
[cc305b5]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);
[42947f1]63}
64
[24ccc01]65void owl_log_outgoing_zephyr_error(const owl_zwrite *zw, const char *text)
[2b86d14]66{
[6e764aa]67  owl_message *m = g_slice_new(owl_message);
[e5da3fe]68  /* recip_index = 0 because there can only be one recipient anyway */
69  owl_message_create_from_zwrite(m, zw, text, 0);
[6e764aa]70  g_free(owl_perlconfig_call_with_message("BarnOwl::Logging::log_outgoing_error", m));
[f511c1e]71  owl_message_delete(m);
[2b86d14]72}
73
[cc305b5]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{
[0a9ffc5]83  log_context = g_main_context_new();
[0792d99]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;
[cc305b5]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  }
[0792d99]100#endif
[cc305b5]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.