Upgrade bluez5_37 :Merge the code from private
[platform/upstream/bluez.git] / src / log.c
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 <errno.h>
30 #include <syslog.h>
31 #include <stdarg.h>
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <string.h>
36 #include <sys/socket.h>
37
38 #include <glib.h>
39
40 #include "lib/bluetooth.h"
41 #include "lib/hci.h"
42
43 #include "src/shared/util.h"
44 #include "log.h"
45
46 #define LOG_IDENT "bluetoothd"
47 #define LOG_IDENT_LEN sizeof(LOG_IDENT)
48
49 struct log_hdr {
50         uint16_t opcode;
51         uint16_t index;
52         uint16_t len;
53         uint8_t  priority;
54         uint8_t  ident_len;
55 } __attribute__((packed));
56
57 static int logging_fd = -1;
58
59 static void logging_open(void)
60 {
61         struct sockaddr_hci addr;
62         int fd;
63
64         if (logging_fd >= 0)
65                 return;
66
67         fd = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
68         if (fd < 0)
69                 return;
70
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;
75
76         if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
77                 close(fd);
78                 return;
79         }
80
81         logging_fd = fd;
82 }
83
84 static void logging_close(void)
85 {
86         if (logging_fd >= 0) {
87                 close(logging_fd);
88                 logging_fd = -1;
89         }
90 }
91
92 static void logging_log(uint16_t index, int priority,
93                                         const char *format, va_list ap)
94 {
95         struct log_hdr hdr;
96         struct msghdr msg;
97         struct iovec iov[3];
98         uint16_t len;
99         char *str;
100
101         if (vasprintf(&str, format, ap) < 0)
102                 return;
103
104         len = strlen(str) + 1;
105
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;
111
112         iov[0].iov_base = &hdr;
113         iov[0].iov_len = sizeof(hdr);
114
115         iov[1].iov_base = LOG_IDENT;
116         iov[1].iov_len = LOG_IDENT_LEN;
117
118         iov[2].iov_base = str;
119         iov[2].iov_len = len;
120
121         memset(&msg, 0, sizeof(msg));
122         msg.msg_iov = iov;
123         msg.msg_iovlen = 3;
124
125         if (sendmsg(logging_fd, &msg, 0) < 0) {
126                 if (errno != ENODEV) {
127                         close(logging_fd);
128                         logging_fd = -1;
129                 }
130         }
131
132         free(str);
133 }
134
135 void error(const char *format, ...)
136 {
137         va_list ap;
138
139         va_start(ap, format);
140         vsyslog(LOG_ERR, format, ap);
141         va_end(ap);
142
143         if (logging_fd < 0)
144                 return;
145
146         va_start(ap, format);
147         logging_log(HCI_DEV_NONE, LOG_ERR, format, ap);
148         va_end(ap);
149 }
150
151 void warn(const char *format, ...)
152 {
153         va_list ap;
154
155         va_start(ap, format);
156         vsyslog(LOG_WARNING, format, ap);
157         va_end(ap);
158
159         if (logging_fd < 0)
160                 return;
161
162         va_start(ap, format);
163         logging_log(HCI_DEV_NONE, LOG_WARNING, format, ap);
164         va_end(ap);
165 }
166
167 void info(const char *format, ...)
168 {
169         va_list ap;
170
171         va_start(ap, format);
172         vsyslog(LOG_INFO, format, ap);
173         va_end(ap);
174
175         if (logging_fd < 0)
176                 return;
177
178         va_start(ap, format);
179         logging_log(HCI_DEV_NONE, LOG_INFO, format, ap);
180         va_end(ap);
181 }
182
183 void btd_log(uint16_t index, int priority, const char *format, ...)
184 {
185         va_list ap;
186
187         va_start(ap, format);
188         vsyslog(priority, format, ap);
189         va_end(ap);
190
191         if (logging_fd < 0)
192                 return;
193
194         va_start(ap, format);
195         logging_log(index, priority, format, ap);
196         va_end(ap);
197 }
198
199 void btd_error(uint16_t index, const char *format, ...)
200 {
201         va_list ap;
202
203         va_start(ap, format);
204         vsyslog(LOG_ERR, format, ap);
205         va_end(ap);
206
207         if (logging_fd < 0)
208                 return;
209
210         va_start(ap, format);
211         logging_log(index, LOG_ERR, format, ap);
212         va_end(ap);
213 }
214
215 void btd_warn(uint16_t index, const char *format, ...)
216 {
217         va_list ap;
218
219         va_start(ap, format);
220         vsyslog(LOG_WARNING, format, ap);
221         va_end(ap);
222
223         if (logging_fd < 0)
224                 return;
225
226         va_start(ap, format);
227         logging_log(index, LOG_WARNING, format, ap);
228         va_end(ap);
229 }
230
231 void btd_info(uint16_t index, const char *format, ...)
232 {
233         va_list ap;
234
235         va_start(ap, format);
236         vsyslog(LOG_INFO, format, ap);
237         va_end(ap);
238
239         if (logging_fd < 0)
240                 return;
241
242         va_start(ap, format);
243         logging_log(index, LOG_INFO, format, ap);
244         va_end(ap);
245 }
246
247 void btd_debug(uint16_t index, const char *format, ...)
248 {
249         va_list ap;
250
251         va_start(ap, format);
252         vsyslog(LOG_DEBUG, format, ap);
253         va_end(ap);
254
255         if (logging_fd < 0)
256                 return;
257
258         va_start(ap, format);
259         logging_log(index, LOG_DEBUG, format, ap);
260         va_end(ap);
261 }
262
263 extern struct btd_debug_desc __start___debug[];
264 extern struct btd_debug_desc __stop___debug[];
265
266 static char **enabled = NULL;
267
268 static gboolean is_enabled(struct btd_debug_desc *desc)
269 {
270         int i;
271
272         if (enabled == NULL)
273                 return 0;
274
275         for (i = 0; enabled[i] != NULL; i++)
276                 if (desc->file != NULL && g_pattern_match_simple(enabled[i],
277                                                         desc->file) == TRUE)
278                         return 1;
279
280         return 0;
281 }
282
283 void __btd_enable_debug(struct btd_debug_desc *start,
284                                         struct btd_debug_desc *stop)
285 {
286         struct btd_debug_desc *desc;
287
288         if (start == NULL || stop == NULL)
289                 return;
290
291         for (desc = start; desc < stop; desc++) {
292                 if (is_enabled(desc))
293                         desc->flags |= BTD_DEBUG_FLAG_PRINT;
294         }
295 }
296
297 void __btd_toggle_debug(void)
298 {
299         struct btd_debug_desc *desc;
300
301         for (desc = __start___debug; desc < __stop___debug; desc++)
302                 desc->flags |= BTD_DEBUG_FLAG_PRINT;
303 }
304
305 #ifdef __TIZEN_PATCH__
306 void __hci_attach_log_init(void)
307 {
308         int option = LOG_NDELAY | LOG_PID;
309
310         enabled = g_strsplit_set(g_strdup("*"), ":, ", 0);
311
312         __btd_enable_debug(__start___debug, __stop___debug);
313
314         openlog("hciattach", option, LOG_DAEMON);
315
316         syslog(LOG_INFO, "hciattach daemon for debugging");
317 }
318 #endif
319
320
321 void __btd_log_init(const char *debug, int detach)
322 {
323         int option = LOG_NDELAY | LOG_PID;
324
325         if (debug != NULL)
326                 enabled = g_strsplit_set(debug, ":, ", 0);
327
328         __btd_enable_debug(__start___debug, __stop___debug);
329
330         logging_open();
331
332         if (!detach)
333                 option |= LOG_PERROR;
334
335         openlog(LOG_IDENT, option, LOG_DAEMON);
336
337         info("Bluetooth daemon %s", VERSION);
338 }
339
340 void __btd_log_cleanup(void)
341 {
342         closelog();
343
344         logging_close();
345
346         g_strfreev(enabled);
347 }