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
|
|
|
|
|
*/
|
|
|
|
|
|
2002-06-14 12:52:56 +00:00
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
2002-04-05 10:44:51 +00:00
|
|
|
|
#include "log.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__
|
2002-11-18 09:05:06 +00:00
|
|
|
|
void error(struct sc_context *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);
|
|
|
|
|
}
|
|
|
|
|
|
2002-11-18 09:05:06 +00:00
|
|
|
|
void debug(struct sc_context *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
|
|
|
|
|
|
2003-01-06 21:46:46 +00:00
|
|
|
|
static int use_color(struct sc_context *ctx, FILE * outf)
|
2002-01-09 01:03:10 +00:00
|
|
|
|
{
|
|
|
|
|
static char *term = NULL;
|
|
|
|
|
static const char *terms[] = { "linux", "xterm", "Eterm" };
|
2003-01-06 21:46:46 +00:00
|
|
|
|
int term_count = sizeof(terms) / sizeof(terms[0]);
|
|
|
|
|
int do_color;
|
2002-01-09 01:03:10 +00:00
|
|
|
|
int i;
|
|
|
|
|
|
2002-01-24 16:02:54 +00:00
|
|
|
|
if (!isatty(fileno(outf)))
|
2002-01-09 01:03:10 +00:00
|
|
|
|
return 0;
|
|
|
|
|
if (term == NULL) {
|
|
|
|
|
term = getenv("TERM");
|
|
|
|
|
if (term == NULL)
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
do_color = 0;
|
|
|
|
|
for (i = 0; i < term_count; i++)
|
|
|
|
|
if (strcmp(terms[i], term) == 0) {
|
|
|
|
|
do_color = 1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return do_color;
|
|
|
|
|
}
|
|
|
|
|
|
2003-01-06 21:46:46 +00:00
|
|
|
|
void sc_do_log(struct sc_context *ctx, int type, const char *file, int line, const char *func, const char *format, ...)
|
|
|
|
|
{
|
|
|
|
|
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)
|
2001-12-13 21:19:11 +00:00
|
|
|
|
{
|
|
|
|
|
FILE *outf = NULL;
|
|
|
|
|
char buf[1024], *p;
|
|
|
|
|
int left, r;
|
2002-01-24 16:02:54 +00:00
|
|
|
|
const char *color_pfx = "", *color_sfx = "";
|
2001-12-13 21:19:11 +00:00
|
|
|
|
|
|
|
|
|
assert(ctx != NULL);
|
2002-01-24 16:02:54 +00:00
|
|
|
|
switch (type) {
|
|
|
|
|
case SC_LOG_TYPE_ERROR:
|
|
|
|
|
if (ctx->log_errors == 0)
|
|
|
|
|
return;
|
|
|
|
|
outf = ctx->error_file;
|
|
|
|
|
break;
|
|
|
|
|
case SC_LOG_TYPE_DEBUG:
|
2002-06-03 15:05:58 +00:00
|
|
|
|
if (ctx->debug == 0)
|
|
|
|
|
return;
|
2002-01-24 16:02:54 +00:00
|
|
|
|
outf = ctx->debug_file;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (outf == NULL)
|
|
|
|
|
return;
|
|
|
|
|
if (use_color(ctx, outf)) {
|
|
|
|
|
color_sfx = "\33[0m";
|
2001-12-19 21:58:04 +00:00
|
|
|
|
switch (type) {
|
2001-12-21 23:34:47 +00:00
|
|
|
|
case SC_LOG_TYPE_ERROR:
|
2002-01-24 16:02:54 +00:00
|
|
|
|
color_pfx = "\33[01;31m";
|
2001-12-13 21:19:11 +00:00
|
|
|
|
break;
|
2001-12-21 23:34:47 +00:00
|
|
|
|
case SC_LOG_TYPE_DEBUG:
|
2002-01-24 16:02:54 +00:00
|
|
|
|
color_pfx = "\33[00;32m";
|
|
|
|
|
break;
|
2001-12-13 21:19:11 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (file != NULL) {
|
2003-01-06 21:46:46 +00:00
|
|
|
|
r = snprintf(buf, sizeof(buf), "%s:%d:%s: ", file, line, func ? func : "");
|
2001-12-13 21:19:11 +00:00
|
|
|
|
if (r < 0)
|
|
|
|
|
return;
|
2002-01-24 16:02:54 +00:00
|
|
|
|
} else
|
|
|
|
|
r = 0;
|
2001-12-13 21:19:11 +00:00
|
|
|
|
p = buf + r;
|
|
|
|
|
left = sizeof(buf) - r;
|
|
|
|
|
|
2002-01-24 16:02:54 +00:00
|
|
|
|
r = vsnprintf(p, left, format, args);
|
|
|
|
|
if (r < 0)
|
2001-12-13 21:19:11 +00:00
|
|
|
|
return;
|
2002-01-24 16:02:54 +00:00
|
|
|
|
p += r;
|
|
|
|
|
left -= r;
|
|
|
|
|
|
|
|
|
|
fprintf(outf, "%s%s%s", color_pfx, buf, color_sfx);
|
2002-03-11 11:40:30 +00:00
|
|
|
|
fflush(outf);
|
2001-12-13 21:19:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-01-06 21:46:46 +00:00
|
|
|
|
void sc_hex_dump(struct sc_context *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];
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|