From 7480d4761fd3ac2f6f05fd8fe17ce2ae2b16d075 Mon Sep 17 00:00:00 2001 From: Vinicius Costa Gomes Date: Fri, 15 Jan 2010 20:29:50 -0300 Subject: [PATCH] obexd: Add support for using the logging infrastructure from obexd --- obexd/client/main.c | 56 ++++++++++++++++++++++++++++++++++++------ obexd/client/pbap.c | 11 +++++---- obexd/client/session.c | 9 ++++--- 3 files changed, 59 insertions(+), 17 deletions(-) diff --git a/obexd/client/main.c b/obexd/client/main.c index 6387bff13..b15729b63 100644 --- a/obexd/client/main.c +++ b/obexd/client/main.c @@ -29,10 +29,12 @@ #include #include #include +#include #include #include +#include "logging.h" #include "session.h" #define CLIENT_SERVICE "org.openobex.client" @@ -429,6 +431,17 @@ static GDBusMethodTable client_methods[] = { static GMainLoop *event_loop = NULL; +static gboolean option_debug = FALSE; +static gboolean option_stderr = FALSE; + +static GOptionEntry options[] = { + { "debug", 'd', 0, G_OPTION_ARG_NONE, &option_debug, + "Enable debug information output" }, + { "stderr", 's', 0, G_OPTION_ARG_NONE, &option_stderr, + "Write log information to stderr" }, + { NULL }, +}; + static void sig_term(int sig) { g_main_loop_quit(event_loop); @@ -436,32 +449,59 @@ static void sig_term(int sig) int main(int argc, char *argv[]) { + GOptionContext *context; struct sigaction sa; DBusConnection *conn; - DBusError err; + DBusError derr; + GError *gerr = NULL; + int log_option; + + context = g_option_context_new(NULL); + g_option_context_add_main_entries(context, options, NULL); + + g_option_context_parse(context, &argc, &argv, &gerr); + if (gerr != NULL) { + g_printerr("%s\n", gerr->message); + g_error_free(gerr); + exit(EXIT_FAILURE); + } - dbus_error_init(&err); + g_option_context_free(context); - conn = g_dbus_setup_bus(DBUS_BUS_SESSION, CLIENT_SERVICE, &err); + dbus_error_init(&derr); + + conn = g_dbus_setup_bus(DBUS_BUS_SESSION, CLIENT_SERVICE, &derr); if (conn == NULL) { - if (dbus_error_is_set(&err) == TRUE) { - fprintf(stderr, "%s\n", err.message); - dbus_error_free(&err); + if (dbus_error_is_set(&derr) == TRUE) { + g_printerr("%s", derr.message); + dbus_error_free(&derr); } else - fprintf(stderr, "Can't register with session bus\n"); + g_printerr("Can't register with session bus\n"); exit(EXIT_FAILURE); } if (g_dbus_register_interface(conn, CLIENT_PATH, CLIENT_INTERFACE, client_methods, NULL, NULL, NULL, NULL) == FALSE) { - fprintf(stderr, "Can't register client interface\n"); + g_printerr("Can't register client interface\n"); dbus_connection_unref(conn); exit(EXIT_FAILURE); } event_loop = g_main_loop_new(NULL, FALSE); + log_option = LOG_NDELAY | LOG_PID; + + if (option_stderr == TRUE) + log_option |= LOG_PERROR; + + openlog("obex-client", log_option, LOG_DAEMON); + + if (option_debug == TRUE) { + info("Enabling debug information"); + enable_debug(); + } + memset(&sa, 0, sizeof(sa)); sa.sa_handler = sig_term; sigaction(SIGINT, &sa, NULL); diff --git a/obexd/client/pbap.c b/obexd/client/pbap.c index 97a63a84a..06955b471 100644 --- a/obexd/client/pbap.c +++ b/obexd/client/pbap.c @@ -30,6 +30,7 @@ #include #include +#include "logging.h" #include "session.h" #include "pbap.h" @@ -298,8 +299,8 @@ static void read_return_apparam(struct session_data *session, struct apparam_hdr *hdr = (struct apparam_hdr *) buf; if (hdr->len > size - APPARAM_HDR_SIZE) { - fprintf(stderr, "Unexpected PBAP pullphonebook app" - " length, tag %d, len %d\n", + error("Unexpected PBAP pullphonebook app" + " length, tag %d, len %d", hdr->tag, hdr->len); return; } @@ -317,8 +318,8 @@ static void read_return_apparam(struct session_data *session, *new_missed_calls = hdr->val[0]; break; default: - fprintf(stderr, "Unexpected PBAP pullphonebook app" - " parameter, tag %d, len %d\n", + error("Unexpected PBAP pullphonebook app" + " parameter, tag %d, len %d", hdr->tag, hdr->len); } @@ -443,7 +444,7 @@ static DBusMessage *pull_phonebook(struct session_data *session, func = phonebook_size_callback; break; default: - fprintf(stderr, "Unexpected type : 0x%2x\n", type); + error("Unexpected type : 0x%2x", type); return NULL; } diff --git a/obexd/client/session.c b/obexd/client/session.c index 86ae746fd..ed0b94b7a 100644 --- a/obexd/client/session.c +++ b/obexd/client/session.c @@ -39,6 +39,7 @@ #include #include +#include "logging.h" #include "pbap.h" #include "sync.h" #include "session.h" @@ -1078,7 +1079,7 @@ static void get_xfer_listing_progress(GwObexXfer *xfer, } if (err) { - fprintf(stderr, "gw_obex_xfer_read(): %s\n", + error("gw_obex_xfer_read(): %s", OBEX_ResponseToString(err)); goto complete; } @@ -1154,7 +1155,7 @@ static void get_xfer_progress(GwObexXfer *xfer, gpointer user_data) } if (ret == FALSE) { - fprintf(stderr, "gw_obex_xfer_read(): %s\n", + error("gw_obex_xfer_read(): %s", OBEX_ResponseToString(err)); goto complete; } @@ -1281,7 +1282,7 @@ static void agent_request_reply(DBusPendingCall *call, gpointer user_data) dbus_error_init(&derr); if (dbus_set_error_from_message(&derr, reply)) { - fprintf(stderr, "Replied with an error: %s, %s\n", + error("Replied with an error: %s, %s", derr.name, derr.message); dbus_error_free(&derr); goto fail; @@ -1430,7 +1431,7 @@ int session_get(struct session_data *session, const char *type, fd = open(targetname, O_WRONLY | O_CREAT, 0600); if (fd < 0) { err = errno; - fprintf(stderr, "open(): %s(%d)\n", strerror(err), err); + error("open(): %s(%d)", strerror(err), err); return -err; } } -- 2.47.3