debugPrint-1.1

现在把改良后的debug调试程序放在这里。针对以前曾经给出的唆实现的功能并没有什么变化, 只是把程序修改得更加简洁罢了。或许这也是体现自己的成长之路的吧, 可以看出这几个版本之间, 我个人的编程风格的一些变化, 以及自己的设计哲学的逐步形成吧。

各位同学, 有什么高见尽请提出, 我一直都希望大家可以多多交流, 这也可以更快、更直接地促进我们的进步……

好了, 先说头文件吧, 还是和以前的版本一样, 利用ifdef来控制只在debug版本打印出相应的调试信息:

#if _WIN32 || _WIN64

#define inline __inline

#endif

extern void dbgPrintA(const char* format, ... );

#ifdef _DEBUG

#define dbgPrint    dbgPrintA

#else

#define dbgPrint

#endif /* _DEBUG */

在内部我们添加两个函数来处理内部的过程:

static int _debugInternalA(const char* _format, va_list _argList);

static int _debugPrintA(const char* _debugSTR);

这样整个内部实现如下:

static int g_isTake = 0;

void dbgPrintA(const char* format, ... ) {

    va_list argList = NULL;

    __try {

        while (g_isTake) Sleep(10);

        g_isTake = 1;

        va_start(argList, format);

        _debugInternalA(format, argList);

    } __finally {

        va_end(argList);

        g_isTake = 0;

    }

}

static inline int 

_debugInternalA(const char* _format, va_list _argList) {

    char dbgSTR[1024] = {0};

    if (NULL == _format || NULL == _argList) return 0;

    _vsnprintf(dbgSTR, 1024, _format, _argList);

    return _debugPrintA(dbgSTR);

}

static inline int 

_debugPrintA(const char* _debugSTR) {

    FILE* _File = NULL;

    SYSTEMTIME st = {0};

    if (NULL == _debugSTR) return 0;

    _File = fopen("debug.log""a+");

    if (NULL == _File)  return 0;

    GetLocalTime(&st);

    fprintf(_File, "[%.2d:%.2d:%.2d:%.3d] - %s/n"

        st.wHour, st.wMinute, st.wSecond, 

        st.wMilliseconds, _debugSTR);

    fflush(_File);

    if (NULL != _File) {

        fclose(_File); _File = NULL;

    }

    return 1;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值