Skip to content

Commit

Permalink
Improves log feature:
Browse files Browse the repository at this point in the history
  * Add --disable-log
  * Add a log level filter, configurable using conf file (ie. /etc/nfc/libnfc.conf) or environment var LIBNFC_LOG_LEVEL
  • Loading branch information
neomilium committed Nov 26, 2012
1 parent d6c8790 commit 9b3947b
Show file tree
Hide file tree
Showing 21 changed files with 403 additions and 269 deletions.
17 changes: 14 additions & 3 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,27 @@ AC_TYPE_OFF_T
LIBNFC_CFLAGS='-I$(top_srcdir)/libnfc -I$(top_builddir)/include -I$(top_srcdir)/include'
AC_SUBST(LIBNFC_CFLAGS)

# Debug support (default:no)
AC_ARG_ENABLE([debug],AS_HELP_STRING([--enable-debug],[Enable debug output]),[enable_debug=$enableval],[enable_debug="no"])
# Log support (default:yes)
AC_ARG_ENABLE([log],AS_HELP_STRING([--disable-log],[Disable any logs]),[enable_log=$enableval],[enable_log="yes"])
AC_MSG_CHECKING(for log flag)
AC_MSG_RESULT($enable_log)
AM_CONDITIONAL([WITH_LOG], [test "$enable_log" != "no"])

if test x"$enable_log" = "xyes"
then
AC_DEFINE([LOG], [1], [Enable log])
fi

# Debug support (default:no)
AC_ARG_ENABLE([debug],AS_HELP_STRING([--enable-debug],[Enable debug mode]),[enable_debug=$enableval],[enable_debug="no"])
AC_MSG_CHECKING(for debug flag)
AC_MSG_RESULT($enable_debug)
AM_CONDITIONAL([WITH_DEBUG], [test "$enable_debug" != "no"])

if test x"$enable_debug" = "xyes"
then
CFLAGS="$CFLAGS -g -DDEBUG -O0 -ggdb"
AC_DEFINE([DEBUG], [1], [Enable debug flag])
CFLAGS="$CFLAGS -g -O0 -ggdb"
fi

# Handle --with-drivers option
Expand Down
5 changes: 5 additions & 0 deletions libnfc.conf.sample
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@
# Allow intrusive auto-detection (default: false)
# Warning: intrusive auto-detection can seriously disturb other devices
#allow_intrusive_autoscan = false

# Set log level (default: error)
# Valid log levels are (in order of verbosity): 0 (none), 1 (error), 2 (info), 3 (debug)
# Note: if you set --enable-debug option, the default log level is "debug"
#log_level = 1
3 changes: 2 additions & 1 deletion libnfc/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ lib_LTLIBRARIES = libnfc.la
libnfc_la_SOURCES = \
conf.c \
iso14443-subr.c \
log.c \
mirror-subr.c \
nfc.c \
nfc-device.c \
Expand All @@ -40,7 +41,7 @@ if LIBUSB_ENABLED
libnfc_la_LIBADD += @libusb_LIBS@
endif

if WITH_DEBUG
if WITH_LOG
libnfc_la_SOURCES += log-printf.c
endif

Expand Down
19 changes: 10 additions & 9 deletions libnfc/buses/uart_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

#include "nfc-internal.h"

#define LOG_GROUP NFC_LOG_GROUP_COM
#define LOG_CATEGORY "libnfc.bus.uart"

# if defined(__APPLE__)
Expand Down Expand Up @@ -127,14 +128,14 @@ uart_flush_input(serial_port sp)
char *rx = malloc(available_bytes_count);
// There is something available, read the data
res = read(UART_DATA(sp)->fd, rx, available_bytes_count);
log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "%d bytes have eatten.", available_bytes_count);
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%d bytes have eatten.", available_bytes_count);
free(rx);
}

void
uart_set_speed(serial_port sp, const uint32_t uiPortSpeed)
{
log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "Serial port speed requested to be set to %d bauds.", uiPortSpeed);
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "Serial port speed requested to be set to %d bauds.", uiPortSpeed);

// Portability note: on some systems, B9600 != 9600 so we have to do
// uint32_t <=> speed_t associations by hand.
Expand Down Expand Up @@ -170,7 +171,7 @@ uart_set_speed(serial_port sp, const uint32_t uiPortSpeed)
break;
# endif
default:
log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to set serial port speed to %d bauds. Speed value must be one of those defined in termios(3).",
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to set serial port speed to %d bauds. Speed value must be one of those defined in termios(3).",
uiPortSpeed);
return;
};
Expand All @@ -179,7 +180,7 @@ uart_set_speed(serial_port sp, const uint32_t uiPortSpeed)
cfsetispeed(&(UART_DATA(sp)->termios_new), stPortSpeed);
cfsetospeed(&(UART_DATA(sp)->termios_new), stPortSpeed);
if (tcsetattr(UART_DATA(sp)->fd, TCSADRAIN, &(UART_DATA(sp)->termios_new)) == -1) {
log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to apply new speed settings.");
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Unable to apply new speed settings.");
}
}

Expand Down Expand Up @@ -279,18 +280,18 @@ uart_receive(serial_port sp, uint8_t *pbtRx, const size_t szRx, void *abort_p, i

// Read error
if (res < 0) {
log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "Error: %s", strerror(errno));
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "Error: %s", strerror(errno));
return NFC_EIO;
}
// Read time-out
if (res == 0) {
log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s", "Timeout!");
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%s", "Timeout!");
return NFC_ETIMEOUT;
}

if (FD_ISSET(iAbortFd, &rfds)) {
// Abort requested
log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s", "Abort!");
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%s", "Abort!");
close(iAbortFd);
return NFC_EOPABORTED;
}
Expand All @@ -309,7 +310,7 @@ uart_receive(serial_port sp, uint8_t *pbtRx, const size_t szRx, void *abort_p, i
received_bytes_count += res;

} while (expected_bytes_count > received_bytes_count);
LOG_HEX("RX", pbtRx, szRx);
LOG_HEX(LOG_GROUP, "RX", pbtRx, szRx);
return NFC_SUCCESS;
}

Expand All @@ -322,7 +323,7 @@ int
uart_send(serial_port sp, const uint8_t *pbtTx, const size_t szTx, int timeout)
{
(void) timeout;
LOG_HEX("TX", pbtTx, szTx);
LOG_HEX(LOG_GROUP, "TX", pbtTx, szTx);
if ((int) szTx == write(UART_DATA(sp)->fd, pbtTx, szTx))
return NFC_SUCCESS;
else
Expand Down
18 changes: 9 additions & 9 deletions libnfc/chips/pn53x-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Public platform independent Near Field Communication (NFC) library
*
* Copyright (C) 2011 Romain Tartière
* Copyright (C) 2011 Romuald Conty
* Copyright (C) 2011, 2012 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
Expand Down Expand Up @@ -115,7 +115,7 @@
typedef struct {
uint8_t ui8Code;
uint8_t ui8CompatFlags;
#ifdef LOGGING
#ifdef LOG
const char *abtCommandText;
#endif
} pn53x_command;
Expand All @@ -128,15 +128,15 @@ typedef enum {
RCS360 = 0x08
} pn53x_type;

#ifndef LOGGING
#ifndef LOG
# define PNCMD( X, Y ) { X , Y }
# define PNCMD_TRACE( X ) do {} while(0)
#else
# define PNCMD( X, Y ) { X , Y, #X }
# define PNCMD_TRACE( X ) do { \
for (size_t i=0; i<(sizeof(pn53x_commands)/sizeof(pn53x_command)); i++) { \
if ( X == pn53x_commands[i].ui8Code ) { \
log_put( LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s", pn53x_commands[i].abtCommandText ); \
log_put( LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%s", pn53x_commands[i].abtCommandText ); \
break; \
} \
} \
Expand Down Expand Up @@ -201,7 +201,7 @@ static const pn53x_command pn53x_commands[] = {
#define P35 5

// Registers part
#ifdef LOGGING
#ifdef LOG
typedef struct {
uint16_t ui16Address;
const char *abtRegisterText;
Expand All @@ -210,17 +210,17 @@ typedef struct {

# define PNREG( X, Y ) { X , #X, Y }

#endif /* LOGGING */
#endif /* LOG */


#ifndef LOGGING
#ifndef LOG
# define PNREG_TRACE( X ) do { \
} while(0)
#else
# define PNREG_TRACE( X ) do { \
for (size_t i=0; i<(sizeof(pn53x_registers)/sizeof(pn53x_register)); i++) { \
if ( X == pn53x_registers[i].ui16Address ) { \
log_put( LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s (%s)", pn53x_registers[i].abtRegisterText, pn53x_registers[i].abtRegisterDescription ); \
log_put( LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%s (%s)", pn53x_registers[i].abtRegisterText, pn53x_registers[i].abtRegisterDescription ); \
break; \
} \
} \
Expand Down Expand Up @@ -329,7 +329,7 @@ typedef struct {
#define EOVCURRENT 0x2d
#define ENAD 0x2e

#ifdef LOGGING
#ifdef LOG
static const pn53x_register pn53x_registers[] = {
PNREG(PN53X_REG_CIU_Mode, "Defines general modes for transmitting and receiving"),
PNREG(PN53X_REG_CIU_TxMode, "Defines the transmission data rate and framing during transmission"),
Expand Down
23 changes: 12 additions & 11 deletions libnfc/chips/pn53x.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* Copyright (C) 2009, 2010 Roel Verdult
* Copyright (C) 2010, 2011 Romain Tartière
* Copyright (C) 2009, 2010, 2011 Romuald Conty
* Copyright (C) 2009, 2010, 2011, 2012 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
Expand Down Expand Up @@ -41,6 +41,7 @@
#include "mirror-subr.h"

#define LOG_CATEGORY "libnfc.chip.pn53x"
#define LOG_GROUP NFC_LOG_GROUP_CHIP

const uint8_t pn53x_ack_frame[] = { 0x00, 0x00, 0xff, 0x00, 0xff, 0x00 };
const uint8_t pn53x_nack_frame[] = { 0x00, 0x00, 0xff, 0xff, 0x00, 0x00 };
Expand Down Expand Up @@ -154,13 +155,13 @@ pn53x_transceive(struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx

PNCMD_TRACE(pbtTx[0]);
if (timeout > 0) {
log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "Timeout values: %d", timeout);
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "Timeout values: %d", timeout);
} else if (timeout == 0) {
log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s", "No timeout");
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%s", "No timeout");
} else if (timeout == -1) {
timeout = CHIP_DATA(pnd)->timeout_command;
} else {
log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "Invalid timeout value: %d", timeout);
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Invalid timeout value: %d", timeout);
}

uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN];
Expand Down Expand Up @@ -294,7 +295,7 @@ pn53x_transceive(struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx

if (res < 0) {
pnd->last_error = res;
log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "Chip error: \"%s\" (%02x), returned error: \"%s\" (%d))", pn53x_strerror(pnd), CHIP_DATA(pnd)->last_status_byte, nfc_strerror(pnd), res);
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "Chip error: \"%s\" (%02x), returned error: \"%s\" (%d))", pn53x_strerror(pnd), CHIP_DATA(pnd)->last_status_byte, nfc_strerror(pnd), res);
} else {
pnd->last_error = 0;
}
Expand Down Expand Up @@ -1359,7 +1360,7 @@ pn53x_initiator_transceive_bytes(struct nfc_device *pnd, const uint8_t *pbtTx, c
const size_t szRxLen = (size_t)res - 1;
if (pbtRx != NULL) {
if (szRxLen > szRx) {
log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "Buffer size is too short: %zuo available(s), %zuo needed", szRx, szRxLen);
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Buffer size is too short: %zuo available(s), %zuo needed", szRx, szRxLen);
return NFC_EOVFLOW;
}
// Copy the received bytes
Expand Down Expand Up @@ -1634,7 +1635,7 @@ pn53x_initiator_transceive_bytes_timed(struct nfc_device *pnd, const uint8_t *pb
}
if (pbtRx != NULL) {
if ((szRxLen + sz) > szRx) {
log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "Buffer size is too short: %zuo available(s), %zuo needed", szRx, szRxLen + sz);
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Buffer size is too short: %zuo available(s), %zuo needed", szRx, szRxLen + sz);
return NFC_EOVFLOW;
}
// Copy the received bytes
Expand Down Expand Up @@ -2635,12 +2636,12 @@ pn53x_check_ack_frame(struct nfc_device *pnd, const uint8_t *pbtRxFrame, const s
{
if (szRxFrameLen >= sizeof(pn53x_ack_frame)) {
if (0 == memcmp(pbtRxFrame, pn53x_ack_frame, sizeof(pn53x_ack_frame))) {
log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s", "PN53x ACKed");
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%s", "PN53x ACKed");
return NFC_SUCCESS;
}
}
pnd->last_error = NFC_EIO;
log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unexpected PN53x reply!");
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Unexpected PN53x reply!");
return pnd->last_error;
}

Expand All @@ -2649,7 +2650,7 @@ pn53x_check_error_frame(struct nfc_device *pnd, const uint8_t *pbtRxFrame, const
{
if (szRxFrameLen >= sizeof(pn53x_error_frame)) {
if (0 == memcmp(pbtRxFrame, pn53x_error_frame, sizeof(pn53x_error_frame))) {
log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s", "PN53x sent an error frame");
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%s", "PN53x sent an error frame");
pnd->last_error = NFC_EIO;
return pnd->last_error;
}
Expand Down Expand Up @@ -2714,7 +2715,7 @@ pn53x_build_frame(uint8_t *pbtFrame, size_t *pszFrame, const uint8_t *pbtData, c

(*pszFrame) = szData + PN53x_EXTENDED_FRAME__OVERHEAD;
} else {
log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "We can't send more than %d bytes in a raw (requested: %zd)", PN53x_EXTENDED_FRAME__DATA_MAX_LEN, szData);
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "We can't send more than %d bytes in a raw (requested: %zd)", PN53x_EXTENDED_FRAME__DATA_MAX_LEN, szData);
return NFC_ECHIP;
}
return NFC_SUCCESS;
Expand Down
15 changes: 10 additions & 5 deletions libnfc/conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/

#include "conf.h"

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
Expand All @@ -27,13 +29,14 @@
#include "log.h"

#define LOG_CATEGORY "libnfc.config"
#define LOG_GROUP NFC_LOG_GROUP_CONFIG

#define LIBNFC_SYSCONFDIR "/etc/nfc"
#define LIBNFC_CONFFILE LIBNFC_SYSCONFDIR"/libnfc.conf"
#define LIBNFC_DEVICECONFDIR LIBNFC_SYSCONFDIR"/devices.d"


bool conf_parse_file(const char* filename, void (*conf_keyvalue)(void* data, const char* key, const char* value), void* data)
static bool
conf_parse_file(const char* filename, void (*conf_keyvalue)(void* data, const char* key, const char* value), void* data)
{
FILE *f = fopen (filename, "r");
if (!f) {
Expand Down Expand Up @@ -73,7 +76,7 @@ bool conf_parse_file(const char* filename, void (*conf_keyvalue)(void* data, con
strncpy(value, line+(pmatch[value_pmatch].rm_so), value_size); value[value_size]='\0';
conf_keyvalue(data, key, value);
} else {
log_put( LOG_CATEGORY, NFC_PRIORITY_TRACE, "parse error on line #%d: %s", lineno, line);
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "parse error on line #%d: %s", lineno, line);
}
}
break;
Expand All @@ -82,7 +85,7 @@ bool conf_parse_file(const char* filename, void (*conf_keyvalue)(void* data, con
return false;
}

void
static void
conf_keyvalue_context(void *data, const char* key, const char* value)
{
nfc_context *context = (nfc_context*)data;
Expand All @@ -91,8 +94,10 @@ conf_keyvalue_context(void *data, const char* key, const char* value)
string_as_boolean(value, &(context->allow_autoscan));
} else if (strcmp(key, "allow_intrusive_scan") == 0) {
string_as_boolean(value, &(context->allow_intrusive_scan));
} else if (strcmp(key, "log_level") == 0) {
context->log_level = atoi(value);
} else {
log_put( LOG_CATEGORY, NFC_PRIORITY_INFO, "unknown key in config line: %s = %s", key, value);
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_INFO, "unknown key in config line: %s = %s", key, value);
}
}

Expand Down
1 change: 1 addition & 0 deletions libnfc/conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* 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 "nfc-internal.h"

void conf_load(nfc_context *context);

Loading

0 comments on commit 9b3947b

Please sign in to comment.