From 90c05c7d1361ef319331d8ca7fc5683a0a8d1a22 Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Wed, 7 Dec 2011 14:59:40 +0000 Subject: [PATCH] Add a printf-based logging when log4c is not available (I experiemented some bugs using log4c) --- examples/nfc-dep-target.c | 2 +- include/nfc/nfc-types.h | 3 ++ libnfc/Makefile.am | 15 +++++- libnfc/{log.c => log-log4c.c} | 1 + libnfc/log-printf.c | 66 +++++++++++++++++++++++++ libnfc/log.h | 93 +++++++++++++++++++++-------------- libnfc/nfc-internal.h | 2 +- 7 files changed, 140 insertions(+), 42 deletions(-) rename libnfc/{log.c => log-log4c.c} (98%) create mode 100644 libnfc/log-printf.c diff --git a/examples/nfc-dep-target.c b/examples/nfc-dep-target.c index b7b45a1..7ed991e 100644 --- a/examples/nfc-dep-target.c +++ b/examples/nfc-dep-target.c @@ -77,7 +77,7 @@ main (int argc, const char *argv[]) } else if (szDeviceFound > 1) { pnd = nfc_connect (connstrings[1]); } else { - printf("No device found."); + printf("No device found.\n"); return EXIT_FAILURE; } diff --git a/include/nfc/nfc-types.h b/include/nfc/nfc-types.h index 647ccce..bac47f5 100644 --- a/include/nfc/nfc-types.h +++ b/include/nfc/nfc-types.h @@ -69,6 +69,9 @@ typedef struct { int iLastError; } nfc_device; +/** + * Connection string + */ typedef char nfc_connstring[1024]; // Compiler directive, set struct alignment to 1 uint8_t for compatibility diff --git a/libnfc/Makefile.am b/libnfc/Makefile.am index f596549..a48d818 100644 --- a/libnfc/Makefile.am +++ b/libnfc/Makefile.am @@ -39,7 +39,18 @@ if HAS_LOG4C libnfc_la_CFLAGS += @log4c_CFLAGS@ libnfc_la_LIBADD += @log4c_LIBS@ - libnfc_la_SOURCES += log.c + libnfc_la_SOURCES += log-log4c.c +else +if WITH_DEBUG + libnfc_la_CFLAGS += @log4c_CFLAGS@ + libnfc_la_LIBADD += @log4c_LIBS@ + + libnfc_la_SOURCES += log-printf.c +endif endif -EXTRA_DIST = CMakeLists.txt +EXTRA_DIST = \ + CMakeLists.txt \ + log-log4c.c \ + log-printf.c + diff --git a/libnfc/log.c b/libnfc/log-log4c.c similarity index 98% rename from libnfc/log.c rename to libnfc/log-log4c.c index 5d12136..ef0cf2e 100644 --- a/libnfc/log.c +++ b/libnfc/log-log4c.c @@ -61,5 +61,6 @@ log_put (char *category, int priority, char *format, ...) va_list va; va_start (va, format); log4c_category_vlog (cat, priority, format, va); +// va_end (va); } } diff --git a/libnfc/log-printf.c b/libnfc/log-printf.c new file mode 100644 index 0000000..1ef4203 --- /dev/null +++ b/libnfc/log-printf.c @@ -0,0 +1,66 @@ +/*- + * Copyright (C) 2011, Romain Tartière, Romuald Conty + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see + */ + +#include "config.h" + +#include +#include +#include + +#include "log.h" + +static uint8_t __log_init_counter = 0; + +int +log_init (void) +{ + int res = 0; + + if (__log_init_counter == 0) { + res = 0; + } + if (!res) { + __log_init_counter++; + } + return res; +} + +int +log_fini (void) +{ + int res = 0; + if (__log_init_counter >= 1) { + if (__log_init_counter == 1) { + res = 0; + } + __log_init_counter--; + } else { + res = -1; + } + return res; +} + +void +log_put (char *category, char *priority, char *format, ...) +{ + va_list va; + va_start (va, format); + printf ("%s\t%s\t", priority, category); + vprintf (format, va); + printf ("\n"); + va_end (va); +} diff --git a/libnfc/log.h b/libnfc/log.h index 90cb747..9d8b543 100644 --- a/libnfc/log.h +++ b/libnfc/log.h @@ -18,45 +18,62 @@ #ifndef __LOG_H__ #define __LOG_H__ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif // HAVE_CONFIG_H + #if defined(HAS_LOG4C) && HAS_LOG4C - -#define LOGGING 1 - -#include - -int log_init (void); -int log_fini (void); -void log_put (char *category, int priority, char *format, ...); - -#define NFC_PRIORITY_FATAL LOG4C_PRIORITY_FATAL -#define NFC_PRIORITY_ALERT LOG4C_PRIORITY_ALERT -#define NFC_PRIORITY_CRIT LOG4C_PRIORITY_CRIT -#define NFC_PRIORITY_ERROR LOG4C_PRIORITY_ERROR -#define NFC_PRIORITY_WARN LOG4C_PRIORITY_WARN -#define NFC_PRIORITY_NOTICE LOG4C_PRIORITY_NOTICE -#define NFC_PRIORITY_INFO LOG4C_PRIORITY_INFO -#define NFC_PRIORITY_DEBUG LOG4C_PRIORITY_DEBUG -#define NFC_PRIORITY_TRACE LOG4C_PRIORITY_TRACE - -#else /* HAS_LOG4C */ - -#define log_init() (0) -#define log_fini() (0) -#define log_msg(category, priority, message) do {} while (0) -#define log_set_appender(category, appender) do {} while (0) -#define log_put(category, priority, format, ...) do {} while (0) - -#define NFC_PRIORITY_FATAL 8 -#define NFC_PRIORITY_ALERT 7 -#define NFC_PRIORITY_CRIT 6 -#define NFC_PRIORITY_ERROR 5 -#define NFC_PRIORITY_WARN 4 -#define NFC_PRIORITY_NOTICE 3 -#define NFC_PRIORITY_INFO 2 -#define NFC_PRIORITY_DEBUG 1 -#define NFC_PRIORITY_TRACE 0 - -#endif /* HAS_LOG4C */ + // log4c have been detected so we use it.. + #include + #define LOGGING 1 + + int log_init (void); + int log_fini (void); + void log_put (char *category, int priority, char *format, ...); + + #define NFC_PRIORITY_FATAL LOG4C_PRIORITY_FATAL + #define NFC_PRIORITY_ALERT LOG4C_PRIORITY_ALERT + #define NFC_PRIORITY_CRIT LOG4C_PRIORITY_CRIT + #define NFC_PRIORITY_ERROR LOG4C_PRIORITY_ERROR + #define NFC_PRIORITY_WARN LOG4C_PRIORITY_WARN + #define NFC_PRIORITY_NOTICE LOG4C_PRIORITY_NOTICE + #define NFC_PRIORITY_INFO LOG4C_PRIORITY_INFO + #define NFC_PRIORITY_DEBUG LOG4C_PRIORITY_DEBUG + #define NFC_PRIORITY_TRACE LOG4C_PRIORITY_TRACE +#elif defined DEBUG + // log4c is not detected but user want debug features + #define LOGGING 1 + int log_init (void); + int log_fini (void); + void log_put (char *category, char *priority, char *format, ...); + + #define NFC_PRIORITY_FATAL "fatal" + #define NFC_PRIORITY_ALERT "alert" + #define NFC_PRIORITY_CRIT "critical" + #define NFC_PRIORITY_ERROR "error" + #define NFC_PRIORITY_WARN "warning" + #define NFC_PRIORITY_NOTICE "notice" + #define NFC_PRIORITY_INFO "info" + #define NFC_PRIORITY_DEBUG "debug" + #define NFC_PRIORITY_TRACE "trace" +#else + // No logging + #define log_init() (0) + #define log_fini() (0) + #define log_msg(category, priority, message) do {} while (0) + #define log_set_appender(category, appender) do {} while (0) + #define log_put(category, priority, format, ...) do {} while (0) + + #define NFC_PRIORITY_FATAL 8 + #define NFC_PRIORITY_ALERT 7 + #define NFC_PRIORITY_CRIT 6 + #define NFC_PRIORITY_ERROR 5 + #define NFC_PRIORITY_WARN 4 + #define NFC_PRIORITY_NOTICE 3 + #define NFC_PRIORITY_INFO 2 + #define NFC_PRIORITY_DEBUG 1 + #define NFC_PRIORITY_TRACE 0 +#endif /* HAS_LOG4C, DEBUG */ /** * @macro LOG_HEX diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index e59edc4..b52566a 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -127,7 +127,7 @@ struct nfc_driver_t { const char *name; bool (*probe)(nfc_connstring connstrings[], size_t connstrings_len, size_t * pszDeviceFound); - nfc_device * (*connect) (const nfc_connstring connstring); + nfc_device *(*connect) (const nfc_connstring connstring); void (*disconnect) (nfc_device *pnd); const char *(*strerror) (const nfc_device *pnd);