Skip to content

Commit

Permalink
bt-agent: add option to redirect stdout and stderr to syslog
Browse files Browse the repository at this point in the history
Add handlers for g_print() and g_printerr() methods to
redirect output to syslog.

Add corresponding option.

Signed-off-by: Victor Pushkarev <[email protected]>
  • Loading branch information
corviv committed Nov 6, 2024
1 parent f653217 commit 675fa2c
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/bt-agent.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <signal.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>

#include <glib.h>
#include <glib/gstdio.h>
Expand Down Expand Up @@ -151,13 +152,23 @@ term_signal_handler(gpointer data)
return G_SOURCE_REMOVE;
}

static void print_handler(const gchar *msg) {
syslog(LOG_INFO, "%s", msg);
}

static void printerr_handler(const gchar *msg) {
syslog(LOG_ERR, "%s", msg);
}

static gchar *capability_arg = NULL;
static gboolean daemon_arg = FALSE;
static gboolean syslog_arg = FALSE;

static GOptionEntry entries[] = {
{"capability", 'c', 0, G_OPTION_ARG_STRING, &capability_arg, "Agent capability", "<capability>"},
{"pin", 'p', 0, G_OPTION_ARG_STRING, &pin_arg, "Path to the PIN's file"},
{"daemon", 'd', 0, G_OPTION_ARG_NONE, &daemon_arg, "Run in background (as daemon)"},
{"syslog", 's', 0, G_OPTION_ARG_NONE, &syslog_arg, "Redirect output to syslog"},
{NULL}
};

Expand Down Expand Up @@ -192,6 +203,12 @@ int main(int argc, char *argv[])
exit(EXIT_FAILURE);
}

if (syslog_arg) {
openlog("bt-agent", LOG_PID | LOG_CONS, LOG_DAEMON);
g_set_print_handler(print_handler);
g_set_printerr_handler(printerr_handler);
}

if (capability_arg) {
if (
g_strcmp0(capability_arg, "DisplayOnly") != 0 &&
Expand Down Expand Up @@ -294,5 +311,9 @@ int main(int argc, char *argv[])

dbus_disconnect();

if (syslog_arg) {
closelog();
}

exit(EXIT_SUCCESS);
}

0 comments on commit 675fa2c

Please sign in to comment.