简单的trace函数

#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);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值