2019-06-03 17:09:16 -04:00
|
|
|
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
2016-10-12 08:10:10 -04:00
|
|
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2018-04-28 18:31:09 -04:00
|
|
|
#include "spdlog/common.h"
|
2018-07-07 06:38:15 -04:00
|
|
|
#include "spdlog/details/console_globals.h"
|
2018-07-07 07:04:31 -04:00
|
|
|
#include "spdlog/details/null_mutex.h"
|
2018-04-28 18:31:09 -04:00
|
|
|
#include "spdlog/sinks/sink.h"
|
2016-10-12 08:10:10 -04:00
|
|
|
|
2018-04-15 18:07:22 -04:00
|
|
|
#include <memory>
|
2018-04-20 06:20:19 -04:00
|
|
|
#include <mutex>
|
2016-10-12 08:10:10 -04:00
|
|
|
#include <string>
|
2018-02-05 05:20:57 -05:00
|
|
|
#include <unordered_map>
|
2016-10-12 08:10:10 -04:00
|
|
|
#include <wincon.h>
|
|
|
|
|
2018-03-17 06:47:46 -04:00
|
|
|
namespace spdlog {
|
2018-04-20 06:20:19 -04:00
|
|
|
namespace sinks {
|
|
|
|
/*
|
2018-07-21 16:48:07 -04:00
|
|
|
* Windows color console sink. Uses WriteConsoleA to write to the console with
|
|
|
|
* colors
|
2018-04-20 06:20:19 -04:00
|
|
|
*/
|
2019-06-18 05:32:51 -04:00
|
|
|
template<typename ConsoleMutex>
|
2018-04-20 06:20:19 -04:00
|
|
|
class wincolor_sink : public sink
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
const WORD BOLD = FOREGROUND_INTENSITY;
|
|
|
|
const WORD RED = FOREGROUND_RED;
|
|
|
|
const WORD GREEN = FOREGROUND_GREEN;
|
|
|
|
const WORD CYAN = FOREGROUND_GREEN | FOREGROUND_BLUE;
|
|
|
|
const WORD WHITE = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
|
|
|
|
const WORD YELLOW = FOREGROUND_RED | FOREGROUND_GREEN;
|
|
|
|
|
2019-06-18 05:32:51 -04:00
|
|
|
wincolor_sink(HANDLE out_handle, color_mode mode);
|
2019-05-12 08:32:54 -04:00
|
|
|
~wincolor_sink() override;
|
2018-04-20 06:20:19 -04:00
|
|
|
|
|
|
|
wincolor_sink(const wincolor_sink &other) = delete;
|
|
|
|
wincolor_sink &operator=(const wincolor_sink &other) = delete;
|
|
|
|
|
|
|
|
// change the color for the given level
|
2019-05-12 16:08:14 -04:00
|
|
|
void set_color(level::level_enum level, WORD color);
|
|
|
|
void log(const details::log_msg &msg) final override;
|
|
|
|
void flush() final override;
|
|
|
|
void set_pattern(const std::string &pattern) override final;
|
2019-05-12 08:32:54 -04:00
|
|
|
void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) override final;
|
2019-05-24 05:33:14 -04:00
|
|
|
void set_color_mode(color_mode mode);
|
2018-07-14 09:21:53 -04:00
|
|
|
|
2018-04-20 06:20:19 -04:00
|
|
|
private:
|
2019-05-12 16:08:14 -04:00
|
|
|
using mutex_t = typename ConsoleMutex::mutex_t;
|
2018-04-20 06:20:19 -04:00
|
|
|
HANDLE out_handle_;
|
2018-06-10 15:59:17 -04:00
|
|
|
mutex_t &mutex_;
|
2019-05-09 07:16:38 -04:00
|
|
|
bool should_do_colors_;
|
2018-04-20 06:20:19 -04:00
|
|
|
std::unordered_map<level::level_enum, WORD, level::level_hasher> colors_;
|
2019-05-12 08:32:54 -04:00
|
|
|
|
2019-05-12 16:08:14 -04:00
|
|
|
// set color and return the orig console attributes (for resetting later)
|
2019-05-12 08:32:54 -04:00
|
|
|
WORD set_console_attribs(WORD attribs);
|
|
|
|
// print a range of formatted message to console
|
|
|
|
void print_range_(const fmt::memory_buffer &formatted, size_t start, size_t end);
|
2018-04-20 06:20:19 -04:00
|
|
|
};
|
|
|
|
|
2019-06-18 05:32:51 -04:00
|
|
|
template<typename ConsoleMutex>
|
|
|
|
class wincolor_stdout_sink : public wincolor_sink<ConsoleMutex>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit wincolor_stdout_sink(color_mode mode = color_mode::automatic);
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename ConsoleMutex>
|
|
|
|
class wincolor_stderr_sink : public wincolor_sink<ConsoleMutex>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit wincolor_stderr_sink(color_mode mode = color_mode::automatic);
|
|
|
|
};
|
|
|
|
|
|
|
|
using wincolor_stdout_sink_mt = wincolor_stdout_sink<details::console_mutex>;
|
|
|
|
using wincolor_stdout_sink_st = wincolor_stdout_sink<details::console_nullmutex>;
|
2018-04-20 06:20:19 -04:00
|
|
|
|
2019-06-18 05:32:51 -04:00
|
|
|
using wincolor_stderr_sink_mt = wincolor_stderr_sink<details::console_mutex>;
|
|
|
|
using wincolor_stderr_sink_st = wincolor_stderr_sink<details::console_nullmutex>;
|
2018-04-20 06:20:19 -04:00
|
|
|
|
|
|
|
} // namespace sinks
|
2018-03-17 06:47:46 -04:00
|
|
|
} // namespace spdlog
|
2019-05-12 09:11:35 -04:00
|
|
|
|
|
|
|
#ifdef SPDLOG_HEADER_ONLY
|
|
|
|
#include "wincolor_sink-inl.h"
|
2019-05-24 05:33:14 -04:00
|
|
|
#endif
|