如何向 boost::log 控制台输出添加颜色编码?

How to add color coding to boost::log console output?

我正在尝试在 linux 下为 boost::log 添加彩色日志输出。我读了 the following 并尝试了这个:

#define MY_LOG_ERROR() BOOST_LOG_TRIVIAL(error) << "3[1;31"

MY_LOG_ERROR() << "This is an error log."

但它给了我以下结果:

[2016-07-11 17:23:16.328435] [0x00007f15f03d6780] [error] [1;31This is an error log.

如何正确地将彩色日志输出添加到 boost::log?

使用 Boost.Log 自定义输出的正确方法是使用 formatters. To set a formatter you will have to set up a sink for that as described here,但您可以继续使用 BOOST_LOG_TRIVIAL 宏来生成日志记录。

格式化程序的好处是您可以访问格式化程序中的日志记录属性,例如严重性级别。例如,您可以使用严重级别来选择控制台上格式化日志记录的颜色。

void coloring_formatter(
    logging::record_view const& rec, logging::formatting_ostream& strm)
{
    auto severity = rec[logging::trivial::severity];
    if (severity)
    {
        // Set the color
        switch (severity.get())
        {
        case logging::trivial::severity_level::info:
            strm << "3[32m";
            break;
        case logging::trivial::severity_level::warning:
            strm << "3[33m";
            break;
        case logging::trivial::severity_level::error:
        case logging::trivial::severity_level::fatal:
            strm << "3[31m";
            break;
        default:
            break;
        }
    }

    // Format the message here...
    strm << rec[logging::expressions::smessage];

    if (severity)
    {
        // Restore the default color
        strm << "3[0m";
    }
}

sink->set_formatter(&coloring_formatter);

我最近用一个简单的自定义接收器后端完成了这项工作

coloured_console_sink.h

#pragma once
#include <boost/log/sinks/basic_sink_backend.hpp>

class coloured_console_sink : public boost::log::sinks::basic_formatted_sink_backend<char, boost::log::sinks::synchronized_feeding>
{
public:
    static void consume(boost::log::record_view const& rec, string_type const& formatted_string);
};

coloured_console_sink.cpp

#include "coloured_console_sink.h"
#include <iostream>
#include <windows.h>
#include <boost/log/trivial.hpp>
#include <boost/log/attributes/value_extraction.hpp>
#include <boost/log/attributes/attribute_value.hpp>

WORD get_colour(boost::log::trivial::severity_level level)
{
    switch (level)
    {
        case boost::log::trivial::trace: return 0x08;
        case boost::log::trivial::debug: return 0x07;
        case boost::log::trivial::info: return 0x0F;
        case boost::log::trivial::warning: return 0x0D;
        case boost::log::trivial::error: return 0x0E;
        case boost::log::trivial::fatal: return 0x0C;
        default: return 0x0F;
    }
}

void coloured_console_sink::consume(boost::log::record_view const& rec, string_type const& formatted_string)
{
    auto level = rec.attribute_values()["Severity"].extract<boost::log::trivial::severity_level>();
    auto hstdout = GetStdHandle(STD_OUTPUT_HANDLE);

    CONSOLE_SCREEN_BUFFER_INFO csbi;
    GetConsoleScreenBufferInfo(hstdout, &csbi);

    SetConsoleTextAttribute(hstdout, get_colour(level.get()));
    std::cout << formatted_string << std::endl;
    SetConsoleTextAttribute(hstdout, csbi.wAttributes);
}

用法

typedef boost::log::sinks::synchronous_sink<coloured_console_sink> coloured_console_sink_t;
auto coloured_console_sink = boost::make_shared<coloured_console_sink_t>();

boost::log::core::get()->add_sink(coloured_console_sink);