• 打印调试之简单彩色输出文件名和行号等调试信息


    分享自己写了个方便打印调试的一点代码,可用于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 <stdio.h>
    #include <string.h>
    #ifdef WIN32
        #include <windows.h>
    //    #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
    ");
            dbg_print("hello world
    ");
            inf_print("hello world
    ");
            err_printc(cLRD,"hello world
    ");
            dbg_printc(cLGN,"hello world
    ");
            inf_printc(cLYL,"hello world
    ");
    
            inf_printc(cRED,"cRED hello world
    ");
            inf_printc(cGRN,"cGRN hello world
    ");
            inf_printc(cYEL,"cYEL hello world
    ");
            inf_printc(cBLU,"cBLU hello world
    ");
            inf_printc(cMGN,"cMGN hello world
    ");
            inf_printc(cCYA,"cCYA hello world
    ");
            inf_printc(cNOR,"cNOR hello world
    ");
            inf_printc(cGRA,"cGRA hello world
    ");
            inf_printc(cLRD,"cLRD hello world
    ");
            inf_printc(cLGN,"cLGN hello world
    ");
            inf_printc(cLYL,"cLYL hello world
    ");
            inf_printc(cLBL,"cLBL hello world
    ");
            inf_printc(cPIN,"cPIN hello world
    ");
            inf_printc(cLCY,"cLCY hello world
    ");
            inf_printc(cBRI,"cBRI hello world
    ");
        }
    */
    #endif
    #endif /* ! _HAVE_DEBUG_H */
  • 相关阅读:
    (转)WinForm 开发框架【加载DLL模式】
    (转)精通正则表达式(元字符)
    (转)svn入门指南
    (转)ERP 高级查询(Advanced Query)设计与实现 SQL语句解析成LLBL Gen ORM代码
    (转)工作了一个星期各位一定累了吧,那我们一起来表单验证一番吧!
    (转)ASP.net Web API综合示例
    (转)搭建SVN环境
    (转)使用T4模板批量生成代码
    (转)Sql Server参数化查询之where in和like实现详解
    scau ooxx numbers
  • 原文地址:https://www.cnblogs.com/cers/p/c_debug_color.html
Copyright © 2020-2023  润新知