linux-bluetooth.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Vinicius Costa Gomes <vcgomes@gmail.com>
To: linux-bluetooth@vger.kernel.org
Cc: Vinicius Costa Gomes <vcgomes@gmail.com>
Subject: [RFC BlueZ 2/2] monitor: Add support for printing bluetoothd messages
Date: Fri, 24 Jan 2014 19:41:52 -0200	[thread overview]
Message-ID: <1390599712-1609-3-git-send-email-vcgomes@gmail.com> (raw)
In-Reply-To: <1390599712-1609-1-git-send-email-vcgomes@gmail.com>

In many cases when debugging bluetoothd behaviour, it is useful
to associate bluetooth debug outputs with HCI/MGMT commands and
events.
---
 monitor/journal.c | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 94 insertions(+), 1 deletion(-)

diff --git a/monitor/journal.c b/monitor/journal.c
index ecc7dac..1b04f02 100644
--- a/monitor/journal.c
+++ b/monitor/journal.c
@@ -22,12 +22,105 @@
  */
 
 #include <errno.h>
+#include <stdio.h>
 
 #include <systemd/sd-journal.h>
 
+#include "mainloop.h"
 #include "journal.h"
 
+static sd_journal *journal;
+
+static void journal_destroy(void *user_data)
+{
+	sd_journal *j = user_data;
+	sd_journal_close(j);
+}
+
+static void journal_event(int fd, uint32_t events, void *user_data)
+{
+	sd_journal *j = user_data;
+	const void *d;
+	const char *msg;
+	size_t l;
+	int err, prefix = strlen("MESSAGE=");
+
+	err = sd_journal_process(j);
+	if (err < 0) {
+		printf("Could not process journal: %s (%d)\n",
+				strerror(-err), err);
+		goto failed;
+	}
+
+	err = sd_journal_next(j);
+	if (err == 0) {
+		/* No more events in the journal, let's wait. */
+		return;
+	} else if (err < 0) {
+		printf("Failed to iterate to the next entry: %s (%d)\n",
+				strerror(-err), err);
+		goto failed;
+	}
+
+	err = sd_journal_get_data(j, "MESSAGE", &d, &l);
+	if (err < 0) {
+		printf("Could not get journal messages: %s (%d)",
+				strerror(-err), err);
+		goto failed;
+	}
+
+	msg = d;
+
+	/* TODO: better formatting, perhaps colors. */
+	/* msg is prefixed with 'MESSAGE=', remove it. */
+	printf("# %.*s\n", (int) l - prefix, msg + prefix);
+
+	return;
+
+failed:
+	mainloop_remove_fd(fd);
+}
+
 int journal_monitor(void)
 {
-	return -ENOSYS;
+	int err, events, fd;
+
+	err = sd_journal_open(&journal, SD_JOURNAL_SYSTEM |
+					SD_JOURNAL_RUNTIME_ONLY |
+					SD_JOURNAL_LOCAL_ONLY);
+	if (err < 0)
+		goto failed;
+
+	err = sd_journal_add_match(journal, "_SYSTEMD_UNIT=bluetooth.service", 0);
+	if (err < 0)
+		goto failed;
+
+	err = sd_journal_add_disjunction(journal);
+	if (err < 0)
+		goto failed;
+
+	err = sd_journal_seek_tail(journal);
+	if (err < 0)
+		goto failed;
+
+	fd = sd_journal_get_fd(journal);
+	if (fd < 0) {
+		err = fd;
+		goto failed;
+	}
+
+	events = sd_journal_get_events(journal);
+	if (events < 0) {
+		err = events;
+		goto failed;
+	}
+
+	return mainloop_add_fd(fd, events, journal_event, journal,
+				journal_destroy);
+
+failed:
+	printf("Failed to listen for journal messages: %s (%d)\n",
+			strerror(-err), err);
+	journal_destroy(journal);
+	return err;
 }
-- 
1.8.5.3


  parent reply	other threads:[~2014-01-24 21:41 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-01-24 21:41 [RFC BlueZ 0/2] Print bluetoothd messages in btmon Vinicius Costa Gomes
2014-01-24 21:41 ` [RFC BlueZ 1/2] monitor: Add stubs for monitoring bluetoothd's journal entries Vinicius Costa Gomes
2014-01-24 21:41 ` Vinicius Costa Gomes [this message]
2014-01-29  6:42 ` [RFC BlueZ 0/2] Print bluetoothd messages in btmon Marcel Holtmann
2014-01-29 21:35   ` Vinicius Costa Gomes

Reply instructions:

You may reply publicly 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=1390599712-1609-3-git-send-email-vcgomes@gmail.com \
    --to=vcgomes@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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).