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
|
|
|
|
*
|
2002-04-05 14:46:44 +00:00
|
|
|
|
* Copyright (C) 2001, 2002 Juha Yrj<EFBFBD>l<EFBFBD> <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-08-29 16:26:59 +00:00
|
|
|
|
#include "internal.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>
|
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
|
2001-12-13 21:19:11 +00:00
|
|
|
|
|
|
|
|
|
#ifndef __GNUC__
|
2005-03-08 20:59:35 +00:00
|
|
|
|
void sc_error(sc_context_t *ctx, const char *format, ...)
|
2001-12-13 21:19:11 +00:00
|
|
|
|
{
|
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
|
|
va_start(ap, format);
|
2003-01-06 21:46:46 +00:00
|
|
|
|
sc_do_log_va(ctx, SC_LOG_TYPE_ERROR, NULL, 0, NULL, format, ap);
|
2001-12-13 21:19:11 +00:00
|
|
|
|
va_end(ap);
|
|
|
|
|
}
|
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
|
void sc_debug(sc_context_t *ctx, const char *format, ...)
|
2001-12-13 21:19:11 +00:00
|
|
|
|
{
|
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
|
|
va_start(ap, format);
|
2003-01-06 21:46:46 +00:00
|
|
|
|
sc_do_log_va(ctx, SC_LOG_TYPE_DEBUG, NULL, 0, NULL, format, ap);
|
2001-12-13 21:19:11 +00:00
|
|
|
|
va_end(ap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
|
void sc_do_log(sc_context_t *ctx, int type, const char *file, int line, const char *func, const char *format, ...)
|
2003-01-06 21:46:46 +00:00
|
|
|
|
{
|
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
|
sc_do_log_va(ctx, type, file, line, func, format, ap);
|
|
|
|
|
va_end(ap);
|
|
|
|
|
}
|
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
|
void sc_do_log_va(sc_context_t *ctx, int type, const char *file, int line, const char *func, const char *format, va_list args)
|
2001-12-13 21:19:11 +00:00
|
|
|
|
{
|
2003-10-22 06:49:59 +00:00
|
|
|
|
int (*display_fn)(sc_context_t *, const char *);
|
2004-12-21 14:01:22 +00:00
|
|
|
|
char buf[1536], *p;
|
|
|
|
|
const char *tag = "";
|
2005-12-23 10:15:17 +00:00
|
|
|
|
int r;
|
2004-09-20 09:47:35 +00:00
|
|
|
|
size_t left;
|
2001-12-13 21:19:11 +00:00
|
|
|
|
|
2005-12-23 10:15:17 +00:00
|
|
|
|
assert(ctx != NULL);
|
2003-10-22 06:49:59 +00:00
|
|
|
|
|
2002-01-24 16:02:54 +00:00
|
|
|
|
switch (type) {
|
|
|
|
|
case SC_LOG_TYPE_ERROR:
|
2005-12-23 10:15:17 +00:00
|
|
|
|
if (!ctx->suppress_errors) {
|
2003-11-20 15:39:18 +00:00
|
|
|
|
display_fn = &sc_ui_display_error;
|
|
|
|
|
tag = "error:";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
/* Fall thru - suppressed errors are logged as
|
|
|
|
|
* debug messages */
|
|
|
|
|
tag = "error (suppressed):";
|
|
|
|
|
type = SC_LOG_TYPE_DEBUG;
|
2003-10-22 06:49:59 +00:00
|
|
|
|
|
2002-01-24 16:02:54 +00:00
|
|
|
|
case SC_LOG_TYPE_DEBUG:
|
2005-12-23 10:15:17 +00:00
|
|
|
|
if (ctx->debug == 0)
|
2002-06-03 15:05:58 +00:00
|
|
|
|
return;
|
2003-10-22 06:49:59 +00:00
|
|
|
|
display_fn = &sc_ui_display_debug;
|
2002-01-24 16:02:54 +00:00
|
|
|
|
break;
|
2003-10-22 06:49:59 +00:00
|
|
|
|
|
|
|
|
|
default:
|
2002-01-24 16:02:54 +00:00
|
|
|
|
return;
|
2001-12-13 21:19:11 +00:00
|
|
|
|
}
|
2003-10-22 06:49:59 +00:00
|
|
|
|
|
2001-12-13 21:19:11 +00:00
|
|
|
|
if (file != NULL) {
|
2006-05-01 10:06:35 +00:00
|
|
|
|
r = snprintf(buf, sizeof(buf), "%s:%d:%s: ", file, line, func ? func : "");
|
|
|
|
|
if (r < 0 || (unsigned int)r > sizeof(buf))
|
2001-12-13 21:19:11 +00:00
|
|
|
|
return;
|
2003-10-22 06:49:59 +00:00
|
|
|
|
} else {
|
2002-01-24 16:02:54 +00:00
|
|
|
|
r = 0;
|
2003-10-22 06:49:59 +00:00
|
|
|
|
}
|
2001-12-13 21:19:11 +00:00
|
|
|
|
p = buf + r;
|
|
|
|
|
left = sizeof(buf) - r;
|
|
|
|
|
|
2006-05-01 10:06:35 +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
|
|
|
|
p += r;
|
|
|
|
|
left -= r;
|
|
|
|
|
|
2005-12-23 10:15:17 +00:00
|
|
|
|
display_fn(ctx, buf);
|
2001-12-13 21:19:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
|
void sc_hex_dump(sc_context_t *ctx, 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;
|
|
|
|
|
|
|
|
|
|
assert(buf != NULL && in != NULL);
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|