XMMS2
log.c
Go to the documentation of this file.
1 /* XMMS2 - X Music Multiplexer System
2  * Copyright (C) 2003-2011 XMMS2 Team
3  *
4  * PLUGINS ARE NOT CONSIDERED TO BE DERIVED WORK !!!
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  */
16 
17 /** @file
18  * Logging functions.
19  *
20  */
21 
22 #include <time.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <glib.h>
27 #include "xmmspriv/xmms_log.h"
29 
30 static gchar *logts_format = NULL;
31 static void xmms_log_handler (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data);
32 
33 
34 void
35 xmms_log_set_format (const gchar *format)
36 {
37  /* copying the string ensures the formatting directives will
38  * last until log_shutdown */
39  g_free (logts_format);
40  logts_format = g_strdup (format);
41 }
42 
43 void
44 xmms_log_init (gint verbosity)
45 {
46  g_log_set_handler (NULL, G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL, xmms_log_handler,
47  GINT_TO_POINTER (verbosity));
48 
49  xmms_log_info ("Initialized logging system :)");
50 }
51 
52 void
54 {
55  xmms_log_info ("Logging says bye bye :)");
56  g_free (logts_format);
57  logts_format = NULL;
58 }
59 
60 
61 static void
62 xmms_log_handler (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data)
63 {
64  gchar logts_buf[256];
65  time_t tv = 0;
66  struct tm st;
67  const char *level = "??";
68  gint verbosity = GPOINTER_TO_INT (user_data);
69 
70  tv = time (NULL);
71 
72  if (log_level & G_LOG_LEVEL_CRITICAL) {
73  level = " FAIL";
74  } else if (log_level & G_LOG_LEVEL_ERROR) {
75  level = "FATAL";
76  } else if (log_level & G_LOG_LEVEL_WARNING) {
77  level = "ERROR";
78  } else if (log_level & G_LOG_LEVEL_MESSAGE) {
79  level = " INFO";
80  if (verbosity < 1)
81  return;
82  } else if (log_level & G_LOG_LEVEL_DEBUG) {
83  level = "DEBUG";
84  if (verbosity < 2)
85  return;
86  }
87 
88  if (!logts_format ||
89  !xmms_localtime (&tv, &st) ||
90  !strftime (logts_buf, sizeof(logts_buf), logts_format, &st)) {
91  logts_buf[0] = '\0';
92  }
93 
94  printf ("%s%s: %s\n", logts_buf, level, message);
95 
96  fflush (stdout);
97 
98  if (log_level & G_LOG_LEVEL_ERROR) {
99  exit (EXIT_FAILURE);
100  }
101 }
void xmms_log_init(gint verbosity)
Definition: log.c:44
#define xmms_log_info(fmt,...)
Definition: xmms_log.h:34
void xmms_log_shutdown()
Definition: log.c:53
void xmms_log_set_format(const gchar *format)
Definition: log.c:35
gboolean xmms_localtime(const time_t *tt, struct tm *res)