2016-07-11 65 views
4

我想在linux下为boost :: log添加彩色日志输出。我读the following,我想这:如何添加颜色编码到boost :: log控制台输出?

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

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

,但它给了我下面的结果:

[2016年7月11日17:23:16.328435] [0x00007f15f03d6780] [错误] [1; 31This 是一个错误日志。

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

+1

你使用什么终端?他们多次声明,终端必须支持这些颜色代码... – starturtle

+2

您错过了代码序列中的尾随'm'。 –

回答

8

使用Boost.Log自定义输出的正确方法是使用formatters。要设置格式化程序,您必须为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::info: 
      strm << "\033[32m"; 
      break; 
     case logging::trivial::severity::warning: 
      strm << "\033[33m"; 
      break; 
     case logging::trivial::severity::error: 
     case logging::trivial::severity::fatal: 
      strm << "\033[31m"; 
      break; 
     default: 
      break; 
     } 
    } 

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

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

sink->set_formatter(&coloring_formatter); 
+0

如何在上面的代码中将* TimeStamp *和* ThreadID *添加到日志消息的格式中?我希望结果类似于默认的简单日志记录格式。当我使用日志记录文件时,我只写了'keywords :: format =“[%TimeStamp%] [%ThreadID%] [%Severity%]:%Message%”'。 – bobeff

+0

我刚刚发布上述评论为[新问题](http://stackoverflow.com/questions/38618094/how-to-output-timestamp-and-threadid-attributes-with-custom-boostlog-formatter)。 – bobeff

2

我最近有一个简单的自定义接收后端

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); 
01做到了这一点