zoukankan      html  css  js  c++  java
  • 控制台格式化输出设置

    //header.h

    #pragma once
    #include <map>
    #include <time.h>

    #ifdef WIN32
    # define GTEST_OS_WINDOWS 1
    #include <windows.h>
    #else
    #include <unistd.h>
    #include <stdarg.h>
    #endif

    typedef unsigned long int uint32; /* Unsigned 32 bit value */
    typedef unsigned short uint16; /* Unsigned 16 bit value */
    typedef unsigned char uint8; /* Unsigned 8 bit value */
    typedef signed long int int32; /* Signed 32 bit value */
    typedef signed short int16; /* Signed 16 bit value */
    typedef signed char int8; /* Signed 8 bit value */

    enum LOGTAG {
    LOG_NOUSE,
    LOG_PRINT,
    LOG_DEBUG,
    LOG_ERROR,
    LOG_INFO
    };

    #define PRINT(...) Print(LOG_PRINT, __FILE__, __LINE__, __VA_ARGS__)
    #define DEBUG(...) Print(LOG_DEBUG, __FILE__, __LINE__, __VA_ARGS__)
    #define ERRRR(...) Print(LOG_ERROR, __FILE__, __LINE__, __VA_ARGS__)

    #if GTEST_OS_WINDOWS
    inline uint16 GetColorAttr(LOGTAG tag)
    {
    switch (tag) {
    case LOG_ERROR: return FOREGROUND_RED;
    case LOG_PRINT: return FOREGROUND_GREEN;
    case LOG_DEBUG: return FOREGROUND_RED | FOREGROUND_GREEN; //yellow
    case LOG_INFO: return FOREGROUND_INTENSITY|BACKGROUND_BLUE;
    default: return FOREGROUND_GREEN;
    }
    }
    #else
    inline const char* GetColorCode(LOGTAG tag)
    {
    switch (tag) {
    case LOG_ERROR: return "1";
    case LOG_PRINT: return "2";
    case LOG_DEBUG: return "3";
    default: return "2";
    };
    }
    #endif

    inline void ColoredPrintf(LOGTAG tag, const char* fmt, ...)
    {
    va_list args;
    va_start(args, fmt);
    #if GTEST_OS_WINDOWS
    const HANDLE stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);

    // Gets the current text color.
    CONSOLE_SCREEN_BUFFER_INFO buffer_info;
    GetConsoleScreenBufferInfo(stdout_handle, &buffer_info);
    const uint16 old_color_attrs = buffer_info.wAttributes;

    // We need to flush the stream buffers into the console before each
    // SetConsoleTextAttribute call lest it affect the text that is already
    // printed but has not yet reached the console.
    fflush(stdout);
    SetConsoleTextAttribute(stdout_handle, GetColorAttr(tag)| FOREGROUND_INTENSITY);
    vprintf(fmt, args);

    fflush(stdout);
    // Restores the text color.
    SetConsoleTextAttribute(stdout_handle, old_color_attrs);
    #else
    printf("33[0;3%sm", GetColorCode(tag));
    vprintf(fmt, args);
    printf("33[m"); // Resets the terminal to default.
    #endif
    va_end(args);
    }

    inline void Print(LOGTAG outputTag, const char * fileName, int lineNum, const char * fmt, ...)
    {
    std::string fileNameStr = fileName;
    int n = fileNameStr.find_last_of('\');
    fileNameStr = fileNameStr.substr(n+1, fileNameStr.length());

    time_t now = time(NULL);
    char timeStr[128];
    strftime(timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S", localtime(&now));

    printf("#");
    const char * tagStr;
    switch(outputTag)
    {
    case LOG_PRINT:
    tagStr = "Print";
    break;
    case LOG_DEBUG:
    tagStr = "Debug";
    break;
    case LOG_ERROR:
    tagStr = "Error";
    break;
    default:
    tagStr = "Otput";
    break;
    }
    ColoredPrintf(outputTag, tagStr);
    printf("#");
    printf(" %s [%s:%d] ", timeStr, fileNameStr.c_str(), lineNum);

    char tmp[1024];
    va_list args;
    va_start(args, fmt);
    int printed = vsnprintf(tmp, sizeof(tmp)-1, fmt, args);
    va_end(args);
    tmp[sizeof(tmp)-1] = '';
    //puts(tmp);
    printf("%s", tmp);
    /* from glic printf.c
    va_list arg;
    va_start(arg, fmt);
    int done = vfprintf(stdout, fmt, arg);
    va_end(arg);
    */
    }

    1.改变整个控制台的颜色
    用 system("color 0A");
    其中color后面的0是背景色代号,A是前景色代号。各颜色代码如下:
    0=黑色
    1=蓝色
    2=绿色
    3=湖蓝色
    4=红色
    5=紫色
    6=黄色
    7=白色
    8=灰色
    9=淡蓝色
    A=淡绿色
    B=淡浅绿色
    C=淡红色
    D=淡紫色
    E=淡黄色
    F=亮白色
    2.改变下一个输出或者输入字体和背景的颜色

    采用SetConsoleTextAttribute函数,如
    White on Black:
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |
    FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    Red on Black:
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |
    FOREGROUND_RED);
    Green on Black:
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |
    FOREGROUND_GREEN);
    Yellow on Black:
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |
    FOREGROUND_RED | FOREGROUND_GREEN);
    Blue on Black:
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |
    FOREGROUND_BLUE);
    Magenta on Black:
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |
    FOREGROUND_RED | FOREGROUND_BLUE);
    Cyan on Black:
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |
    FOREGROUND_GREEN | FOREGROUND_BLUE);
    Black on Gray:
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY |
    BACKGROUND_INTENSITY);
    Black on White:
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY |
    FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
    Red on White:
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY |
    FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE |
    FOREGROUND_RED);
    等等。
    需要引入头文件:
    windows.h
    函数原型:
    BOOL SetConsoleTextAttribute( HANDLE hConsoleOutput, WORD wAttributes );
    wAttributes 的取值含义对应如下:
    Attribute Meaning
    FOREGROUND_BLUE Text color contains blue.
    FOREGROUND_GREEN Text color contains green.
    FOREGROUND_RED Text color contains red.
    FOREGROUND_INTENSITY Text color is intensified.
    BACKGROUND_BLUE Background color contains blue.
    BACKGROUND_GREEN Background color contains green.
    BACKGROUND_RED Background color contains red.
    BACKGROUND_INTENSITY Background color is intensified.
    COMMON_LVB_LEADING_BYTE Leading byte.
    COMMON_LVB_TRAILING_BYTE Trailing byte.
    COMMON_LVB_GRID_HORIZONTAL Top horizontal.
    COMMON_LVB_GRID_LVERTICAL Left vertical.
    COMMON_LVB_GRID_RVERTICAL Right vertical.
    COMMON_LVB_REVERSE_VIDEO Reverse foreground and background attributes.
    COMMON_LVB_UNDERSCORE Underscore.

  • 相关阅读:
    Linux如何修改命令提示符
    Linux命令详解-install
    Linux命令详解-info
    Linux命令详解-man
    Linux命令详解-printf
    Linux命令详解-echo
    Linux命令详解-whatis
    Linux命令详解-file
    Linux命令详解-help
    Linux命令详解-type
  • 原文地址:https://www.cnblogs.com/xiao0913/p/3949847.html
Copyright © 2011-2022 走看看