Add a printf-based logging when log4c is not available (I experiemented some bugs using log4c)

This commit is contained in:
Romuald Conty 2011-12-07 14:59:40 +00:00
parent c286eec920
commit 90c05c7d13
7 changed files with 140 additions and 42 deletions

View file

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

View file

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

View file

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

View file

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

66
libnfc/log-printf.c Normal file
View file

@ -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 <http://www.gnu.org/licenses/>
*/
#include "config.h"
#include <stdio.h>
#include <stdarg.h>
#include <fcntl.h>
#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);
}

View file

@ -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 <log4c.h>
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 <log4c.h>
#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

View file

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