Cleanups and logging improvements for non-GCC compilers
git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@844 c6295689-39f2-0310-b995-f0e70906c6a9
This commit is contained in:
parent
2e417f3d8d
commit
12bfb8ba64
|
@ -2,6 +2,7 @@
|
||||||
* log.c: Miscellaneous logging functions
|
* log.c: Miscellaneous logging functions
|
||||||
*
|
*
|
||||||
* Copyright (C) 2001, 2002 Juha Yrjölä <juha.yrjola@iki.fi>
|
* Copyright (C) 2001, 2002 Juha Yrjölä <juha.yrjola@iki.fi>
|
||||||
|
* Copyright (C) 2003 Antti Tapaninen <aet@cc.hut.fi>
|
||||||
*
|
*
|
||||||
* This library is free software; you can redistribute it and/or
|
* This library is free software; you can redistribute it and/or
|
||||||
* modify it under the terms of the GNU Lesser General Public
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
|
@ -43,7 +44,7 @@ void error(struct sc_context *ctx, const char *format, ...)
|
||||||
va_list ap;
|
va_list ap;
|
||||||
|
|
||||||
va_start(ap, format);
|
va_start(ap, format);
|
||||||
do_log2(ctx, SC_LOG_TYPE_ERROR, NULL, 0, "", format, ap);
|
sc_do_log_va(ctx, SC_LOG_TYPE_ERROR, NULL, 0, NULL, format, ap);
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,28 +53,18 @@ void debug(struct sc_context *ctx, const char *format, ...)
|
||||||
va_list ap;
|
va_list ap;
|
||||||
|
|
||||||
va_start(ap, format);
|
va_start(ap, format);
|
||||||
do_log2(ctx, SC_LOG_TYPE_DEBUG, NULL, 0, "", format, ap);
|
sc_do_log_va(ctx, SC_LOG_TYPE_DEBUG, NULL, 0, NULL, format, ap);
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void do_log(struct sc_context *ctx, int facility, const char *file,
|
static int use_color(struct sc_context *ctx, FILE * outf)
|
||||||
int line, const char *func, const char *format, ...)
|
|
||||||
{
|
|
||||||
va_list ap;
|
|
||||||
|
|
||||||
va_start(ap, format);
|
|
||||||
do_log2(ctx, facility, file, line, func, format, ap);
|
|
||||||
va_end(ap);
|
|
||||||
}
|
|
||||||
|
|
||||||
int use_color(struct sc_context *ctx, FILE *outf)
|
|
||||||
{
|
{
|
||||||
static char *term = NULL;
|
static char *term = NULL;
|
||||||
static const char *terms[] = { "linux", "xterm", "Eterm" };
|
static const char *terms[] = { "linux", "xterm", "Eterm" };
|
||||||
int term_count = sizeof(terms)/sizeof(terms[0]);
|
int term_count = sizeof(terms) / sizeof(terms[0]);
|
||||||
int do_color = 0;
|
int do_color;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
if (!isatty(fileno(outf)))
|
if (!isatty(fileno(outf)))
|
||||||
|
@ -89,13 +80,19 @@ int use_color(struct sc_context *ctx, FILE *outf)
|
||||||
do_color = 1;
|
do_color = 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (!do_color)
|
|
||||||
return 0;
|
|
||||||
return do_color;
|
return do_color;
|
||||||
}
|
}
|
||||||
|
|
||||||
void do_log2(struct sc_context *ctx, int type, const char *file,
|
void sc_do_log(struct sc_context *ctx, int type, const char *file, int line, const char *func, const char *format, ...)
|
||||||
int line, const char *func, const char *format, va_list args)
|
{
|
||||||
|
va_list ap;
|
||||||
|
|
||||||
|
va_start(ap, format);
|
||||||
|
sc_do_log_va(ctx, type, file, line, func, format, ap);
|
||||||
|
va_end(ap);
|
||||||
|
}
|
||||||
|
|
||||||
|
void sc_do_log_va(struct sc_context *ctx, int type, const char *file, int line, const char *func, const char *format, va_list args)
|
||||||
{
|
{
|
||||||
FILE *outf = NULL;
|
FILE *outf = NULL;
|
||||||
char buf[1024], *p;
|
char buf[1024], *p;
|
||||||
|
@ -129,7 +126,7 @@ void do_log2(struct sc_context *ctx, int type, const char *file,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (file != NULL) {
|
if (file != NULL) {
|
||||||
r = snprintf(buf, sizeof(buf), "%s:%d:%s: ", file, line, func);
|
r = snprintf(buf, sizeof(buf), "%s:%d:%s: ", file, line, func ? func : "");
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
return;
|
return;
|
||||||
} else
|
} else
|
||||||
|
@ -147,17 +144,16 @@ void do_log2(struct sc_context *ctx, int type, const char *file,
|
||||||
fflush(outf);
|
fflush(outf);
|
||||||
}
|
}
|
||||||
|
|
||||||
void sc_hex_dump(struct sc_context *ctx, const u8 *in, size_t count,
|
void sc_hex_dump(struct sc_context *ctx, const u8 * in, size_t count, char *buf, size_t len)
|
||||||
char *buf, size_t len)
|
|
||||||
{
|
{
|
||||||
char *p = buf;
|
char *p = buf;
|
||||||
int lines = 0;
|
int lines = 0;
|
||||||
|
|
||||||
assert(buf != NULL && in != NULL);
|
assert(buf != NULL && in != NULL);
|
||||||
buf[0] = 0;
|
buf[0] = 0;
|
||||||
if (count*5 > len)
|
if ((count * 5) > len)
|
||||||
return;
|
return;
|
||||||
while (count) {
|
while (count) {
|
||||||
char ascbuf[17];
|
char ascbuf[17];
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
* log.h: Logging functions header file
|
* log.h: Logging functions header file
|
||||||
*
|
*
|
||||||
* Copyright (C) 2001, 2002 Juha Yrjölä <juha.yrjola@iki.fi>
|
* Copyright (C) 2001, 2002 Juha Yrjölä <juha.yrjola@iki.fi>
|
||||||
|
* Copyright (C) 2003 Antti Tapaninen <aet@cc.hut.fi>
|
||||||
*
|
*
|
||||||
* This library is free software; you can redistribute it and/or
|
* This library is free software; you can redistribute it and/or
|
||||||
* modify it under the terms of the GNU Lesser General Public
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
|
@ -21,7 +22,7 @@
|
||||||
#ifndef _SC_LOG_H
|
#ifndef _SC_LOG_H
|
||||||
#define _SC_LOG_H
|
#define _SC_LOG_H
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -32,10 +33,17 @@ extern "C" {
|
||||||
#define SC_LOG_TYPE_VERBOSE 1
|
#define SC_LOG_TYPE_VERBOSE 1
|
||||||
#define SC_LOG_TYPE_DEBUG 2
|
#define SC_LOG_TYPE_DEBUG 2
|
||||||
|
|
||||||
|
/* You can't do #ifndef __FUNCTION__ */
|
||||||
|
#if !defined(__GNUC__) && !defined(__IBMC__)
|
||||||
|
#define __FUNCTION__ NULL
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* XXX: Add sc_ prefix? */
|
||||||
|
|
||||||
#if defined(__GNUC__)
|
#if defined(__GNUC__)
|
||||||
|
|
||||||
#define error(ctx, format, args...) do_log(ctx, SC_LOG_TYPE_ERROR, __FILE__, __LINE__, __FUNCTION__, format , ## args)
|
#define error(ctx, format, args...) sc_do_log(ctx, SC_LOG_TYPE_ERROR, __FILE__, __LINE__, __FUNCTION__, format , ## args)
|
||||||
#define debug(ctx, format, args...) do_log(ctx, SC_LOG_TYPE_DEBUG, __FILE__, __LINE__, __FUNCTION__, format , ## args)
|
#define debug(ctx, format, args...) sc_do_log(ctx, SC_LOG_TYPE_DEBUG, __FILE__, __LINE__, __FUNCTION__, format , ## args)
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
|
@ -44,35 +52,39 @@ void debug(struct sc_context *ctx, const char *format, ...);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define SC_FUNC_CALLED(ctx, level) {\
|
void sc_do_log(struct sc_context *ctx, int type, const char *file, int line, const char *func, const char *format, ...);
|
||||||
if ((ctx)->debug >= level)\
|
void sc_do_log_va(struct sc_context *ctx, int type, const char *file, int line, const char *func, const char *format, va_list args);
|
||||||
debug(ctx, "called\n"); }
|
|
||||||
#define SC_FUNC_RETURN(ctx, level, r) {\
|
void sc_hex_dump(struct sc_context *ctx, const u8 * buf, size_t len, char *out, size_t outlen);
|
||||||
int _ret = r;\
|
|
||||||
if (_ret < 0) {\
|
#define SC_FUNC_CALLED(ctx, level) { \
|
||||||
error(ctx, "returning with: %s\n", sc_strerror(_ret));\
|
if (ctx->debug >= level) \
|
||||||
} else if ((ctx)->debug >= level) {\
|
sc_do_log(ctx, SC_LOG_TYPE_DEBUG, __FILE__, __LINE__, __FUNCTION__, "called\n"); \
|
||||||
debug(ctx, "returning with: %d\n", _ret);\
|
|
||||||
}\
|
|
||||||
return _ret; }
|
|
||||||
#define SC_TEST_RET(ctx, r, text) {\
|
|
||||||
int _ret = (r);\
|
|
||||||
if (_ret < 0) {\
|
|
||||||
error((ctx), "%s: %s\n", (text), sc_strerror(_ret));\
|
|
||||||
return _ret;\
|
|
||||||
}\
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void do_log(struct sc_context *ctx, int facility, const char *file,
|
#define SC_FUNC_RETURN(ctx, level, r) { \
|
||||||
int line, const char *func, const char *format, ...);
|
int _ret = r; \
|
||||||
void do_log2(struct sc_context *ctx, int type, const char *file,
|
if (_ret < 0) { \
|
||||||
int line, const char *func, const char *format, va_list args);
|
sc_do_log(ctx, SC_LOG_TYPE_ERROR, __FILE__, __LINE__, __FUNCTION__, "returning with: %s\n", sc_strerror(_ret)); \
|
||||||
|
} else if (ctx->debug >= level) { \
|
||||||
|
sc_do_log(ctx, SC_LOG_TYPE_DEBUG, __FILE__, __LINE__, __FUNCTION__, "returning with: %d\n", _ret); \
|
||||||
|
} \
|
||||||
|
return _ret; \
|
||||||
|
}
|
||||||
|
|
||||||
void sc_hex_dump(struct sc_context *ctx, const u8 *buf, size_t len,
|
#define SC_TEST_RET(ctx, r, text) { \
|
||||||
char *out, size_t outlen);
|
int _ret = (r); \
|
||||||
#define sc_perror(ctx, errno, str) { error((ctx), "%s: %s\n", str, sc_strerror((errno))); }
|
if (_ret < 0) { \
|
||||||
|
sc_do_log(ctx, SC_LOG_TYPE_ERROR, __FILE__, __LINE__, __FUNCTION__, "%s: %s\n", (text), sc_strerror(_ret)); \
|
||||||
|
return _ret; \
|
||||||
|
} \
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#define sc_perror(ctx, errno, str) { \
|
||||||
|
sc_do_log(ctx, SC_LOG_TYPE_ERROR, __FILE__, __LINE__, __FUNCTION__, "%s: %s\n", str, sc_strerror(errno)); \
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue