diff --git a/obexd/client/main.c b/obexd/client/main.c
index 89b6c65..3bde09e 100644
--- a/obexd/client/main.c
+++ b/obexd/client/main.c
#include <glib.h>
#include <gdbus.h>
-#include "logging.h"
+#include "log.h"
#include "transfer.h"
#include "session.h"
static GMainLoop *event_loop = NULL;
-static gboolean option_debug = FALSE;
+static char *option_debug = NULL;
static gboolean option_stderr = FALSE;
+static gboolean parse_debug(const char *key, const char *value,
+ gpointer user_data, GError **error)
+{
+ if (value)
+ option_debug = g_strdup(value);
+ else
+ option_debug = g_strdup("*");
+
+ return TRUE;
+}
+
static GOptionEntry options[] = {
- { "debug", 'd', 0, G_OPTION_ARG_NONE, &option_debug,
- "Enable debug information output" },
+ { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
+ G_OPTION_ARG_CALLBACK, parse_debug,
+ "Enable debug information output", "DEBUG" },
{ "stderr", 's', 0, G_OPTION_ARG_NONE, &option_stderr,
"Write log information to stderr" },
{ NULL },
DBusConnection *conn;
DBusError derr;
GError *gerr = NULL;
- int log_option;
+ int log_option = 0;
context = g_option_context_new(NULL);
g_option_context_add_main_entries(context, options, NULL);
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);
+ event_loop = g_main_loop_new(NULL, FALSE);
- if (option_debug == TRUE) {
- info("Enabling debug information");
- enable_debug();
- }
+ log_init("obex-client", option_debug, log_option);
+
+ DBG("Entering main loop");
memset(&sa, 0, sizeof(sa));
sa.sa_handler = sig_term;
diff --git a/obexd/client/pbap.c b/obexd/client/pbap.c
index cb3def4..8e4e025 100644
--- a/obexd/client/pbap.c
+++ b/obexd/client/pbap.c
#include <glib.h>
#include <gdbus.h>
-#include "logging.h"
+#include "log.h"
#include "transfer.h"
#include "session.h"
#include "pbap.h"
diff --git a/obexd/client/session.c b/obexd/client/session.c
index 05eb6a6..2db482b 100644
--- a/obexd/client/session.c
+++ b/obexd/client/session.c
#include <bluetooth/sdp.h>
#include <bluetooth/sdp_lib.h>
-#include "logging.h"
+#include "log.h"
#include "pbap.h"
#include "sync.h"
#include "transfer.h"
diff --git a/obexd/client/transfer.c b/obexd/client/transfer.c
index 6af8c52..dc585dc 100644
--- a/obexd/client/transfer.c
+++ b/obexd/client/transfer.c
#include <gdbus.h>
#include <gw-obex.h>
-#include "logging.h"
+#include "log.h"
#include "transfer.h"
#include "session.h"
diff --git a/obexd/plugins/bluetooth.c b/obexd/plugins/bluetooth.c
index 8134e6b..ae4ca8d 100644
--- a/obexd/plugins/bluetooth.c
+++ b/obexd/plugins/bluetooth.c
#include "obex.h"
#include "transport.h"
#include "service.h"
-#include "logging.h"
+#include "log.h"
#include "btio.h"
#define BT_RX_MTU 32767
diff --git a/obexd/plugins/filesystem.c b/obexd/plugins/filesystem.c
index 2844329..adbf6d6 100644
--- a/obexd/plugins/filesystem.c
+++ b/obexd/plugins/filesystem.c
#include <openobex/obex_const.h>
#include "plugin.h"
-#include "logging.h"
+#include "log.h"
#include "obex.h"
#include "mimetype.h"
#include "service.h"
diff --git a/obexd/plugins/ftp.c b/obexd/plugins/ftp.c
index c2dac96..24553e5 100644
--- a/obexd/plugins/ftp.c
+++ b/obexd/plugins/ftp.c
#include <openobex/obex_const.h>
#include "plugin.h"
-#include "logging.h"
+#include "log.h"
#include "obex.h"
#include "dbus.h"
#include "mimetype.h"
diff --git a/obexd/plugins/nokia-backup.c b/obexd/plugins/nokia-backup.c
index e6504f5..2b590b6 100644
--- a/obexd/plugins/nokia-backup.c
+++ b/obexd/plugins/nokia-backup.c
#include <openobex/obex_const.h>
#include "plugin.h"
-#include "logging.h"
+#include "log.h"
#include "obex.h"
#include "mimetype.h"
#include "service.h"
diff --git a/obexd/plugins/opp.c b/obexd/plugins/opp.c
index 976db1f..cd46464 100644
--- a/obexd/plugins/opp.c
+++ b/obexd/plugins/opp.c
#include "plugin.h"
#include "obex.h"
#include "service.h"
-#include "logging.h"
+#include "log.h"
#include "dbus.h"
#define VCARD_TYPE "text/x-vcard"
diff --git a/obexd/plugins/pbap.c b/obexd/plugins/pbap.c
index 96b6452..32f5ef3 100644
--- a/obexd/plugins/pbap.c
+++ b/obexd/plugins/pbap.c
#include <openobex/obex_const.h>
#include "plugin.h"
-#include "logging.h"
+#include "log.h"
#include "obex.h"
#include "service.h"
#include "phonebook.h"
diff --git a/obexd/plugins/phonebook-dummy.c b/obexd/plugins/phonebook-dummy.c
index 6ad0266..6f3a7d4 100644
--- a/obexd/plugins/phonebook-dummy.c
+++ b/obexd/plugins/phonebook-dummy.c
#include <libical/vobject.h>
#include <libical/vcc.h>
-#include "logging.h"
+#include "log.h"
#include "phonebook.h"
typedef void (*vcard_func_t) (const char *file, VObject *vo, void *user_data);
diff --git a/obexd/plugins/phonebook-ebook.c b/obexd/plugins/phonebook-ebook.c
index 0ca1cba..2d5bec3 100644
--- a/obexd/plugins/phonebook-ebook.c
+++ b/obexd/plugins/phonebook-ebook.c
#include <libebook/e-book.h>
-#include "logging.h"
+#include "log.h"
#include "obex.h"
#include "service.h"
#include "phonebook.h"
diff --git a/obexd/plugins/phonebook-tracker.c b/obexd/plugins/phonebook-tracker.c
index 243be51..2c73747 100644
--- a/obexd/plugins/phonebook-tracker.c
+++ b/obexd/plugins/phonebook-tracker.c
#include <openobex/obex.h>
#include <openobex/obex_const.h>
-#include "logging.h"
+#include "log.h"
#include "obex.h"
#include "service.h"
#include "mimetype.h"
diff --git a/obexd/plugins/syncevolution.c b/obexd/plugins/syncevolution.c
index 87deb17..bdc4446 100644
--- a/obexd/plugins/syncevolution.c
+++ b/obexd/plugins/syncevolution.c
#include "obex.h"
#include "service.h"
#include "mimetype.h"
-#include "logging.h"
+#include "log.h"
#include "dbus.h"
#include "btio.h"
#include "obexd.h"
diff --git a/obexd/plugins/usb.c b/obexd/plugins/usb.c
index b3fe035..779532d 100644
--- a/obexd/plugins/usb.c
+++ b/obexd/plugins/usb.c
#include "obex.h"
#include "transport.h"
#include "service.h"
-#include "logging.h"
+#include "log.h"
static GIOChannel *usb_io = NULL;
static guint usb_reconnecting = 0;
diff --git a/obexd/src/logging.c b/obexd/src/log.c
similarity index 71%
rename from obexd/src/logging.c
rename to obexd/src/log.c
index f42770e..fe045a4 100644
--- a/obexd/src/logging.c
+++ b/obexd/src/log.c
#include <openobex/obex.h>
-#include "logging.h"
+#include <glib.h>
-static volatile int debug_enabled = 0;
-
-static inline void vinfo(const char *format, va_list ap)
-{
- vsyslog(LOG_INFO, format, ap);
-}
+#include "log.h"
void info(const char *format, ...)
{
va_start(ap, format);
- vinfo(format, ap);
+ vsyslog(LOG_INFO, format, ap);
va_end(ap);
}
{
va_list ap;
- if (!debug_enabled)
- return;
-
va_start(ap, format);
vsyslog(LOG_DEBUG, format, ap);
va_end(ap);
}
-void toggle_debug(void)
-{
- debug_enabled = (debug_enabled + 1) % 2;
-}
-
-void enable_debug(void)
-{
- debug_enabled = 1;
-}
-
-void disable_debug(void)
-{
- debug_enabled = 0;
-}
-
-void start_logging(const char *ident, const char *message, ...)
-{
- va_list ap;
-
- openlog(ident, LOG_PID | LOG_NDELAY | LOG_PERROR, LOG_DAEMON);
-
- va_start(ap, message);
-
- vinfo(message, ap);
-
- va_end(ap);
-}
-
-void stop_logging(void)
-{
- closelog();
-}
-
static struct {
int evt;
const char *name;
{ OBEX_RSP_CONTINUE, "CONTINUE" },
{ OBEX_RSP_SWITCH_PRO, "SWITCH_PRO" },
{ OBEX_RSP_SUCCESS, "SUCCESS" },
- { OBEX_RSP_CREATED, "CREATED" },
+ { OBEX_RSP_CREATED, "CREATED" },
{ OBEX_RSP_ACCEPTED, "ACCEPTED" },
{ OBEX_RSP_NON_AUTHORITATIVE, "NON_AUTHORITATIVE" },
{ OBEX_RSP_NO_CONTENT, "NO_CONTENT" },
const char *evtstr = NULL, *cmdstr = NULL, *rspstr = NULL;
int i;
- if (!debug_enabled)
- return;
-
for (i = 0; obex_event[i].evt != 0xFF; i++) {
if (obex_event[i].evt != evt)
continue;
debug("%s(0x%x), %s(0x%x), %s(0x%x)", evtstr, evt, cmdstr, cmd,
rspstr, rsp);
}
+
+extern struct log_debug_desc __start___debug[];
+extern struct log_debug_desc __stop___debug[];
+
+static gchar **enabled = NULL;
+
+int debug_enabled = FALSE;
+
+static gboolean is_enabled(struct log_debug_desc *desc)
+{
+ int i;
+
+ if (enabled == NULL)
+ return 0;
+
+ for (i = 0; enabled[i] != NULL; i++) {
+ if (desc->name != NULL && g_pattern_match_simple(enabled[i],
+ desc->name) == TRUE)
+ return 1;
+ if (desc->file != NULL && g_pattern_match_simple(enabled[i],
+ desc->file) == TRUE)
+ return 1;
+ }
+
+ return 0;
+}
+
+void log_enable_debug()
+{
+ struct log_debug_desc *desc;
+
+ for (desc = __start___debug; desc < __stop___debug; desc++)
+ desc->flags |= LOG_DEBUG_FLAG_PRINT;
+}
+
+void log_init(const char *ident, const char *debug, int log_option)
+{
+ int option = log_option | LOG_NDELAY | LOG_PID;
+ struct log_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))
+ desc->flags |= LOG_DEBUG_FLAG_PRINT;
+ }
+
+ openlog(ident, option, LOG_DAEMON);
+
+ syslog(LOG_INFO, "%s version %s", ident, VERSION);
+}
+
+void log_cleanup(void)
+{
+ closelog();
+
+ g_strfreev(enabled);
+}
diff --git a/obexd/src/logging.h b/obexd/src/log.h
similarity index 61%
rename from obexd/src/logging.h
rename to obexd/src/log.h
index d876d0f..897924d 100644
--- a/obexd/src/logging.h
+++ b/obexd/src/log.h
void error(const char *format, ...) __attribute__((format(printf, 1, 2)));
void debug(const char *format, ...) __attribute__((format(printf, 1, 2)));
void obex_debug(int evt, int cmd, int rsp);
-void toggle_debug(void);
-void enable_debug(void);
-void disable_debug(void);
-void start_logging(const char *ident, const char *message, ...);
-void stop_logging(void);
-#define DBG(fmt, arg...) debug("%s: " fmt "\n" , __FUNCTION__ , ## arg)
-//#define DBG(fmt, arg...)
+void log_init(const char *ident, const char *debug, int log_option);
+void log_cleanup(void);
+void log_enable_debug(void);
+
+struct log_debug_desc {
+ const char *name;
+ const char *file;
+#define LOG_DEBUG_FLAG_DEFAULT (0)
+#define LOG_DEBUG_FLAG_PRINT (1 << 0)
+ unsigned int flags;
+} __attribute__((aligned(8)));
+
+/**
+ * DBG:
+ * @fmt: format string
+ * @arg...: list of arguments
+ *
+ * Simple macro around debug() which also include the function
+ * name it is called in.
+ */
+#define DBG(fmt, arg...) do { \
+ static struct log_debug_desc __log_debug_desc \
+ __attribute__((used, section("__debug"), aligned(8))) = { \
+ .file = __FILE__, .flags = LOG_DEBUG_FLAG_DEFAULT, \
+ }; \
+ if (__log_debug_desc.flags & LOG_DEBUG_FLAG_PRINT) \
+ debug("%s:%s() " fmt, __FILE__, __FUNCTION__ , ## arg); \
+} while (0)
diff --git a/obexd/src/main.c b/obexd/src/main.c
index b60fcd2..d3ca3b1 100644
--- a/obexd/src/main.c
+++ b/obexd/src/main.c
#include <openobex/obex.h>
#include <openobex/obex_const.h>
-#include "logging.h"
+#include "log.h"
#include "obexd.h"
#include "obex.h"
#include "obex-priv.h"
static void sig_debug(int sig)
{
- toggle_debug();
+ log_enable_debug();
}
static gboolean option_detach = TRUE;
-static gboolean option_debug = FALSE;
+static char *option_debug = NULL;
static char *option_root = NULL;
static char *option_root_setup = NULL;
static gboolean option_symlinks = FALSE;
static gboolean option_syncevolution = FALSE;
+static gboolean parse_debug(const char *key, const char *value,
+ gpointer user_data, GError **error)
+{
+ if (value)
+ option_debug = g_strdup(value);
+ else
+ option_debug = g_strdup("*");
+
+ return TRUE;
+}
+
static GOptionEntry options[] = {
{ "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
G_OPTION_ARG_NONE, &option_detach,
"Don't run as daemon in background" },
- { "debug", 'd', 0, G_OPTION_ARG_NONE, &option_debug,
- "Enable debug information output" },
+ { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
+ G_OPTION_ARG_CALLBACK, parse_debug,
+ "Enable debug information output", "DEBUG" },
{ "root", 'r', 0, G_OPTION_ARG_STRING, &option_root,
"Specify root folder location", "PATH" },
{ "root-setup", 'S', 0, G_OPTION_ARG_STRING, &option_root_setup,
GOptionContext *context;
GError *err = NULL;
struct sigaction sa;
- int log_option = LOG_NDELAY | LOG_PID;
+ int log_option = 0;
#ifdef NEED_THREADS
if (g_thread_supported() == FALSE)
exit(EXIT_FAILURE);
}
- openlog("obexd", log_option, LOG_DAEMON);
+ log_init("obexd", option_debug, log_option);
- if (option_debug == TRUE) {
- info("Enabling debug information");
- enable_debug();
- }
+ DBG("Entering main loop");
main_loop = g_main_loop_new(NULL, FALSE);
diff --git a/obexd/src/manager.c b/obexd/src/manager.c
index c5a3fc0..d0fe936 100644
--- a/obexd/src/manager.c
+++ b/obexd/src/manager.c
#include "obex-priv.h"
#include "server.h"
#include "dbus.h"
-#include "logging.h"
+#include "log.h"
#include "btio.h"
#include "service.h"
diff --git a/obexd/src/mimetype.c b/obexd/src/mimetype.c
index da281fc..95a667b 100644
--- a/obexd/src/mimetype.c
+++ b/obexd/src/mimetype.c
#include <openobex/obex.h>
#include <openobex/obex_const.h>
-#include "logging.h"
+#include "log.h"
#include "obex.h"
#include "mimetype.h"
diff --git a/obexd/src/obex.c b/obexd/src/obex.c
index 4f1b766..200bc17 100644
--- a/obexd/src/obex.c
+++ b/obexd/src/obex.c
#include <openobex/obex.h>
-#include "logging.h"
+#include "log.h"
#include "obex.h"
#include "obex-priv.h"
#include "server.h"
diff --git a/obexd/src/plugin.c b/obexd/src/plugin.c
index 2e12ac3..a181858 100644
--- a/obexd/src/plugin.c
+++ b/obexd/src/plugin.c
#include "obexd.h"
#include "plugin.h"
-#include "logging.h"
+#include "log.h"
/*
* Plugins that are using libraries with threads and their own mainloop
diff --git a/obexd/src/server.c b/obexd/src/server.c
index 1f96644..f52be54 100644
--- a/obexd/src/server.c
+++ b/obexd/src/server.c
#include <openobex/obex.h>
-#include "logging.h"
+#include "log.h"
#include "obex.h"
#include "obex-priv.h"
#include "server.h"
diff --git a/obexd/src/service.c b/obexd/src/service.c
index 5afd29b..395b8f9 100644
--- a/obexd/src/service.c
+++ b/obexd/src/service.c
#include "obex.h"
#include "service.h"
-#include "logging.h"
+#include "log.h"
static GSList *drivers = NULL;
diff --git a/obexd/src/transport.c b/obexd/src/transport.c
index 6b3c9d3..80096b3 100644
--- a/obexd/src/transport.c
+++ b/obexd/src/transport.c
#include "obex.h"
#include "server.h"
#include "transport.h"
-#include "logging.h"
+#include "log.h"
static GSList *drivers = NULL;