分享自己写了个方便打印调试的一点代码,可用于Windows和Linux平台,希望对某些朋友有用。C语言初学者,希望大家多多指点下。

/****************************************************************//**
    @file       debug-inl.h    
    @author     idi0t
    @brief      as printf();
    @time       2013-7-1
    @version    1.0
*********************************************************************/
#ifndef _HAVE_DEBUG_H
#define _HAVE_DEBUG_H

#include 
#include <string.h>
#ifdef WIN32
    #include 
//    #define _MSC_VER            1200    //VC6.0
//    #define _MSC_VER            1400    //VC7.0
#endif

#define PRINT_LEN            2048
#define HAVE_USE_COLOR        1
#define HAVE_SHOW_DBG        1        /* Show filename and line */

#ifdef HAVE_SHOW_DBG
#define SHOW_LINE                do { printf(":%d] ",__LINE__); } while(0)
    #ifdef WIN32
        #define SHOW_FILE        do { \
            printf(strrchr(__FILE__,'\\')?strrchr(__FILE__,'\\')+1:__FILE__); \
        }while(0)
        #if MSC_VER > 1200
            #define SHOW_FUNC    do { printf(__FUNCTION__); } while(0)
        #else
            #define SHOW_FUNC
        #endif
    #else
        #define SHOW_FILE        do { printf(strrchr(__FILE__,'/')+1); } while(0)
        #define SHOW_FUNC        do { printf(__FUNCTION__); } while(0)
    #endif
    #define SHOW_FILE_LINE    do {SHOW_FILE; SHOW_LINE; } while(0)
#else
    #define SHOW_FILE
    #define SHOW_LINE
    #define SHOW_FUNC
    #define SHOW_FILE_LINE
#endif    /* ^HAVE_SHOW_DBG */

#ifdef HAVE_USE_COLOR
    #ifdef WIN32
        #define cRST (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
        #define cBLK 0
        #define cRED (FOREGROUND_RED)
        #define cGRN (FOREGROUND_GREEN)
        #define cBLU (FOREGROUND_BLUE)
        #define cYEL (FOREGROUND_RED | FOREGROUND_GREEN )
        #define cMGN (FOREGROUND_RED | FOREGROUND_BLUE)
        #define cCYA (FOREGROUND_GREEN | FOREGROUND_BLUE)
        #define cNOR (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY)
        #define cGRA (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
        #define cLRD (FOREGROUND_RED | FOREGROUND_INTENSITY)
        #define cLGN (FOREGROUND_GREEN | FOREGROUND_INTENSITY)
        #define cLYL (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY)
        #define cLBL (FOREGROUND_BLUE | FOREGROUND_INTENSITY)
        #define cPIN (FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY)
        #define cLCY (FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY)
        #define cBRI cRST
    #else
        #define cBLK "\x1b[0;30m"
        #define cRED "\x1b[0;31m"
        #define cGRN "\x1b[0;32m"
        #define cYEL "\x1b[0;33m"
        #define cBLU "\x1b[0;34m"
        #define cMGN "\x1b[0;35m"
        #define cCYA "\x1b[0;36m"
        #define cNOR "\x1b[0;37m"
        #define cGRA "\x1b[1;30m"
        #define cLRD "\x1b[1;31m"
        #define cLGN "\x1b[1;32m"
        #define cLYL "\x1b[1;33m"
        #define cLBL "\x1b[1;34m"
        #define cPIN "\x1b[1;35m"
        #define cLCY "\x1b[1;36m"
        #define cBRI "\x1b[1;37m"
        #define cRST "\x1b[0m"
    #endif    /* ^WIN32 */
#else
    #ifdef WIN32
        #define cRST (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
        #define cBLK cRST    /* 黑 */
        #define cRED cRST    /* 红 */
        #define cGRN cRST    /* 绿 */
        #define cYEL cRST    /* 棕 */
        #define cBLU cRST    /* 蓝 */
        #define cMGN cRST    /* 紫 */
        #define cCYA cRST    /* 浅蓝 */
        #define cNOR cRST    /* 浅灰 */
        #define cGRA cRST    /* 灰 */
        #define cLRD cRST    /* 亮红 */
        #define cLGN cRST    /* 亮绿 */
        #define cLYL cRST    /* 亮黄 */
        #define cLBL cRST    /* 亮蓝 */
        #define cPIN cRST    /* 亮紫 */
        #define cLCY cRST    /* 亮浅蓝 */
        #define cBRI cRST    /* 灰 */
        

    #else
        #define cBLK ""        /* 黑 */
        #define cRED ""        /* 红 */
        #define cGRN ""        /* 绿 */
        #define cYEL ""        /* 黄 */
        #define cBLU ""        /* 蓝 */
        #define cMGN ""        /* 紫 */
        #define cCYA ""        /* 浅蓝 */
        #define cNOR ""        /* 浅灰 */
        #define cGRA ""        /* 灰 */
        #define cLRD ""        /* 亮红 */
        #define cLGN ""        /* 亮绿 */
        #define cLYL ""        /* 亮黄 */
        #define cLBL ""        /* 亮蓝 */
        #define cPIN ""        /* 亮紫 */
        #define cLCY ""        /* 亮浅蓝 */
        #define cBRI ""        /* 灰 */
        #define cRST ""
    #endif /* ^WIN32 */
#endif /* ^HAVE_USE_COLOR */

#ifdef WIN32
    #if _MSC_VER > 1200
        #define _win_printf(clr, x...) do { \
            HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); \
            SetConsoleTextAttribute(hOut, clr); \
            printf(x); \
            SetConsoleTextAttribute(hOut, cRST); \
        } while(0)
    #else
        static inline void _win_printf(unsigned short clr, char *fmt, ...)
        {
            HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
            char buf[PRINT_LEN] = {0};
            va_list args;

            va_start(args, fmt);
            _vsnprintf(buf, sizeof(buf)-1, fmt, args);
            va_end(args);

            SetConsoleTextAttribute(hOut, clr);
            printf("%s",buf);
            SetConsoleTextAttribute(hOut, cRST);
        }
        static inline _abt_printf(char *fmt, ...)
        {
            char buf[PRINT_LEN] = {0};
            va_list args;

            va_start(args, fmt);
            _vsnprintf(buf, sizeof(buf)-1, fmt, args);
            va_end(args);
            
            printf("%s",buf);
            abort();
        }
    #endif    /* >MSC_VER */
    #ifndef    NDEBUG
        #if _MSC_VER > 1200
            #define clr_print _win_printf
            #define abt_print(x...) do { \
                    _win_printf(cLRD, x); abort(); \
                } while(0)
            #define dbg_print(x...) do { \
                    _win_printf(cLGN, "[DBG]:"); SHOW_FILE_LINE; printf(x); \
                }while(0)
            #define inf_print(x...) do { \
                    _win_printf(cLYL, "[INF]:"); SHOW_FILE_LINE; printf(x); \
                }while(0)
            #define err_print(x...) do { \
                    _win_printf(cLRD, "[ERR]:"); SHOW_FILE_LINE; printf(x); \
                }while(0)
            #define dbg_printc(clr, x...) do { \
                    _win_printf(cLGRN, "[DBG]:"); SHOW_FILE_LINE; \
                    _win_printf(clr, x); \
                }while(0)
            #define inf_printc(clr, x...) do { \
                    _win_printf(cLYL, "[INF]:"); SHOW_FILE_LINE; \
                    _win_printf(clr, x); \
                }while(0)
            #define err_printc(clr, x...) do { \
                    _win_printf(cLRD, "[ERR]:"); SHOW_FILE_LINE; \
                    _win_printf(clr, x); \
                }while(0)
        #else
            #define clr_print _win_printf
            #define abt_print _abt_printf
            #define dbg_print _win_printf(cLGN, "[DBG]:"), printf
            #define inf_print _win_printf(cLYL, "[INF]:"), printf
            #define err_print _win_printf(cLRD, "[ERR]:"), printf
            #define dbg_printc _win_printf(cLGN, "[DBG]:"), _win_printf
            #define inf_printc _win_printf(cLYL, "[INF]:"), _win_printf
            #define err_printc _win_printf(cLRD, "[ERR]:"), _win_printf
        #endif    /* >_MSC_VER */
    #else
        #if _MSC_VER > 1200
            #define clr_print(x...)
            #define dbg_print(x...)
            #define inf_print(x...)
            #define err_print(x...)
            #define dbg_printc(x...)
            #define inf_printc(x...)
            #define err_printc(x...)
        #else
            #define clr_print
            #define dbg_print
            #define inf_print
            #define err_print
            #define dbg_printc
            #define inf_printc
            #define err_printc
        #endif /* >_MSC_VER */
        
    #endif /* ^NDEBUG */
    
#else
/*
    static inline void _lnx_printf(int clr, char *fmt, ...)
    {
        char buf[PRINT_LEN] = {0};
        va_list args;

        va_start(args, fmt);
        vsnprintf(buf, sizeof(buf)-1, fmt, args);
        va_end(args);
        printf("%s%s",clr,buf);
        printf(cRST);
    }
*/
    #define _lnx_printf(clr, x...) do { \
        printf((clr)); \
        printf(x); \
        printf(cRST); \
    } while(0)
    #ifndef    NDEBUG
        #define clr_print _lnx_printf
        #define abt_print(x...) do { \
                _lnx_printf(cLRD, x); abort(); \
            } while(0)
        #define dbg_print(x...) do { \
                _lnx_printf(cLGN,"[DBG]:"); SHOW_FILE_LINE; \
                printf(x); \
            }while(0)
        #define inf_print(x...) do { \
                _lnx_printf(cLYL,"[INF]:"); SHOW_FILE_LINE; \
                printf(x); \
            }while(0)
        #define err_print(x...) do { \
                _lnx_printf(cLRD,"[ERR]:"); SHOW_FILE_LINE; \
                printf(x); \
            }while(0)
        #define dbg_printc(clr,x...) do { \
                _lnx_printf(cLGN,"[DBG]:"); SHOW_FILE_LINE; \
                _lnx_printf(clr,x); \
            }while(0)
        #define inf_printc(clr,x...) do { \
                _lnx_printf(cLYL,"[INF]:"); SHOW_FILE_LINE; \
                _lnx_printf(clr,x); \
            }while(0)
        #define err_printc(clr,x...) do { \
                _lnx_printf(cLRD,"[ERR]:"); SHOW_FILE_LINE; \
                _lnx_printf(clr,x); \
            }while(0)
    #else
        #define clr_print(x...)
        #define dbg_print(x...)
        #define inf_print(x...)
        #define err_print(x...)
        #define dbg_printc(x...)
        #define inf_printc(x...)
        #define err_printc(x...)
    #endif /* ^NDEBUG */
#endif    /* ^WIN32 */

#ifndef NDEBUG
/*  //for test 
    static inline void debug_test()
    {
        int i=0;
        err_print("hello world\n");
        dbg_print("hello world\n");
        inf_print("hello world\n");
        err_printc(cLRD,"hello world\n");
        dbg_printc(cLGN,"hello world\n");
        inf_printc(cLYL,"hello world\n");

        inf_printc(cRED,"cRED hello world\n");
        inf_printc(cGRN,"cGRN hello world\n");
        inf_printc(cYEL,"cYEL hello world\n");
        inf_printc(cBLU,"cBLU hello world\n");
        inf_printc(cMGN,"cMGN hello world\n");
        inf_printc(cCYA,"cCYA hello world\n");
        inf_printc(cNOR,"cNOR hello world\n");
        inf_printc(cGRA,"cGRA hello world\n");
        inf_printc(cLRD,"cLRD hello world\n");
        inf_printc(cLGN,"cLGN hello world\n");
        inf_printc(cLYL,"cLYL hello world\n");
        inf_printc(cLBL,"cLBL hello world\n");
        inf_printc(cPIN,"cPIN hello world\n");
        inf_printc(cLCY,"cLCY hello world\n");
        inf_printc(cBRI,"cBRI hello world\n");
    }
*/
#endif
#endif /* ! _HAVE_DEBUG_H */