Split logging internals so that platforms may choose additional or alternate spew mechanisms

In the case of windows, allow for the OutputDebugString call in addition to logging to stderr.
Useful for the dll scenario and debugging in Visual Studio.
This commit is contained in:
Alex Lian 2013-03-03 17:38:19 -05:00 committed by Philippe Teuwen
parent 563054d2a2
commit b3c6ea86ad
5 changed files with 122 additions and 5 deletions

View file

@ -46,7 +46,12 @@ SET(LIBRARY_SOURCES nfc nfc-device nfc-emulation nfc-internal conf iso14443-subr
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}) INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
IF(LIBNFC_LOG) IF(LIBNFC_LOG)
LIST(APPEND LIBRARY_SOURCES log-printf) IF(WIN32)
SET(CMAKE_C_FLAGS "-fgnu89-inline ${CMAKE_C_FLAGS}")
LIST(APPEND LIBRARY_SOURCES log-printf log_win32)
ELSE(WIN32)
LIST(APPEND LIBRARY_SOURCES log-printf log_posix)
ENDIF(WIN32)
ENDIF(LIBNFC_LOG) ENDIF(LIBNFC_LOG)
ADD_LIBRARY(nfc SHARED ${LIBRARY_SOURCES}) ADD_LIBRARY(nfc SHARED ${LIBRARY_SOURCES})

View file

@ -45,5 +45,7 @@ endif
EXTRA_DIST = \ EXTRA_DIST = \
CMakeLists.txt \ CMakeLists.txt \
log-printf.c log-printf.c \
log_posix.c \
log_win32.c

View file

@ -1,6 +1,7 @@
/*- /*-
* Copyright (C) 2011 Romain Tartière * Copyright (C) 2011 Romain Tartière
* Copyright (C) 2011, 2012 Romuald Conty * Copyright (C) 2011, 2012 Romuald Conty
* Copyright (C) 2013 Alex Lian
* *
* This program is free software: you can redistribute it and/or modify it * 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 * under the terms of the GNU Lesser General Public License as published by the
@ -31,6 +32,12 @@
#error "No logging defined, but log-printf.c still compiled." #error "No logging defined, but log-printf.c still compiled."
#else // LOG #else // LOG
// Internal methods so different platforms can route the logging
// Offering both forms of the variadic function
// These are implemented in the log_<platform> specific file
void log_put_internal(const char *format, ...);
void log_vput_internal(const char *format, va_list args);
void void
log_init(const nfc_context *context) log_init(const nfc_context *context)
{ {
@ -71,11 +78,17 @@ log_put(const uint8_t group, const char *category, const uint8_t priority, const
if (log_level) { // If log is not disabled by log_level=none if (log_level) { // If log is not disabled by log_level=none
if (((log_level & 0x00000003) >= priority) || // Global log level if (((log_level & 0x00000003) >= priority) || // Global log level
(((log_level >> (group * 2)) & 0x00000003) >= priority)) { // Group log level (((log_level >> (group * 2)) & 0x00000003) >= priority)) { // Group log level
va_list va; va_list va;
va_start(va, format); va_start(va, format);
fprintf(stderr, "%s\t%s\t", log_priority_to_str(priority), category);
vfprintf(stderr, format, va); // fprintf(stderr, "%s\t%s\t", log_priority_to_str(priority), category);
fprintf(stderr, "\n"); // vfprintf(stderr, format, va);
// fprintf(stderr, "\n");
log_put_internal("%s\t%s\t", log_priority_to_str(priority), category);
log_vput_internal(format, va);
log_put_internal("\n");
va_end(va); va_end(va);
} }
} }

41
libnfc/log_posix.c Normal file
View file

@ -0,0 +1,41 @@
/*-
* Copyright (C) 2011 Romain Tartière
* Copyright (C) 2011, 2012 Romuald Conty
* Copyright (C) 2013 Alex Lian
*
* 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 "log.h"
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdarg.h>
void
log_vput_internal(const char *format, va_list args)
{
vfprintf(stderr, format, args);
}
void
log_put_internal(const char *format, ...)
{
va_list va;
va_start(va, format);
vfprintf(stderr, format, va);
va_end(va);
}

56
libnfc/log_win32.c Normal file
View file

@ -0,0 +1,56 @@
/*-
* Copyright (C) 2011 Romain Tartière
* Copyright (C) 2011, 2012 Romuald Conty
* Copyright (C) 2013 Alex Lian
*
* 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 "log.h"
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdarg.h>
#include <strsafe.h>
void
log_output_debug(const char *format, va_list args)
{
char buffer[1024];
HRESULT hr = StringCbVPrintf( buffer, sizeof( buffer ), format, args );
// Spew what we got, even if the buffer is not sized large enough
if ( (STRSAFE_E_INSUFFICIENT_BUFFER == hr) || (S_OK == hr) )
OutputDebugString( buffer );
}
void
log_vput_internal(const char *format, va_list args)
{
vfprintf(stderr, format, args);
// Additional windows output to the debug window for debugging purposes
log_output_debug(format, args);
}
void
log_put_internal(const char *format, ...)
{
va_list va;
va_start(va, format);
vfprintf(stderr, format, va);
// Additional windows output to the debug window for debugging purposes
log_output_debug(format, va);
va_end(va);
}