File: lm-debug.c

package info (click to toggle)
loudmouth 1.4.3-9
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 3,132 kB
  • sloc: sh: 9,112; ansic: 8,939; xml: 2,597; makefile: 182
file content (97 lines) | stat: -rw-r--r-- 2,339 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2003 Imendio AB
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "lm-debug.h"

#ifndef LM_NO_DEBUG

static LmLogLevelFlags debug_flags = 0;
static gboolean initialized = FALSE;

static const GDebugKey debug_keys[] = {
	{"VERBOSE",      LM_LOG_LEVEL_VERBOSE},
	{"NET",          LM_LOG_LEVEL_NET},
	{"PARSER",       LM_LOG_LEVEL_PARSER},
	{"SSL",          LM_LOG_LEVEL_SSL},
	{"SASL",         LM_LOG_LEVEL_SASL},
	{"ALL",          LM_LOG_LEVEL_ALL}
};

#define NUM_DEBUG_KEYS (sizeof (debug_keys) / sizeof (GDebugKey))

static void
debug_log_handler (const gchar    *log_domain,
		   GLogLevelFlags  log_level,
		   const gchar    *message,
		   gpointer        user_data)
{
	if (debug_flags & log_level) {
		if (log_level & LM_LOG_LEVEL_VERBOSE) {
			g_print ("*** ");
		}
		else if (log_level & LM_LOG_LEVEL_PARSER) {
			g_print ("LM-PARSER: ");
		} 
		else if (log_level & LM_LOG_LEVEL_SASL) {
			g_print ("LM-SASL: ");
		}
		else if (log_level & LM_LOG_LEVEL_SSL) {
			g_print ("LM-SSL: ");
		}
	
		g_print ("%s", message);
	}
}

/**
 * lm_debug_init
 *
 * Initialized the debug system.
 **/
void 
lm_debug_init (void)
{
	const gchar *env_lm_debug;

	if (initialized) {
		return;
	}
	
	env_lm_debug = g_getenv ("LM_DEBUG");
	if (env_lm_debug) {
		debug_flags = g_parse_debug_string (env_lm_debug, debug_keys,
						    NUM_DEBUG_KEYS);
	}

	g_log_set_handler (LM_LOG_DOMAIN, LM_LOG_LEVEL_ALL, 
			   debug_log_handler, NULL);

	initialized = TRUE;
}

#else  /* LM_NO_DEBUG */

void 
lm_debug_init (void)
{
}

#endif /* LM_NO_DEBUG */