spdlog
Loading...
Searching...
No Matches
wincolor_sink.h
Go to the documentation of this file.
1// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
2// Distributed under the MIT License (http://opensource.org/licenses/MIT)
3
4#pragma once
5
6#include <spdlog/common.h>
9#include <spdlog/sinks/sink.h>
10
11#include <memory>
12#include <mutex>
13#include <string>
14#include <array>
15#include <cstdint>
16
17namespace spdlog {
18namespace sinks {
19/*
20 * Windows color console sink. Uses WriteConsoleA to write to the console with
21 * colors
22 */
23template<typename ConsoleMutex>
24class wincolor_sink : public sink
25{
26public:
27 wincolor_sink(void *out_handle, color_mode mode);
28 ~wincolor_sink() override;
29
30 wincolor_sink(const wincolor_sink &other) = delete;
31 wincolor_sink &operator=(const wincolor_sink &other) = delete;
32
33 // change the color for the given level
35 void log(const details::log_msg &msg) final override;
36 void flush() final override;
37 void set_pattern(const std::string &pattern) override final;
38 void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) override final;
39 void set_color_mode(color_mode mode);
40
41protected:
42 using mutex_t = typename ConsoleMutex::mutex_t;
47 std::array<std::uint16_t, level::n_levels> colors_;
48
49 // set foreground color and return the orig console attributes (for resetting later)
50 std::uint16_t set_foreground_color_(std::uint16_t attribs);
51
52 // print a range of formatted message to console
53 void print_range_(const memory_buf_t &formatted, size_t start, size_t end);
54
55 // in case we are redirected to file (not in console mode)
56 void write_to_file_(const memory_buf_t &formatted);
57
59};
60
61template<typename ConsoleMutex>
62class wincolor_stdout_sink : public wincolor_sink<ConsoleMutex>
63{
64public:
66};
67
68template<typename ConsoleMutex>
69class wincolor_stderr_sink : public wincolor_sink<ConsoleMutex>
70{
71public:
72 explicit wincolor_stderr_sink(color_mode mode = color_mode::automatic);
73};
74
77
80} // namespace sinks
81} // namespace spdlog
82
83#ifdef SPDLOG_HEADER_ONLY
84# include "wincolor_sink-inl.h"
85#endif
level::level_enum level() const
Definition sink-inl.h:22
void flush() final override
std::array< std::uint16_t, level::n_levels > colors_
void log(const details::log_msg &msg) final override
wincolor_sink & operator=(const wincolor_sink &other)=delete
void write_to_file_(const memory_buf_t &formatted)
void set_color_mode_impl(color_mode mode)
std::unique_ptr< spdlog::formatter > formatter_
wincolor_sink(const wincolor_sink &other)=delete
void print_range_(const memory_buf_t &formatted, size_t start, size_t end)
void set_formatter(std::unique_ptr< spdlog::formatter > sink_formatter) override final
std::uint16_t set_foreground_color_(std::uint16_t attribs)
void set_pattern(const std::string &pattern) override final
void set_color_mode(color_mode mode)
void set_color(level::level_enum level, std::uint16_t color)
typename ConsoleMutex::mutex_t mutex_t
color
Definition color.h:23
Definition async.h:25
color_mode
Definition common.h:207
fmt::basic_memory_buffer< char, 250 > memory_buf_t
Definition common.h:116