From 675fa2cd8590227e114b614fe2ff781ba1eff4fc Mon Sep 17 00:00:00 2001 From: Victor Pushkarev Date: Fri, 1 Nov 2024 19:58:45 +0300 Subject: [PATCH] bt-agent: add option to redirect stdout and stderr to syslog Add handlers for g_print() and g_printerr() methods to redirect output to syslog. Add corresponding option. Signed-off-by: Victor Pushkarev --- src/bt-agent.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/bt-agent.c b/src/bt-agent.c index 9e4d0c2..e5c5efa 100644 --- a/src/bt-agent.c +++ b/src/bt-agent.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include @@ -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", ""}, {"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} }; @@ -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 && @@ -294,5 +311,9 @@ int main(int argc, char *argv[]) dbus_disconnect(); + if (syslog_arg) { + closelog(); + } + exit(EXIT_SUCCESS); }