나는 최근 간단한 사용자 정의 싱크 백엔드
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 이런 짓을했습니다
어떤 터미널을 사용합니까? 그들은 단말기가이 색상 코드를 지원해야한다고 여러 번 말합니다 ... – starturtle
코드 시퀀스에서 후행 'm'을 놓쳤습니다. –