#include <stdio.h>
#include <time.h>
#include <stdarg.h>
#include <ctype.h>
#include <string.h>
#define __a2x(ch) (((ch) & 15) + ((ch) > '9'? 9: 0))
#define __x2a(n) ((n & 15) + (n > 9? 55: 48))
#define __print_xdigital__(buffer, r, l) /
do { /
r = (l >> 20) & 15; buffer[3] = __x2a(r); /
r = (l >> 16) & 15; buffer[4] = __x2a(r); /
r = (l >> 12) & 15; buffer[5] = __x2a(r); /
r = (l >> 8) & 15; buffer[6] = __x2a(r); /
r = (l >> 4) & 15; buffer[7] = __x2a(r); /
r = l & 15; buffer[8] = __x2a(r); /
} while (0)
void
xdumpf(FILE *outp, void *buffer, size_t length)
{
char *fmt =
"(0x000000) ";
char line[80], pmap[32];
int count, pos;
register int index, var;
register unsigned char *p = (unsigned char *)buffer;
memset(pmap, 0, sizeof(pmap));
for (index = 0; index < 256; ++index)
if (isprint(index))
pmap[index >> 3] |= 1 << (index & 7);
strcpy(line, fmt);
for (count = 0, pos = 0; length; --length, ++pos)
{
var = *p++;
index = var >> 4;
line[11 + (pos << 1) + pos] = __x2a(index);
index = var & 15;
line[12 + (pos << 1) + pos] = __x2a(index);
line[60 + pos] = (pmap[var >> 3] & (1 << (var & 7)))? var: '.';
if (pos == 15)
{
line[34] = '-';
__print_xdigital__(line, index, count);
fprintf(outp, "%s/n", line);
strcpy(line, fmt);
count += 16;
pos = -1;
}
}
if (pos)
{
line[60 + pos] = '/0';
if (pos > 8)
line[34] = '-';
__print_xdigital__(line, index, count);
fprintf(outp, "%s/n", line);
}
}
void
xdump(char *logfile, void *buffer, size_t length)
{
FILE *fp;
fp = fopen(logfile, "a+");
if (fp)
{
xdumpf(fp, buffer, length);
fclose(fp);
}
}
void
trace(char *logfile, char *format, ...)
{
va_list args;
char tb[32];
time_t _current_t;
FILE *fp;
fp = fopen(logfile, "a+");
if (fp != 0)
{
va_start(args, format);
time(&_current_t);
while (*format == '/n')
{
fputc('/n', fp);
format++;
}
strftime(tb, sizeof(tb), "%m/%d %T", localtime(&_current_t));
fprintf(fp, "[%s] ", tb);
vfprintf(fp, format, args);
va_end(args);
fclose(fp);
}
}