logs: time stamp; dump_hex() with the static output buffer

- time stamp in the log messages: for Windows 1msec resolution, otherwise 1sec;
- one more dump hex function, to be easily inserted into the formatted message.


git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@3856 c6295689-39f2-0310-b995-f0e70906c6a9
This commit is contained in:
viktor.tarasov 2009-11-17 11:11:05 +00:00
parent 11e9cab070
commit b41fcbedd9
3 changed files with 67 additions and 4 deletions

View File

@ -75,6 +75,7 @@ sc_get_data
sc_get_mf_path
sc_get_version
sc_hex_dump
sc_dump_hex
sc_hex_to_bin
sc_list_files
sc_lock

View File

@ -25,6 +25,7 @@
#include <assert.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
@ -60,6 +61,13 @@ void sc_do_log_va(sc_context_t *ctx, int type, const char *file, int line, const
char buf[1836], *p;
int r;
size_t left;
#ifdef _WIN32
SYSTEMTIME st;
char szHora[64];
#else
time_t curtime;
struct tm *tm;
#endif
assert(ctx != NULL);
@ -74,16 +82,37 @@ void sc_do_log_va(sc_context_t *ctx, int type, const char *file, int line, const
return;
}
p = buf;
left = sizeof(buf);
#ifdef _WIN32
GetLocalTime(&st);
r = snprintf(p, left,
"%i-%02i-%02i %02i:%02i:%02i.%03i ",
st.wYear, st.wMonth, st.wDay,
st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
#else
curtime = time(NULL);
tm = localtime(&curtime);
r = snprintf(p, left,
"%i-%02i-%02i %02i:%02i:%02i ",
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec);
#endif
p += r;
left -= r;
if (file != NULL) {
r = snprintf(buf, sizeof(buf), "[%s] %s:%d:%s: ",
r = snprintf(p, left, "[%s] %s:%d:%s: ",
ctx->app_name, file, line, func ? func : "");
if (r < 0 || (unsigned int)r > sizeof(buf))
return;
} else {
r = 0;
}
p = buf + r;
left = sizeof(buf) - r;
}
p += r;
left -= r;
r = vsnprintf(p, left, format, args);
if (r < 0)
@ -129,3 +158,35 @@ void sc_hex_dump(sc_context_t *ctx, const u8 * in, size_t count, char *buf, size
lines++;
}
}
char *
sc_dump_hex(const u8 * in, size_t count)
{
static char dump_buf[0x1000];
size_t ii, size = sizeof(dump_buf) - 0x10;
size_t offs = 0;
memset(dump_buf, 0, sizeof(dump_buf));
if (in == NULL)
return dump_buf;
for (ii=0; ii<count; ii++) {
if (!(ii%16)) {
if (!(ii%48))
snprintf(dump_buf + offs, size - offs, "\n");
else
snprintf(dump_buf + offs, size - offs, " ");
}
snprintf(dump_buf + offs, size - offs, "%02X", *(in + ii));
offs = strlen(dump_buf);
if (offs > size)
break;
}
if (ii<count)
snprintf(dump_buf + offs, sizeof(dump_buf) - offs, "....\n");
return dump_buf;
}

View File

@ -51,6 +51,7 @@ void sc_do_log(struct sc_context *ctx, int type, const char *file, int line, con
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);
void sc_hex_dump(struct sc_context *ctx, const u8 * buf, size_t len, char *out, size_t outlen);
char * sc_dump_hex(const u8 * in, size_t count);
#define SC_FUNC_CALLED(ctx, level) do { \
if (ctx->debug >= level) \