From: Luiz Augusto von Dentz <luiz.von.dentz@xxxxxxxxx> Use bt_log_* to send messages to the logging channel. --- Makefile.tools | 6 +- android/Android.mk | 1 + android/Makefile.am | 1 + src/log.c | 130 +++++--------------------------------------- 4 files changed, 19 insertions(+), 119 deletions(-) diff --git a/Makefile.tools b/Makefile.tools index f81fd0a4c..231a4b09b 100644 --- a/Makefile.tools +++ b/Makefile.tools @@ -223,13 +223,15 @@ tools_mcaptest_SOURCES = tools/mcaptest.c \ btio/btio.h btio/btio.c \ src/log.c src/log.h \ profiles/health/mcap.h profiles/health/mcap.c -tools_mcaptest_LDADD = lib/libbluetooth-internal.la @GLIB_LIBS@ -lrt +tools_mcaptest_LDADD = lib/libbluetooth-internal.la @GLIB_LIBS@ \ + src/libshared-mainloop.la -lrt tools_bneptest_SOURCES = tools/bneptest.c \ btio/btio.h btio/btio.c \ src/log.h src/log.c \ profiles/network/bnep.h profiles/network/bnep.c -tools_bneptest_LDADD = lib/libbluetooth-internal.la @GLIB_LIBS@ +tools_bneptest_LDADD = lib/libbluetooth-internal.la @GLIB_LIBS@ \ + src/libshared-mainloop.la tools_cltest_SOURCES = tools/cltest.c tools_cltest_LDADD = lib/libbluetooth-internal.la src/libshared-mainloop.la diff --git a/android/Android.mk b/android/Android.mk index 76a826b47..8f842e775 100644 --- a/android/Android.mk +++ b/android/Android.mk @@ -228,6 +228,7 @@ include $(BUILD_EXECUTABLE) include $(CLEAR_VARS) LOCAL_SRC_FILES := \ + bluez/src/shared/log.c \ bluez/src/log.c \ bluez/btio/btio.c \ bluez/lib/bluetooth.c \ diff --git a/android/Makefile.am b/android/Makefile.am index 154f8db56..d665d1e14 100644 --- a/android/Makefile.am +++ b/android/Makefile.am @@ -107,6 +107,7 @@ android_avdtptest_SOURCES = android/avdtptest.c \ btio/btio.h btio/btio.c \ src/shared/util.h src/shared/util.c \ src/shared/queue.h src/shared/queue.c \ + src/shared/log.h src/shared/log.c \ android/avdtp.h android/avdtp.c android_avdtptest_CFLAGS = $(AM_CFLAGS) android_avdtptest_LDADD = lib/libbluetooth-internal.la @GLIB_LIBS@ diff --git a/src/log.c b/src/log.c index d2a20de78..ea639cbd6 100644 --- a/src/log.c +++ b/src/log.c @@ -41,95 +41,15 @@ #include "lib/hci.h" #include "src/shared/util.h" +#include "src/shared/log.h" #include "log.h" #define LOG_IDENT "bluetoothd" -#define LOG_IDENT_LEN sizeof(LOG_IDENT) -struct log_hdr { - uint16_t opcode; - uint16_t index; - uint16_t len; - uint8_t priority; - uint8_t ident_len; -} __attribute__((packed)); - -static int logging_fd = -1; - -static void logging_open(void) -{ - struct sockaddr_hci addr; - int fd; - - if (logging_fd >= 0) - return; - - fd = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI); - if (fd < 0) - return; - - memset(&addr, 0, sizeof(addr)); - addr.hci_family = AF_BLUETOOTH; - addr.hci_dev = HCI_DEV_NONE; - addr.hci_channel = HCI_CHANNEL_LOGGING; - - if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { - close(fd); - return; - } - - logging_fd = fd; -} - -static void logging_close(void) -{ - if (logging_fd >= 0) { - close(logging_fd); - logging_fd = -1; - } -} - -static void logging_log(uint16_t index, int priority, +static void monitor_log(uint16_t index, int priority, const char *format, va_list ap) { - struct log_hdr hdr; - struct msghdr msg; - struct iovec iov[3]; - uint16_t len; - char *str; - - if (vasprintf(&str, format, ap) < 0) - return; - - len = strlen(str) + 1; - - hdr.opcode = cpu_to_le16(0x0000); - hdr.index = cpu_to_le16(index); - hdr.len = cpu_to_le16(2 + LOG_IDENT_LEN + len); - hdr.priority = priority; - hdr.ident_len = LOG_IDENT_LEN; - - iov[0].iov_base = &hdr; - iov[0].iov_len = sizeof(hdr); - - iov[1].iov_base = LOG_IDENT; - iov[1].iov_len = LOG_IDENT_LEN; - - iov[2].iov_base = str; - iov[2].iov_len = len; - - memset(&msg, 0, sizeof(msg)); - msg.msg_iov = iov; - msg.msg_iovlen = 3; - - if (sendmsg(logging_fd, &msg, 0) < 0) { - if (errno != ENODEV) { - close(logging_fd); - logging_fd = -1; - } - } - - free(str); + bt_log_vprintf(index, LOG_IDENT, priority, format, ap); } void error(const char *format, ...) @@ -140,11 +60,8 @@ void error(const char *format, ...) vsyslog(LOG_ERR, format, ap); va_end(ap); - if (logging_fd < 0) - return; - va_start(ap, format); - logging_log(HCI_DEV_NONE, LOG_ERR, format, ap); + monitor_log(HCI_DEV_NONE, LOG_ERR, format, ap); va_end(ap); } @@ -156,11 +73,8 @@ void warn(const char *format, ...) vsyslog(LOG_WARNING, format, ap); va_end(ap); - if (logging_fd < 0) - return; - va_start(ap, format); - logging_log(HCI_DEV_NONE, LOG_WARNING, format, ap); + monitor_log(HCI_DEV_NONE, LOG_WARNING, format, ap); va_end(ap); } @@ -172,11 +86,8 @@ void info(const char *format, ...) vsyslog(LOG_INFO, format, ap); va_end(ap); - if (logging_fd < 0) - return; - va_start(ap, format); - logging_log(HCI_DEV_NONE, LOG_INFO, format, ap); + monitor_log(HCI_DEV_NONE, LOG_INFO, format, ap); va_end(ap); } @@ -188,11 +99,8 @@ void btd_log(uint16_t index, int priority, const char *format, ...) vsyslog(priority, format, ap); va_end(ap); - if (logging_fd < 0) - return; - va_start(ap, format); - logging_log(index, priority, format, ap); + monitor_log(index, priority, format, ap); va_end(ap); } @@ -204,11 +112,8 @@ void btd_error(uint16_t index, const char *format, ...) vsyslog(LOG_ERR, format, ap); va_end(ap); - if (logging_fd < 0) - return; - va_start(ap, format); - logging_log(index, LOG_ERR, format, ap); + monitor_log(index, LOG_ERR, format, ap); va_end(ap); } @@ -220,11 +125,8 @@ void btd_warn(uint16_t index, const char *format, ...) vsyslog(LOG_WARNING, format, ap); va_end(ap); - if (logging_fd < 0) - return; - va_start(ap, format); - logging_log(index, LOG_WARNING, format, ap); + monitor_log(index, LOG_WARNING, format, ap); va_end(ap); } @@ -236,11 +138,8 @@ void btd_info(uint16_t index, const char *format, ...) vsyslog(LOG_INFO, format, ap); va_end(ap); - if (logging_fd < 0) - return; - va_start(ap, format); - logging_log(index, LOG_INFO, format, ap); + monitor_log(index, LOG_INFO, format, ap); va_end(ap); } @@ -252,11 +151,8 @@ void btd_debug(uint16_t index, const char *format, ...) vsyslog(LOG_DEBUG, format, ap); va_end(ap); - if (logging_fd < 0) - return; - va_start(ap, format); - logging_log(index, LOG_DEBUG, format, ap); + monitor_log(index, LOG_DEBUG, format, ap); va_end(ap); } @@ -311,7 +207,7 @@ void __btd_log_init(const char *debug, int detach) __btd_enable_debug(__start___debug, __stop___debug); - logging_open(); + bt_log_open(); if (!detach) option |= LOG_PERROR; @@ -325,7 +221,7 @@ void __btd_log_cleanup(void) { closelog(); - logging_close(); + bt_log_close(); g_strfreev(enabled); } -- 2.17.2