2001-12-13 21:19:11 +00:00
|
|
|
/*
|
2002-04-06 08:21:40 +00:00
|
|
|
* log.h: Logging functions header file
|
2001-12-13 21:19:11 +00:00
|
|
|
*
|
2006-12-19 21:31:17 +00:00
|
|
|
* Copyright (C) 2001, 2002 Juha Yrjölä <juha.yrjola@iki.fi>
|
2003-01-06 21:46:46 +00:00
|
|
|
* Copyright (C) 2003 Antti Tapaninen <aet@cc.hut.fi>
|
2001-12-13 21:19:11 +00:00
|
|
|
*
|
|
|
|
* This library 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 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2003-09-03 17:59:08 +00:00
|
|
|
#ifndef _OPENSC_LOG_H
|
|
|
|
#define _OPENSC_LOG_H
|
2001-12-13 21:19:11 +00:00
|
|
|
|
2003-01-06 21:46:46 +00:00
|
|
|
#ifdef __cplusplus
|
2002-04-19 14:23:31 +00:00
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2001-12-13 21:19:11 +00:00
|
|
|
#include <stdarg.h>
|
2010-03-04 08:14:36 +00:00
|
|
|
#include "libopensc/opensc.h"
|
2001-12-13 21:19:11 +00:00
|
|
|
|
2010-03-15 12:17:13 +00:00
|
|
|
enum {
|
|
|
|
SC_LOG_DEBUG_VERBOSE_TOOL = 1, /* tools only: verbose */
|
|
|
|
SC_LOG_DEBUG_VERBOSE, /* helps users */
|
|
|
|
SC_LOG_DEBUG_NORMAL, /* helps developers */
|
|
|
|
SC_LOG_DEBUG_RFU1, /* RFU */
|
2018-11-22 08:14:50 +00:00
|
|
|
SC_LOG_DEBUG_SM, /* secure messaging */
|
2018-11-20 11:48:58 +00:00
|
|
|
SC_LOG_DEBUG_ASN1, /* asn1.c */
|
|
|
|
SC_LOG_DEBUG_MATCH, /* card matching */
|
2020-03-05 21:15:27 +00:00
|
|
|
SC_LOG_DEBUG_PIN, /* PIN commands */
|
2010-03-15 12:17:13 +00:00
|
|
|
};
|
2001-12-13 21:19:11 +00:00
|
|
|
|
2018-11-25 21:08:36 +00:00
|
|
|
#define SC_COLOR_FG_RED 0x0001
|
|
|
|
#define SC_COLOR_FG_GREEN 0x0002
|
|
|
|
#define SC_COLOR_FG_YELLOW 0x0004
|
|
|
|
#define SC_COLOR_FG_BLUE 0x0008
|
|
|
|
#define SC_COLOR_FG_MAGENTA 0x0010
|
|
|
|
#define SC_COLOR_FG_CYAN 0x0020
|
|
|
|
#define SC_COLOR_BG_RED 0x0100
|
|
|
|
#define SC_COLOR_BG_GREEN 0x0200
|
|
|
|
#define SC_COLOR_BG_YELLOW 0x0400
|
|
|
|
#define SC_COLOR_BG_BLUE 0x0800
|
|
|
|
#define SC_COLOR_BG_MAGENTA 0x1000
|
|
|
|
#define SC_COLOR_BG_CYAN 0x2000
|
|
|
|
#define SC_COLOR_BOLD 0x8080
|
|
|
|
|
2003-01-06 21:46:46 +00:00
|
|
|
/* You can't do #ifndef __FUNCTION__ */
|
2005-10-24 21:58:35 +00:00
|
|
|
#if !defined(__GNUC__) && !defined(__IBMC__) && !(defined(_MSC_VER) && (_MSC_VER >= 1300))
|
2003-01-06 21:46:46 +00:00
|
|
|
#define __FUNCTION__ NULL
|
|
|
|
#endif
|
|
|
|
|
2002-06-14 12:52:56 +00:00
|
|
|
#if defined(__GNUC__)
|
2010-03-15 12:17:13 +00:00
|
|
|
#define sc_debug(ctx, level, format, args...) sc_do_log(ctx, level, __FILE__, __LINE__, __FUNCTION__, format , ## args)
|
2011-01-04 11:33:03 +00:00
|
|
|
#define sc_log(ctx, format, args...) sc_do_log(ctx, SC_LOG_DEBUG_NORMAL, __FILE__, __LINE__, __FUNCTION__, format , ## args)
|
2002-11-18 09:05:06 +00:00
|
|
|
#else
|
2010-04-14 11:52:27 +00:00
|
|
|
#define sc_debug _sc_debug
|
2011-01-04 11:33:03 +00:00
|
|
|
#define sc_log _sc_log
|
2002-11-18 09:05:06 +00:00
|
|
|
#endif
|
|
|
|
|
2016-08-24 22:12:55 +00:00
|
|
|
#if defined(__GNUC__)
|
2017-03-14 19:39:29 +00:00
|
|
|
#if defined(__MINGW32__) && defined (__MINGW_PRINTF_FORMAT)
|
|
|
|
#define SC_PRINTF_FORMAT __MINGW_PRINTF_FORMAT
|
|
|
|
#else
|
|
|
|
#define SC_PRINTF_FORMAT printf
|
|
|
|
#endif
|
|
|
|
|
2016-08-24 22:12:55 +00:00
|
|
|
/* GCC can check format and param correctness for us */
|
|
|
|
void sc_do_log(struct sc_context *ctx, int level, const char *file, int line,
|
|
|
|
const char *func, const char *format, ...)
|
2017-03-14 19:39:29 +00:00
|
|
|
__attribute__ ((format (SC_PRINTF_FORMAT, 6, 7)));
|
2018-11-25 21:08:36 +00:00
|
|
|
void sc_do_log_color(struct sc_context *ctx, int level, const char *file, int line,
|
|
|
|
const char *func, int color, const char *format, ...)
|
|
|
|
__attribute__ ((format (SC_PRINTF_FORMAT, 7, 8)));
|
2016-08-24 22:12:55 +00:00
|
|
|
void sc_do_log_noframe(sc_context_t *ctx, int level, const char *format,
|
2017-03-14 19:39:29 +00:00
|
|
|
va_list args) __attribute__ ((format (SC_PRINTF_FORMAT, 3, 0)));
|
2016-08-24 22:12:55 +00:00
|
|
|
void _sc_debug(struct sc_context *ctx, int level, const char *format, ...)
|
2017-03-14 19:39:29 +00:00
|
|
|
__attribute__ ((format (SC_PRINTF_FORMAT, 3, 4)));
|
2016-08-24 22:12:55 +00:00
|
|
|
void _sc_log(struct sc_context *ctx, const char *format, ...)
|
2017-03-14 19:39:29 +00:00
|
|
|
__attribute__ ((format (SC_PRINTF_FORMAT, 2, 3)));
|
2018-11-25 21:08:36 +00:00
|
|
|
int sc_color_fprintf(int colors, struct sc_context *ctx, FILE * stream, const char * format, ...)
|
|
|
|
__attribute__ ((format (SC_PRINTF_FORMAT, 4, 5)));
|
2016-08-24 22:12:55 +00:00
|
|
|
#else
|
|
|
|
void sc_do_log(struct sc_context *ctx, int level, const char *file, int line, const char *func,
|
2010-04-14 11:52:27 +00:00
|
|
|
const char *format, ...);
|
2018-11-25 21:08:36 +00:00
|
|
|
void sc_do_log_color(struct sc_context *ctx, int level, const char *file, int line, const char *func, int color,
|
|
|
|
const char *format, ...);
|
2011-05-08 08:07:03 +00:00
|
|
|
void sc_do_log_noframe(sc_context_t *ctx, int level, const char *format, va_list args);
|
2010-04-14 11:52:27 +00:00
|
|
|
void _sc_debug(struct sc_context *ctx, int level, const char *format, ...);
|
2011-01-04 11:33:03 +00:00
|
|
|
void _sc_log(struct sc_context *ctx, const char *format, ...);
|
2018-11-25 21:08:36 +00:00
|
|
|
int sc_color_fprintf(int colors, struct sc_context *ctx, FILE * stream, const char * format, ...);
|
2016-08-24 22:12:55 +00:00
|
|
|
#endif
|
2012-08-03 21:12:12 +00:00
|
|
|
/**
|
|
|
|
* @brief Log binary data to a sc context
|
|
|
|
*
|
|
|
|
* @param[in] ctx Context for logging
|
|
|
|
* @param[in] level
|
|
|
|
* @param[in] label Label to prepend to the buffer
|
|
|
|
* @param[in] data Binary data
|
|
|
|
* @param[in] len Length of \a data
|
|
|
|
*/
|
|
|
|
#define sc_debug_hex(ctx, level, label, data, len) \
|
|
|
|
_sc_debug_hex(ctx, level, __FILE__, __LINE__, __FUNCTION__, label, data, len)
|
2017-11-13 14:11:58 +00:00
|
|
|
#define sc_log_hex(ctx, label, data, len) \
|
|
|
|
sc_debug_hex(ctx, SC_LOG_DEBUG_NORMAL, label, data, len)
|
2012-08-03 21:12:12 +00:00
|
|
|
/**
|
|
|
|
* @brief Log binary data
|
|
|
|
*
|
|
|
|
* @param[in] ctx Context for logging
|
2018-10-31 11:59:00 +00:00
|
|
|
* @param[in] level Debug level
|
2012-08-03 21:12:12 +00:00
|
|
|
* @param[in] file File name to be prepended
|
|
|
|
* @param[in] line Line to be prepended
|
|
|
|
* @param[in] func Function to be prepended
|
|
|
|
* @param[in] label label to prepend to the buffer
|
|
|
|
* @param[in] data binary data
|
|
|
|
* @param[in] len length of \a data
|
|
|
|
*/
|
|
|
|
void _sc_debug_hex(struct sc_context *ctx, int level, const char *file, int line,
|
|
|
|
const char *func, const char *label, const u8 *data, size_t len);
|
2003-01-06 21:46:46 +00:00
|
|
|
|
2017-11-13 14:11:58 +00:00
|
|
|
void sc_hex_dump(const u8 *buf, size_t len, char *out, size_t outlen);
|
|
|
|
const char * sc_dump_hex(const u8 * in, size_t count);
|
|
|
|
const char * sc_dump_oid(const struct sc_object_id *oid);
|
2006-11-06 11:15:09 +00:00
|
|
|
#define SC_FUNC_CALLED(ctx, level) do { \
|
2010-03-15 12:17:13 +00:00
|
|
|
sc_do_log(ctx, level, __FILE__, __LINE__, __FUNCTION__, "called\n"); \
|
2006-11-06 11:15:09 +00:00
|
|
|
} while (0)
|
2011-01-04 11:33:03 +00:00
|
|
|
#define LOG_FUNC_CALLED(ctx) SC_FUNC_CALLED((ctx), SC_LOG_DEBUG_NORMAL)
|
2001-12-13 21:19:11 +00:00
|
|
|
|
2006-11-06 11:15:09 +00:00
|
|
|
#define SC_FUNC_RETURN(ctx, level, r) do { \
|
2003-01-06 21:46:46 +00:00
|
|
|
int _ret = r; \
|
2010-12-14 03:08:17 +00:00
|
|
|
if (_ret <= 0) { \
|
2018-11-25 21:08:36 +00:00
|
|
|
sc_do_log_color(ctx, level, __FILE__, __LINE__, __FUNCTION__, _ret ? SC_COLOR_FG_RED : 0, \
|
2010-12-14 03:08:17 +00:00
|
|
|
"returning with: %d (%s)\n", _ret, sc_strerror(_ret)); \
|
|
|
|
} else { \
|
|
|
|
sc_do_log(ctx, level, __FILE__, __LINE__, __FUNCTION__, \
|
|
|
|
"returning with: %d\n", _ret); \
|
|
|
|
} \
|
2003-01-06 21:46:46 +00:00
|
|
|
return _ret; \
|
2006-11-06 11:15:09 +00:00
|
|
|
} while(0)
|
2011-01-04 11:33:03 +00:00
|
|
|
#define LOG_FUNC_RETURN(ctx, r) SC_FUNC_RETURN((ctx), SC_LOG_DEBUG_NORMAL, (r))
|
2003-01-06 21:46:46 +00:00
|
|
|
|
2010-03-15 12:17:13 +00:00
|
|
|
#define SC_TEST_RET(ctx, level, r, text) do { \
|
2003-01-06 21:46:46 +00:00
|
|
|
int _ret = (r); \
|
|
|
|
if (_ret < 0) { \
|
2018-11-25 21:08:36 +00:00
|
|
|
sc_do_log_color(ctx, level, __FILE__, __LINE__, __FUNCTION__, SC_COLOR_FG_RED, \
|
2010-12-14 03:08:17 +00:00
|
|
|
"%s: %d (%s)\n", (text), _ret, sc_strerror(_ret)); \
|
2003-01-06 21:46:46 +00:00
|
|
|
return _ret; \
|
|
|
|
} \
|
2006-11-06 11:15:09 +00:00
|
|
|
} while(0)
|
2011-01-04 11:33:03 +00:00
|
|
|
#define LOG_TEST_RET(ctx, r, text) SC_TEST_RET((ctx), SC_LOG_DEBUG_NORMAL, (r), (text))
|
2001-12-13 21:19:11 +00:00
|
|
|
|
2015-04-24 16:13:34 +00:00
|
|
|
#define SC_TEST_GOTO_ERR(ctx, level, r, text) do { \
|
|
|
|
int _ret = (r); \
|
|
|
|
if (_ret < 0) { \
|
2018-11-25 21:08:36 +00:00
|
|
|
sc_do_log_color(ctx, level, __FILE__, __LINE__, __FUNCTION__, SC_COLOR_FG_RED, \
|
2015-04-24 16:13:34 +00:00
|
|
|
"%s: %d (%s)\n", (text), _ret, sc_strerror(_ret)); \
|
|
|
|
goto err; \
|
|
|
|
} \
|
|
|
|
} while(0)
|
|
|
|
#define LOG_TEST_GOTO_ERR(ctx, r, text) SC_TEST_GOTO_ERR((ctx), SC_LOG_DEBUG_NORMAL, (r), (text))
|
|
|
|
|
2003-01-06 21:46:46 +00:00
|
|
|
#ifdef __cplusplus
|
2002-04-19 14:23:31 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-12-13 21:19:11 +00:00
|
|
|
#endif
|