3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
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.
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.
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
36 #include <sys/socket.h>
40 #include "lib/bluetooth.h"
43 #include "src/shared/util.h"
46 #define LOG_IDENT "bluetoothd"
47 #define LOG_IDENT_LEN sizeof(LOG_IDENT)
55 } __attribute__((packed));
57 static int logging_fd = -1;
59 static void logging_open(void)
61 struct sockaddr_hci addr;
67 fd = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
71 memset(&addr, 0, sizeof(addr));
72 addr.hci_family = AF_BLUETOOTH;
73 addr.hci_dev = HCI_DEV_NONE;
74 addr.hci_channel = HCI_CHANNEL_LOGGING;
76 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
84 static void logging_close(void)
86 if (logging_fd >= 0) {
92 static void logging_log(uint16_t index, int priority,
93 const char *format, va_list ap)
101 if (vasprintf(&str, format, ap) < 0)
104 len = strlen(str) + 1;
106 hdr.opcode = cpu_to_le16(0x0000);
107 hdr.index = cpu_to_le16(index);
108 hdr.len = cpu_to_le16(2 + LOG_IDENT_LEN + len);
109 hdr.priority = priority;
110 hdr.ident_len = LOG_IDENT_LEN;
112 iov[0].iov_base = &hdr;
113 iov[0].iov_len = sizeof(hdr);
115 iov[1].iov_base = LOG_IDENT;
116 iov[1].iov_len = LOG_IDENT_LEN;
118 iov[2].iov_base = str;
119 iov[2].iov_len = len;
121 memset(&msg, 0, sizeof(msg));
125 if (sendmsg(logging_fd, &msg, 0) < 0) {
126 if (errno != ENODEV) {
135 void error(const char *format, ...)
139 va_start(ap, format);
140 vsyslog(LOG_ERR, format, ap);
146 va_start(ap, format);
147 logging_log(HCI_DEV_NONE, LOG_ERR, format, ap);
151 void warn(const char *format, ...)
155 va_start(ap, format);
156 vsyslog(LOG_WARNING, format, ap);
162 va_start(ap, format);
163 logging_log(HCI_DEV_NONE, LOG_WARNING, format, ap);
167 void info(const char *format, ...)
171 va_start(ap, format);
172 vsyslog(LOG_INFO, format, ap);
178 va_start(ap, format);
179 logging_log(HCI_DEV_NONE, LOG_INFO, format, ap);
183 void btd_log(uint16_t index, int priority, const char *format, ...)
187 va_start(ap, format);
188 vsyslog(priority, format, ap);
194 va_start(ap, format);
195 logging_log(index, priority, format, ap);
199 void btd_error(uint16_t index, const char *format, ...)
203 va_start(ap, format);
204 vsyslog(LOG_ERR, format, ap);
210 va_start(ap, format);
211 logging_log(index, LOG_ERR, format, ap);
215 void btd_warn(uint16_t index, const char *format, ...)
219 va_start(ap, format);
220 vsyslog(LOG_WARNING, format, ap);
226 va_start(ap, format);
227 logging_log(index, LOG_WARNING, format, ap);
231 void btd_info(uint16_t index, const char *format, ...)
235 va_start(ap, format);
236 vsyslog(LOG_INFO, format, ap);
242 va_start(ap, format);
243 logging_log(index, LOG_INFO, format, ap);
247 void btd_debug(uint16_t index, const char *format, ...)
251 va_start(ap, format);
252 vsyslog(LOG_DEBUG, format, ap);
258 va_start(ap, format);
259 logging_log(index, LOG_DEBUG, format, ap);
263 extern struct btd_debug_desc __start___debug[];
264 extern struct btd_debug_desc __stop___debug[];
266 static char **enabled = NULL;
268 static gboolean is_enabled(struct btd_debug_desc *desc)
275 for (i = 0; enabled[i] != NULL; i++)
276 if (desc->file != NULL && g_pattern_match_simple(enabled[i],
283 void __btd_enable_debug(struct btd_debug_desc *start,
284 struct btd_debug_desc *stop)
286 struct btd_debug_desc *desc;
288 if (start == NULL || stop == NULL)
291 for (desc = start; desc < stop; desc++) {
292 if (is_enabled(desc))
293 desc->flags |= BTD_DEBUG_FLAG_PRINT;
297 void __btd_toggle_debug(void)
299 struct btd_debug_desc *desc;
301 for (desc = __start___debug; desc < __stop___debug; desc++)
302 desc->flags |= BTD_DEBUG_FLAG_PRINT;
305 #ifdef __TIZEN_PATCH__
306 void __hci_attach_log_init(void)
308 int option = LOG_NDELAY | LOG_PID;
310 enabled = g_strsplit_set(g_strdup("*"), ":, ", 0);
312 __btd_enable_debug(__start___debug, __stop___debug);
314 openlog("hciattach", option, LOG_DAEMON);
316 syslog(LOG_INFO, "hciattach daemon for debugging");
321 void __btd_log_init(const char *debug, int detach)
323 int option = LOG_NDELAY | LOG_PID;
326 enabled = g_strsplit_set(debug, ":, ", 0);
328 __btd_enable_debug(__start___debug, __stop___debug);
333 option |= LOG_PERROR;
335 openlog(LOG_IDENT, option, LOG_DAEMON);
337 info("Bluetooth daemon %s", VERSION);
340 void __btd_log_cleanup(void)