Add support for dynamic debug feature

This commit is contained in:
Marcel Holtmann 2010-01-26 10:06:53 +01:00
parent b334372ef8
commit e4850f2eeb
4 changed files with 66 additions and 27 deletions

View File

@ -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
}

View File

@ -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);
}

View File

@ -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);

View File

@ -23,6 +23,8 @@
#define OFONO_API_SUBJECT_TO_CHANGE
#include <ofono/types.h>
void __ofono_exit();
int __ofono_manager_init();
@ -33,11 +35,9 @@ void __ofono_modem_shutdown();
#include <ofono/log.h>
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 <ofono/dbus.h>
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 <ofono/types.h>
struct ofono_watchlist_item {
unsigned int id;
void *notify;