2001-12-13 21:19:11 +00:00
|
|
|
/*
|
2002-01-08 13:56:50 +00:00
|
|
|
* log.c: Miscellaneous logging functions
|
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
|
|
|
|
*/
|
|
|
|
|
2010-03-04 08:14:36 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2001-12-13 21:19:11 +00:00
|
|
|
#include <stdarg.h>
|
2001-12-19 21:58:04 +00:00
|
|
|
#include <stdlib.h>
|
2001-12-13 21:19:11 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
2009-11-17 11:11:05 +00:00
|
|
|
#include <time.h>
|
2002-10-19 14:04:52 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2002-06-14 12:52:56 +00:00
|
|
|
#include <unistd.h>
|
2002-10-19 14:04:52 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_TIME_H
|
2002-01-07 18:32:13 +00:00
|
|
|
#include <sys/time.h>
|
2002-10-19 14:04:52 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_IO_H
|
2002-06-14 12:52:56 +00:00
|
|
|
#include <io.h>
|
|
|
|
#endif
|
2010-02-05 11:21:31 +00:00
|
|
|
#ifdef HAVE_PTHREAD
|
2010-02-04 06:19:52 +00:00
|
|
|
#include <pthread.h>
|
2010-02-05 11:21:31 +00:00
|
|
|
#endif
|
2001-12-13 21:19:11 +00:00
|
|
|
|
2010-03-04 08:14:36 +00:00
|
|
|
#include "internal.h"
|
|
|
|
|
2011-05-08 08:07:03 +00:00
|
|
|
static void sc_do_log_va(sc_context_t *ctx, int level, const char *file, int line, const char *func, const char *format, va_list args);
|
|
|
|
|
2010-03-15 12:17:13 +00:00
|
|
|
void sc_do_log(sc_context_t *ctx, int level, const char *file, int line, const char *func, const char *format, ...)
|
2001-12-13 21:19:11 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
2010-04-14 11:52:27 +00:00
|
|
|
va_start(ap, format);
|
|
|
|
sc_do_log_va(ctx, level, file, line, func, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
sc_do_log_va(ctx, level, NULL, 0, NULL, format, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sc_do_log_va(sc_context_t *ctx, int level, const char *file, int line, const char *func, const char *format, va_list args)
|
2010-04-14 11:52:27 +00:00
|
|
|
{
|
2013-05-02 06:47:08 +00:00
|
|
|
char buf[4096], *p;
|
2005-12-23 10:15:17 +00:00
|
|
|
int r;
|
2004-09-20 09:47:35 +00:00
|
|
|
size_t left;
|
2009-11-17 11:11:05 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
SYSTEMTIME st;
|
|
|
|
#else
|
|
|
|
struct tm *tm;
|
2009-11-17 13:27:15 +00:00
|
|
|
struct timeval tv;
|
|
|
|
char time_string[40];
|
2009-11-17 11:11:05 +00:00
|
|
|
#endif
|
2010-02-04 06:19:52 +00:00
|
|
|
FILE *outf = NULL;
|
|
|
|
int n;
|
2001-12-13 21:19:11 +00:00
|
|
|
|
2010-02-04 06:19:52 +00:00
|
|
|
assert(ctx != NULL);
|
2003-10-22 06:49:59 +00:00
|
|
|
|
2010-03-15 12:17:13 +00:00
|
|
|
if (ctx->debug < level)
|
2002-01-24 16:02:54 +00:00
|
|
|
return;
|
2003-10-22 06:49:59 +00:00
|
|
|
|
2009-11-17 11:11:05 +00:00
|
|
|
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
|
2009-11-17 13:27:15 +00:00
|
|
|
gettimeofday (&tv, NULL);
|
|
|
|
tm = localtime (&tv.tv_sec);
|
2010-01-24 15:25:08 +00:00
|
|
|
strftime (time_string, sizeof(time_string), "%H:%M:%S", tm);
|
2010-12-09 19:52:56 +00:00
|
|
|
r = snprintf(p, left, "0x%lx %s.%03ld ", (unsigned long)pthread_self(), time_string, tv.tv_usec / 1000);
|
2009-11-17 11:11:05 +00:00
|
|
|
#endif
|
|
|
|
p += r;
|
|
|
|
left -= r;
|
|
|
|
|
2001-12-13 21:19:11 +00:00
|
|
|
if (file != NULL) {
|
2012-05-29 17:44:54 +00:00
|
|
|
r = snprintf(p, left, "[%s] %s:%d:%s: ",
|
2007-05-25 20:10:34 +00:00
|
|
|
ctx->app_name, file, line, func ? func : "");
|
2006-05-01 10:06:35 +00:00
|
|
|
if (r < 0 || (unsigned int)r > sizeof(buf))
|
2001-12-13 21:19:11 +00:00
|
|
|
return;
|
2012-05-29 17:44:54 +00:00
|
|
|
}
|
|
|
|
else {
|
2002-01-24 16:02:54 +00:00
|
|
|
r = 0;
|
2012-05-29 17:44:54 +00:00
|
|
|
}
|
2009-11-17 11:11:05 +00:00
|
|
|
p += r;
|
|
|
|
left -= r;
|
2001-12-13 21:19:11 +00:00
|
|
|
|
2010-04-14 11:52:27 +00:00
|
|
|
r = vsnprintf(p, left, format, args);
|
2002-01-24 16:02:54 +00:00
|
|
|
if (r < 0)
|
2001-12-13 21:19:11 +00:00
|
|
|
return;
|
2002-01-24 16:02:54 +00:00
|
|
|
|
2012-05-29 17:44:54 +00:00
|
|
|
#ifdef _WIN32
|
2013-08-02 10:22:42 +00:00
|
|
|
if (ctx->debug_filename) {
|
|
|
|
r = sc_ctx_log_to_file(ctx, ctx->debug_filename);
|
|
|
|
if (r < 0)
|
|
|
|
return;
|
|
|
|
}
|
2012-05-29 17:44:54 +00:00
|
|
|
#endif
|
|
|
|
|
2010-02-04 06:19:52 +00:00
|
|
|
outf = ctx->debug_file;
|
|
|
|
if (outf == NULL)
|
|
|
|
return;
|
|
|
|
|
2010-02-04 06:22:50 +00:00
|
|
|
fprintf(outf, "%s", buf);
|
2010-02-04 06:19:52 +00:00
|
|
|
n = strlen(buf);
|
|
|
|
if (n == 0 || buf[n-1] != '\n')
|
|
|
|
fprintf(outf, "\n");
|
|
|
|
fflush(outf);
|
2010-03-15 12:17:13 +00:00
|
|
|
|
2012-05-29 17:44:54 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
if (ctx->debug_filename) {
|
2013-08-02 10:22:42 +00:00
|
|
|
if (ctx->debug_file && (ctx->debug_file != stderr && ctx->debug_file != stdout)) {
|
|
|
|
fclose(ctx->debug_file);
|
|
|
|
ctx->debug_file = NULL;
|
|
|
|
}
|
2012-05-29 17:44:54 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2010-02-04 06:19:52 +00:00
|
|
|
return;
|
2001-12-13 21:19:11 +00:00
|
|
|
}
|
|
|
|
|
2010-04-14 11:52:27 +00:00
|
|
|
void _sc_debug(struct sc_context *ctx, int level, const char *format, ...)
|
2012-05-29 17:44:54 +00:00
|
|
|
{
|
2010-04-14 11:52:27 +00:00
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
sc_do_log_va(ctx, level, NULL, 0, NULL, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2011-01-04 11:33:03 +00:00
|
|
|
void _sc_log(struct sc_context *ctx, const char *format, ...)
|
2011-01-04 09:17:37 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
sc_do_log_va(ctx, SC_LOG_DEBUG_NORMAL, NULL, 0, NULL, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
2010-04-14 11:52:27 +00:00
|
|
|
|
|
|
|
/* Although not used, we need this for consistent exports */
|
|
|
|
void sc_hex_dump(struct sc_context *ctx, int level, const u8 * in, size_t count, char *buf, size_t len)
|
2001-12-13 21:19:11 +00:00
|
|
|
{
|
2001-12-30 21:17:34 +00:00
|
|
|
char *p = buf;
|
2001-12-13 21:19:11 +00:00
|
|
|
int lines = 0;
|
|
|
|
|
2010-03-15 12:17:13 +00:00
|
|
|
assert(ctx != NULL);
|
|
|
|
|
|
|
|
if (ctx->debug < level)
|
|
|
|
return;
|
|
|
|
|
2013-08-02 12:21:02 +00:00
|
|
|
assert(buf != NULL && (in != NULL || count == 0));
|
2001-12-13 21:19:11 +00:00
|
|
|
buf[0] = 0;
|
2003-01-06 21:46:46 +00:00
|
|
|
if ((count * 5) > len)
|
2001-12-13 21:19:11 +00:00
|
|
|
return;
|
2003-01-06 21:46:46 +00:00
|
|
|
while (count) {
|
2001-12-13 21:19:11 +00:00
|
|
|
char ascbuf[17];
|
2003-05-30 08:54:42 +00:00
|
|
|
size_t i;
|
2001-12-13 21:19:11 +00:00
|
|
|
|
|
|
|
for (i = 0; i < count && i < 16; i++) {
|
|
|
|
sprintf(p, "%02X ", *in);
|
|
|
|
if (isprint(*in))
|
|
|
|
ascbuf[i] = *in;
|
|
|
|
else
|
|
|
|
ascbuf[i] = '.';
|
|
|
|
p += 3;
|
|
|
|
in++;
|
|
|
|
}
|
|
|
|
count -= i;
|
|
|
|
ascbuf[i] = 0;
|
|
|
|
for (; i < 16 && lines; i++) {
|
|
|
|
strcat(p, " ");
|
|
|
|
p += 3;
|
|
|
|
}
|
|
|
|
strcat(p, ascbuf);
|
|
|
|
p += strlen(p);
|
|
|
|
sprintf(p, "\n");
|
|
|
|
p++;
|
2001-12-19 21:58:04 +00:00
|
|
|
lines++;
|
2001-12-13 21:19:11 +00:00
|
|
|
}
|
|
|
|
}
|
2009-11-17 11:11:05 +00:00
|
|
|
|
|
|
|
char *
|
|
|
|
sc_dump_hex(const u8 * in, size_t count)
|
|
|
|
{
|
|
|
|
static char dump_buf[0x1000];
|
|
|
|
size_t ii, size = sizeof(dump_buf) - 0x10;
|
2013-06-13 18:13:01 +00:00
|
|
|
size_t offs = 0;
|
2009-11-17 11:11:05 +00:00
|
|
|
|
|
|
|
memset(dump_buf, 0, sizeof(dump_buf));
|
|
|
|
if (in == NULL)
|
2013-06-13 18:13:01 +00:00
|
|
|
return dump_buf;
|
2009-11-17 11:11:05 +00:00
|
|
|
|
|
|
|
for (ii=0; ii<count; ii++) {
|
|
|
|
if (!(ii%16)) {
|
2013-06-13 18:13:01 +00:00
|
|
|
if (!ii)
|
|
|
|
snprintf(dump_buf + offs, size - offs, ":\n");
|
|
|
|
else if (!(ii%48))
|
2009-11-17 11:11:05 +00:00
|
|
|
snprintf(dump_buf + offs, size - offs, "\n");
|
|
|
|
else
|
|
|
|
snprintf(dump_buf + offs, size - offs, " ");
|
2013-06-13 18:13:01 +00:00
|
|
|
offs = strlen(dump_buf);
|
2009-11-17 11:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(dump_buf + offs, size - offs, "%02X", *(in + ii));
|
2013-06-13 18:13:01 +00:00
|
|
|
offs += 2;
|
2009-11-17 11:11:05 +00:00
|
|
|
|
|
|
|
if (offs > size)
|
2013-06-13 18:13:01 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-11-17 11:11:05 +00:00
|
|
|
|
2013-06-13 18:13:01 +00:00
|
|
|
if (ii<count)
|
|
|
|
snprintf(dump_buf + offs, sizeof(dump_buf) - offs, "....\n");
|
2009-11-17 11:11:05 +00:00
|
|
|
|
|
|
|
return dump_buf;
|
|
|
|
}
|