3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2014 Intel Corporation. All rights reserved.
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/types.h>
37 #include <sys/socket.h>
42 #define LOG_TAG "bluetoothd"
49 #define LOG_ID_SYSTEM 3
53 uint16_t pid; /* Android logd expects only 2 bytes for PID */
56 } __attribute__ ((packed));
58 static int log_fd = -1;
59 static bool legacy_log = false;
61 static void android_log(unsigned char level, const char *fmt, va_list ap)
63 struct logd_header header;
72 /* no need to call getpid all the time since we don't fork */
76 if (vasprintf(&msg, fmt, ap) < 0)
82 clock_gettime(CLOCK_REALTIME, &ts);
84 header.id = LOG_ID_SYSTEM;
86 header.sec = ts.tv_sec;
87 header.nsec = ts.tv_nsec;
89 vec[0].iov_base = &header;
90 vec[0].iov_len = sizeof(header);
95 vec[cnt + 0].iov_base = &level;
96 vec[cnt + 0].iov_len = sizeof(level);
97 vec[cnt + 1].iov_base = LOG_TAG;
98 vec[cnt + 1].iov_len = sizeof(LOG_TAG);
99 vec[cnt + 2].iov_base = msg;
100 vec[cnt + 2].iov_len = strlen(msg) + 1;
104 writev(log_fd, vec, cnt);
109 void info(const char *format, ...)
113 va_start(ap, format);
115 android_log(LOG_INFO, format, ap);
120 void warn(const char *format, ...)
124 va_start(ap, format);
126 android_log(LOG_WARN, format, ap);
131 void error(const char *format, ...)
135 va_start(ap, format);
137 android_log(LOG_ERR, format, ap);
142 void btd_debug(uint16_t index, const char *format, ...)
146 va_start(ap, format);
148 android_log(LOG_DEBUG, format, ap);
153 static bool init_legacy_log(void)
155 log_fd = open("/dev/log/system", O_WRONLY);
164 static bool init_logd(void)
166 struct sockaddr_un addr;
168 log_fd = socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);
172 if (fcntl(log_fd, F_SETFL, O_NONBLOCK) < 0)
175 memset(&addr, 0, sizeof(addr));
176 addr.sun_family = AF_UNIX;
177 strcpy(addr.sun_path, "/dev/socket/logdw");
179 if (connect(log_fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
191 extern struct btd_debug_desc __start___debug[];
192 extern struct btd_debug_desc __stop___debug[];
194 void __btd_log_init(const char *debug, int detach)
196 if (!init_logd() && !init_legacy_log())
200 struct btd_debug_desc *desc;
202 for (desc = __start___debug; desc < __stop___debug; desc++)
203 desc->flags |= BTD_DEBUG_FLAG_PRINT;
206 info("Bluetooth daemon %s", VERSION);
209 void __btd_log_cleanup(void)