1 /*
2 *
3 * BlueZ - Bluetooth protocol stack for Linux
4 *
5 * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
6 *
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <stdarg.h>
30 #include <syslog.h>
31
32 #include <glib.h>
33
34 #include "log.h"
35
info(const char * format,...)36 void info(const char *format, ...)
37 {
38 va_list ap;
39
40 va_start(ap, format);
41
42 vsyslog(LOG_INFO, format, ap);
43
44 va_end(ap);
45 }
46
error(const char * format,...)47 void error(const char *format, ...)
48 {
49 va_list ap;
50
51 va_start(ap, format);
52
53 vsyslog(LOG_ERR, format, ap);
54
55 va_end(ap);
56 }
57
btd_debug(const char * format,...)58 void btd_debug(const char *format, ...)
59 {
60 va_list ap;
61
62 va_start(ap, format);
63
64 vsyslog(LOG_DEBUG, format, ap);
65
66 va_end(ap);
67 }
68
69 extern struct btd_debug_desc __start___debug[];
70 extern struct btd_debug_desc __stop___debug[];
71
72 static gchar **enabled = NULL;
73
is_enabled(struct btd_debug_desc * desc)74 static gboolean is_enabled(struct btd_debug_desc *desc)
75 {
76 int i;
77
78 if (enabled == NULL)
79 return 0;
80
81 for (i = 0; enabled[i] != NULL; i++) {
82 if (desc->name != NULL && g_pattern_match_simple(enabled[i],
83 desc->name) == TRUE)
84 return 1;
85 if (desc->file != NULL && g_pattern_match_simple(enabled[i],
86 desc->file) == TRUE)
87 return 1;
88 }
89
90 return 0;
91 }
92
__btd_toggle_debug()93 void __btd_toggle_debug()
94 {
95 struct btd_debug_desc *desc;
96
97 for (desc = __start___debug; desc < __stop___debug; desc++)
98 desc->flags |= BTD_DEBUG_FLAG_PRINT;
99 }
100
__btd_log_init(const char * debug,int detach)101 void __btd_log_init(const char *debug, int detach)
102 {
103 int option = LOG_NDELAY | LOG_PID;
104 struct btd_debug_desc *desc;
105 const char *name = NULL, *file = NULL;
106
107 if (debug != NULL)
108 enabled = g_strsplit_set(debug, ":, ", 0);
109
110 for (desc = __start___debug; desc < __stop___debug; desc++) {
111 if (file != NULL || name != NULL) {
112 if (g_strcmp0(desc->file, file) == 0) {
113 if (desc->name == NULL)
114 desc->name = name;
115 } else
116 file = NULL;
117 }
118
119 if (is_enabled(desc))
120 desc->flags |= BTD_DEBUG_FLAG_PRINT;
121 }
122
123 if (!detach)
124 option |= LOG_PERROR;
125
126 openlog("bluetoothd", option, LOG_DAEMON);
127
128 syslog(LOG_INFO, "Bluetooth deamon %s", VERSION);
129 }
130
__btd_log_cleanup(void)131 void __btd_log_cleanup(void)
132 {
133 closelog();
134
135 g_strfreev(enabled);
136 }
137