1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * Copyright (C) 2003 Imendio AB
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (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 GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, see <https://www.gnu.org/licenses>
17 */
18
19 #include "lm-debug.h"
20
21 #ifndef LM_NO_DEBUG
22
23 static LmLogLevelFlags debug_flags = 0;
24 static gboolean initialized = FALSE;
25
26 static const GDebugKey debug_keys[] = {
27 {"VERBOSE", LM_LOG_LEVEL_VERBOSE},
28 {"NET", LM_LOG_LEVEL_NET},
29 {"PARSER", LM_LOG_LEVEL_PARSER},
30 {"SSL", LM_LOG_LEVEL_SSL},
31 {"SASL", LM_LOG_LEVEL_SASL},
32 {"ALL", LM_LOG_LEVEL_ALL}
33 };
34
35 #define NUM_DEBUG_KEYS (sizeof (debug_keys) / sizeof (GDebugKey))
36
37 static void
debug_log_handler(const gchar * log_domain,GLogLevelFlags log_level,const gchar * message,gpointer user_data)38 debug_log_handler (const gchar *log_domain,
39 GLogLevelFlags log_level,
40 const gchar *message,
41 gpointer user_data)
42 {
43 if (debug_flags & log_level) {
44 if (log_level & LM_LOG_LEVEL_VERBOSE) {
45 g_print ("*** ");
46 }
47 else if (log_level & LM_LOG_LEVEL_PARSER) {
48 g_print ("LM-PARSER: ");
49 }
50 else if (log_level & LM_LOG_LEVEL_SASL) {
51 g_print ("LM-SASL: ");
52 }
53 else if (log_level & LM_LOG_LEVEL_SSL) {
54 g_print ("LM-SSL: ");
55 }
56
57 g_print ("%s", message);
58 }
59 }
60
61 /**
62 * lm_debug_init
63 *
64 * Initialized the debug system.
65 **/
66 void
lm_debug_init(void)67 lm_debug_init (void)
68 {
69 const gchar *env_lm_debug;
70
71 if (initialized) {
72 return;
73 }
74
75 env_lm_debug = g_getenv ("LM_DEBUG");
76 if (env_lm_debug) {
77 debug_flags = g_parse_debug_string (env_lm_debug, debug_keys,
78 NUM_DEBUG_KEYS);
79 }
80
81 g_log_set_handler (LM_LOG_DOMAIN, LM_LOG_LEVEL_ALL,
82 debug_log_handler, NULL);
83
84 initialized = TRUE;
85 }
86
87 #else /* LM_NO_DEBUG */
88
89 static void
do_nothing_log_handler(const gchar * log_domain,GLogLevelFlags log_level,const gchar * message,gpointer user_data)90 do_nothing_log_handler (const gchar *log_domain,
91 GLogLevelFlags log_level,
92 const gchar *message,
93 gpointer user_data)
94 {
95 }
96
97 void
lm_debug_init(void)98 lm_debug_init (void)
99 {
100 g_log_set_handler (LM_LOG_DOMAIN, LM_LOG_LEVEL_ALL,
101 do_nothing_log_handler, NULL);
102 }
103
104 #endif /* LM_NO_DEBUG */
105
106