Linux-Bluetooth Archive on lore.kernel.org
 help / Atom feed
From: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
To: linux-bluetooth@vger.kernel.org
Subject: [PATCH v2 4/4] log: Use shared log code
Date: Tue, 20 Nov 2018 15:58:21 +0200
Message-ID: <20181120135821.18082-4-luiz.dentz@gmail.com> (raw)
In-Reply-To: <20181120135821.18082-1-luiz.dentz@gmail.com>

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

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


  parent reply index

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-11-20 13:58 [PATCH v2 1/4] shared/log: Add common code to interface with logging channel Luiz Augusto von Dentz
2018-11-20 13:58 ` [PATCH v2 2/4] shared/tester: Make use of shared log Luiz Augusto von Dentz
2018-11-20 13:58 ` [PATCH v2 3/4] shared/shell: Add option to print to monitor Luiz Augusto von Dentz
2018-11-20 13:58 ` Luiz Augusto von Dentz [this message]
2018-11-21  9:16 ` [PATCH v2 1/4] shared/log: Add common code to interface with logging channel Luiz Augusto von Dentz

Reply instructions:

You may reply publically to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20181120135821.18082-4-luiz.dentz@gmail.com \
    --to=luiz.dentz@gmail.com \
    --cc=linux-bluetooth@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Linux-Bluetooth Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-bluetooth/0 linux-bluetooth/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-bluetooth linux-bluetooth/ https://lore.kernel.org/linux-bluetooth \
		linux-bluetooth@vger.kernel.org linux-bluetooth@archiver.kernel.org
	public-inbox-index linux-bluetooth


Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-bluetooth


AGPL code for this site: git clone https://public-inbox.org/ public-inbox