__dbg.h

#ifndef __HSS_DBG_HSS__
#define __HSS_DBG_HSS__

/**************************************************************************************************\
*  2009-06-28

    调试信息输出的宏版

    (1) 一定起作用的宏
        __trace, __trace_file, __trace_fmt, __trace_end

    (2) 需要定义 __dbg__才有作用的宏
        __if, _if, __trace_dbg, __trace_file_dbg, __trace_fmt_dbg, __trace_end_dbg

        (宏名dbg在后面,是为了在搜索__trace的时候可以找到所有的宏)

    (3) 用法示例:

      __trace "a=%d b=%d\r\n", a, b); __trace_end

      __trace_file "a=%d b=%d\r\n", a, b); __trace_end

      //注意:这个宏有参数 ,并且在宏的参数括号的后面,不能有逗号
      __trace_fmt(__FILE__, __LINE__, TRUE) "a=%d b=%d", a, b); __trace_end

      其中__FILE__可以替换为其他文件名,若是全路径会只显示文件名

    (4) __if 是长格式版,显示条件的内容

    (5) _if 是短格式版,不显示条件的内容,仅显示条件的值


\**************************************************************************************************/
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define __trace_end    if (__sztrace && __sztrace[0])        \
                        OutputDebugStringA(__sztrace);    \
                    }

#define __trace    \
            {    \
                char __sztrace[260] = {0};    \
                _snprintf(__sztrace, sizeof(__sztrace), 

#define __trace_file    \
            {    \
                char __sztrace[260] = {0};    \
                int _n = _snprintf(__sztrace, sizeof(__sztrace)-1, "%08X %s/%d ", GetCurrentThreadId(), strrchr(__FILE__, '\\') + 1, __LINE__);    \
                _snprintf(__sztrace+_n, sizeof(__sztrace)-_n-1,

#define __trace_fmt(file, line, error)     \
            {                            \
                char __sztrace[260] = {0};    \
                int __n = 0;            \
                if (file && ((char*)file)[0])    \
                {                        \
                    LPCTSTR __p = strrchr(file, '\\');    \
                    if (__p == 0)        \
                        __p = file;        \
                    __n += _snprintf(__sztrace+__n, sizeof(__sztrace)-1-__n, "%s ", __p);    \
                }                        \
                if (line)                \
                {                        \
                    __n += _snprintf(__sztrace+__n, sizeof(__sztrace)-1-__n, "/%d ", line);    \
                }                        \
                if (error)                \
                {                        \
                    int se = GetLastError();    \
                    LPSTR __lpMsgbuf = 0;    \
                    if (FormatMessage(    \
                        FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,    \
                        NULL,            \
                        se,            \
                        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),        \
                        (LPSTR) & __lpMsgbuf,    \
                        0,                \
                        NULL)            \
                        )                \
                    {                    \
                        if (__lpMsgbuf && __lpMsgbuf[0])    \
                        {                \
                            __n += _snprintf(__sztrace+__n, sizeof(__sztrace)-1-__n, "%s (%d) ", __lpMsgbuf, se);    \
                        }                \
                        if (__lpMsgbuf)    \
                        {    \
                            LocalFree( __lpMsgbuf );    \
                        }    \
                    }                    \
                }                        \
                __n += _snprintf(__sztrace+__n, sizeof(__sztrace)-1-__n, 

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//sample : __trace_bytes("profix", pData, cbData) __trace_end;
#define __trace_bytes(Profix, pData, cbData)    \
        {                                        \
            int __size = cbData * 3 + (Profix == 0 ? 0 : strlen(Profix)) + 32;    \
            char* __sztrace = (char*)alloca(__size);    \
            LPBYTE __p = (LPBYTE)pData;            \
            if (__sztrace)                        \
            {                                    \
                int __n = _snprintf(__sztrace, __size-1, "%s", Profix);    \
                for (int __l = 0 ; __l < cbData ; __l ++)    \
                {                                \
                    DWORD __d = (DWORD)__p[__l];    \
                    __n += _snprintf(__sztrace + __n, __size-1-__n, "%02X ", __d);    \
                }                                \
                __n += _snprintf(__sztrace + __n, __size-1-__n, "(%d)", cbData);    \
                __sztrace[__n] = 0;                \
                __sztrace[__size-1] = 0;        \
            }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#define __FL__        __trace_file "\r\n");__trace_end;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#ifdef __dbg__

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#define __trace_end_dbg        __trace_end

#define __trace_dbg            __trace

#define __trace_file_dbg    __trace_file

#define __trace_fmt_dbg        __trace_fmt


/**************************************************************************************************\
 *  2009-06-28
 显示程序的分支和判断的值
 长格式:__if 显示判断的条件的文本
 短格式:_if 不显示判断的条件的文本
\**************************************************************************************************/

#define __if_false(x)        \
    {                \
        char __szif[256] = {0};    \
        if (!(x))        \
        {            \
            _snprintf(__szif, sizeof(__szif)-1, "%s/%d %d == ("#x")\r\n", strrchr(__FILE__, '\\') + 1, __LINE__, (x));    \
            OutputDebugStringA(__szif);    \
        }            \
    }                \
    if (x)

///////////////////////////////////////////////////////////////////////////////////////////////////

#define __if_true(x)        \
    {                \
        char __szif[256] = {0};    \
        if (x)        \
        {            \
            _snprintf(__szif, sizeof(__szif)-1, "%s/%d %d == ("#x")\r\n", strrchr(__FILE__, '\\') + 1, __LINE__, (x));    \
            OutputDebugStringA(__szif);    \
        }            \
    }                \
    if (x)

///////////////////////////////////////////////////////////////////////////////////////////////////

#define __if(x)        \
    {                \
        char __szif[256] = {0};    \
        if (x)        \
        {            \
            _snprintf(__szif, sizeof(__szif)-1, "%s/%d %d == ("#x")\r\n", strrchr(__FILE__, '\\') + 1, __LINE__, (x));    \
        }            \
        else        \
        {            \
            _snprintf(__szif, sizeof(__szif)-1, "%s/%d %d == ("#x")\r\n", strrchr(__FILE__, '\\') + 1, __LINE__, (x));    \
        }            \
        OutputDebugStringA(__szif);    \
    }                \
    if (x)

///////////////////////////////////////////////////////////////////////////////////////////////////

#define _if(x)        \
    {                \
        char __szif[256] = {0};    \
        if (x)        \
        {            \
            _snprintf(__szif, sizeof(__szif)-1, "%s/%d %d \r\n", strrchr(__FILE__, '\\') + 1, __LINE__, (x));    \
        }            \
        else        \
        {            \
            _snprintf(__szif, sizeof(__szif)-1, "%s/%d %d \r\n", strrchr(__FILE__, '\\') + 1, __LINE__, (x));    \
        }            \
        OutputDebugStringA(__szif);    \
    }                \
    if (x)

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#else

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#define _if(x)                if(x)
#define __if(x)                if(x)
#define __if_true(x)        if(x)
#define __if_false(x)        if(x)
#define __trace_end_dbg        }
#define __trace_dbg            if (0) { (
#define __trace_file_dbg    if (0) { (
#define __trace_fmt_dbg        if (0) { (

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#endif

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#endif
posted @ 2016-12-01 10:44  shansong.huang  阅读(485)  评论(0编辑  收藏  举报