1 /* Gnome Music Player Client (GMPC)
2  * Copyright (C) 2004-2011 Qball Cow <qball@gmpclient.org>
3  * Project homepage: http://gmpclient.org/
4 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14 
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <strings.h>
25 
26 #include <glib.h>
27 #include <glib/gstdio.h>
28 #include <libmpd/debug_printf.h>
29 
30 #include "log.h"
31 
32 #define LOG_DOMAIN "Gmpc.Log"
33 
34 
35 static GLogLevelFlags global_log_level = G_LOG_LEVEL_MESSAGE;
36 
gmpc_log_func(const gchar * log_domain,GLogLevelFlags log_level,const gchar * message,gpointer user_data)37 static void gmpc_log_func(
38             const gchar * log_domain,
39             GLogLevelFlags log_level,
40             const gchar * message,
41             gpointer user_data)
42 {
43     if (log_level <= global_log_level)
44     {
45         g_log_default_handler(log_domain, log_level, message, user_data);
46     }
47 }
48 
49 
log_add_filter(const gchar * option_name,const gchar * value,gpointer data,GError ** error)50 gboolean log_add_filter(
51             const gchar * option_name,
52             const gchar * value,
53             gpointer data,
54             GError ** error)
55 {
56     if (value == NULL || value[0] == 0)
57     {
58         g_set_error(error, 0, 0, "--log-filter requires a log domain as argument");
59         return FALSE;
60     }
61 
62     g_log_set_handler(
63         value,
64         G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION,
65         g_log_default_handler,
66         NULL);
67     return TRUE;
68 }
69 
log_init(void)70 void log_init(void)
71 {
72 
73     g_log_set_default_handler(gmpc_log_func, NULL);
74 
75 }
76 
log_set_debug_level(int debug_level)77 void log_set_debug_level(int debug_level)
78 {
79     /**
80      * Set debug level, options are
81      * 0 = No debug
82      * 1 = Error messages
83      * 2 = Error + Warning messages
84      * 3 = All messages
85      */
86     if (debug_level >= 0)
87     {
88         if (debug_level == 3)
89         {
90             global_log_level = G_LOG_LEVEL_DEBUG;
91         } else if (debug_level == 2)
92         {
93             global_log_level = G_LOG_LEVEL_INFO;
94         }
95         debug_set_level(debug_level);
96     }
97 }
98