From e4850f2eeb0e4e137b2f9f7f3a63fdf4661fc498 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Tue, 26 Jan 2010 10:06:53 +0100 Subject: [PATCH] Add support for dynamic debug feature --- include/log.h | 18 ++++++++++++++++- src/log.c | 55 +++++++++++++++++++++++++++++++++++++++------------ src/main.c | 12 ++++------- src/ofono.h | 8 +++----- 4 files changed, 66 insertions(+), 27 deletions(-) diff --git a/include/log.h b/include/log.h index 67014ad3..d1fef7b6 100644 --- a/include/log.h +++ b/include/log.h @@ -41,6 +41,14 @@ extern void ofono_error(const char *format, ...) extern void ofono_debug(const char *format, ...) __attribute__((format(printf, 1, 2))); +struct ofono_debug_desc { + const char *name; + const char *file; +#define OFONO_DEBUG_FLAG_DEFAULT (0) +#define OFONO_DEBUG_FLAG_PRINT (1 << 0) + unsigned int flags; +} __attribute__((aligned(8))); + /** * DBG: * @fmt: format string @@ -49,7 +57,15 @@ extern void ofono_debug(const char *format, ...) * Simple macro around ofono_debug() which also include the function * name it is called in. */ -#define DBG(fmt, arg...) ofono_debug("%s:%s() " fmt, __FILE__, __FUNCTION__ , ## arg) +#define DBG(fmt, arg...) do { \ + static struct ofono_debug_desc __ofono_debug_desc \ + __attribute__((used, section("__debug"), aligned(8))) = { \ + .file = __FILE__, .flags = OFONO_DEBUG_FLAG_DEFAULT, \ + }; \ + if (__ofono_debug_desc.flags & OFONO_DEBUG_FLAG_PRINT) \ + ofono_debug("%s:%s() " fmt, \ + __FILE__, __FUNCTION__ , ## arg); \ +} while (0) #ifdef __cplusplus } diff --git a/src/log.c b/src/log.c index fd3c0de5..25ccd12f 100644 --- a/src/log.c +++ b/src/log.c @@ -28,8 +28,6 @@ #include "ofono.h" -static volatile gboolean debug_enabled = FALSE; - /** * ofono_info: * @format: format string @@ -98,9 +96,6 @@ void ofono_debug(const char *format, ...) { va_list ap; - if (debug_enabled == FALSE) - return; - va_start(ap, format); vsyslog(LOG_DEBUG, format, ap); @@ -108,17 +103,51 @@ void ofono_debug(const char *format, ...) va_end(ap); } -void __ofono_toggle_debug(void) +extern struct ofono_debug_desc __start___debug[]; +extern struct ofono_debug_desc __stop___debug[]; + +static gchar **enabled = NULL; + +static ofono_bool_t is_enabled(struct ofono_debug_desc *desc) { - if (debug_enabled == TRUE) - debug_enabled = FALSE; - else - debug_enabled = TRUE; + int i; + + if (enabled == NULL) + return FALSE; + + for (i = 0; enabled[i] != NULL; i++) { + if (desc->name != NULL && g_pattern_match_simple(enabled[i], + desc->name) == TRUE) + return TRUE; + if (desc->file != NULL && g_pattern_match_simple(enabled[i], + desc->file) == TRUE) + return TRUE; + } + + return FALSE; } -int __ofono_log_init(gboolean detach, gboolean debug) +int __ofono_log_init(const char *debug, ofono_bool_t detach) { int option = LOG_NDELAY | LOG_PID; + struct ofono_debug_desc *desc; + const char *name = NULL, *file = NULL; + + if (debug != NULL) + enabled = g_strsplit_set(debug, ":, ", 0); + + for (desc = __start___debug; desc < __stop___debug; desc++) { + if (file != NULL || name != NULL) { + if (g_strcmp0(desc->file, file) == 0) { + if (desc->name == NULL) + desc->name = name; + } else + file = NULL; + } + + if (is_enabled(desc) == TRUE) + desc->flags |= OFONO_DEBUG_FLAG_PRINT; + } if (detach == FALSE) option |= LOG_PERROR; @@ -127,8 +156,6 @@ int __ofono_log_init(gboolean detach, gboolean debug) syslog(LOG_INFO, "oFono version %s", VERSION); - debug_enabled = debug; - return 0; } @@ -137,4 +164,6 @@ void __ofono_log_cleanup(void) syslog(LOG_INFO, "Exit"); closelog(); + + g_strfreev(enabled); } diff --git a/src/main.c b/src/main.c index 3feea935..0fce3bdc 100644 --- a/src/main.c +++ b/src/main.c @@ -74,9 +74,6 @@ static gboolean signal_cb(GIOChannel *channel, GIOCondition cond, gpointer data) terminated++; break; - case SIGUSR2: - __ofono_toggle_debug(); - break; case SIGPIPE: break; default: @@ -93,13 +90,13 @@ static void system_bus_disconnected(DBusConnection *conn, void *user_data) g_main_loop_quit(event_loop); } -static gboolean option_debug = FALSE; +static gchar *option_debug = NULL; static gboolean option_detach = TRUE; static gboolean option_version = FALSE; static GOptionEntry options[] = { - { "debug", 'd', 0, G_OPTION_ARG_NONE, &option_debug, - "Enable debug information output" }, + { "debug", 'd', 0, G_OPTION_ARG_STRING, &option_debug, + "Specify debug options to enable", "DEBUG" }, { "nodetach", 'n', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &option_detach, "Don't run as daemon in background" }, @@ -122,7 +119,6 @@ int main(int argc, char **argv) sigemptyset(&mask); sigaddset(&mask, SIGTERM); sigaddset(&mask, SIGINT); - sigaddset(&mask, SIGUSR2); sigaddset(&mask, SIGPIPE); if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) { @@ -185,7 +181,7 @@ int main(int argc, char **argv) } #endif - __ofono_log_init(option_detach, option_debug); + __ofono_log_init(option_debug, option_detach); dbus_error_init(&error); diff --git a/src/ofono.h b/src/ofono.h index 1f288b03..379f413d 100644 --- a/src/ofono.h +++ b/src/ofono.h @@ -23,6 +23,8 @@ #define OFONO_API_SUBJECT_TO_CHANGE +#include + void __ofono_exit(); int __ofono_manager_init(); @@ -33,11 +35,9 @@ void __ofono_modem_shutdown(); #include -int __ofono_log_init(gboolean detach, gboolean debug); +int __ofono_log_init(const char *debug, ofono_bool_t detach); void __ofono_log_cleanup(void); -void __ofono_toggle_debug(void); - #include int __ofono_dbus_init(DBusConnection *conn); @@ -61,8 +61,6 @@ void __ofono_dbus_pending_reply(DBusMessage **msg, DBusMessage *reply); gboolean __ofono_dbus_valid_object_path(const char *path); -#include - struct ofono_watchlist_item { unsigned int id; void *notify;