mirror of
https://github.com/gabime/spdlog.git
synced 2025-09-29 01:29:35 +08:00
Merge branch 'v1.x' of https://github.com/gabime/spdlog into v1.x
This commit is contained in:
@@ -14,11 +14,25 @@
|
||||
#include <type_traits>
|
||||
#include <functional>
|
||||
|
||||
#ifdef _WIN32
|
||||
#ifndef NOMINMAX
|
||||
#define NOMINMAX // prevent windows redefining min/max
|
||||
#endif
|
||||
|
||||
#ifndef WIN32_LEAN_AND_MEAN
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#endif
|
||||
|
||||
#include <windows.h>
|
||||
#endif //_WIN32
|
||||
|
||||
|
||||
#if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT)
|
||||
#include <codecvt>
|
||||
#include <locale>
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef SPDLOG_COMPILED_LIB
|
||||
#undef SPDLOG_HEADER_ONLY
|
||||
#define SPDLOG_INLINE
|
||||
|
@@ -4,51 +4,11 @@
|
||||
#pragma once
|
||||
|
||||
#include "spdlog/details/null_mutex.h"
|
||||
#include <cstdio>
|
||||
#include <mutex>
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
#ifndef NOMINMAX
|
||||
#define NOMINMAX // prevent windows redefining min/max
|
||||
#endif
|
||||
|
||||
#ifndef WIN32_LEAN_AND_MEAN
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#endif
|
||||
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
namespace spdlog {
|
||||
namespace details {
|
||||
struct console_stdout
|
||||
{
|
||||
static std::FILE *stream()
|
||||
{
|
||||
return stdout;
|
||||
}
|
||||
#ifdef _WIN32
|
||||
static HANDLE handle()
|
||||
{
|
||||
return ::GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
}
|
||||
#endif
|
||||
};
|
||||
|
||||
struct console_stderr
|
||||
{
|
||||
static std::FILE *stream()
|
||||
{
|
||||
return stderr;
|
||||
}
|
||||
#ifdef _WIN32
|
||||
static HANDLE handle()
|
||||
{
|
||||
return ::GetStdHandle(STD_ERROR_HANDLE);
|
||||
}
|
||||
#endif
|
||||
};
|
||||
|
||||
struct console_mutex
|
||||
{
|
||||
|
@@ -9,9 +9,12 @@
|
||||
|
||||
#include "spdlog/details/os.h"
|
||||
|
||||
template<typename TargetStream, typename ConsoleMutex>
|
||||
SPDLOG_INLINE spdlog::sinks::ansicolor_sink<TargetStream, ConsoleMutex>::ansicolor_sink(color_mode mode)
|
||||
: target_file_(TargetStream::stream())
|
||||
namespace spdlog {
|
||||
namespace sinks {
|
||||
|
||||
template<typename ConsoleMutex>
|
||||
SPDLOG_INLINE ansicolor_sink<ConsoleMutex>::ansicolor_sink(FILE *target_file, color_mode mode)
|
||||
: target_file_(target_file)
|
||||
, mutex_(ConsoleMutex::mutex())
|
||||
|
||||
{
|
||||
@@ -25,16 +28,15 @@ SPDLOG_INLINE spdlog::sinks::ansicolor_sink<TargetStream, ConsoleMutex>::ansicol
|
||||
colors_[level::off] = reset;
|
||||
}
|
||||
|
||||
template<typename TargetStream, typename ConsoleMutex>
|
||||
SPDLOG_INLINE void spdlog::sinks::ansicolor_sink<TargetStream, ConsoleMutex>::set_color(
|
||||
level::level_enum color_level, const std::string &color)
|
||||
template<typename ConsoleMutex>
|
||||
SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::set_color(level::level_enum color_level, const std::string &color)
|
||||
{
|
||||
std::lock_guard<mutex_t> lock(mutex_);
|
||||
colors_[color_level] = color;
|
||||
}
|
||||
|
||||
template<typename TargetStream, typename ConsoleMutex>
|
||||
SPDLOG_INLINE void spdlog::sinks::ansicolor_sink<TargetStream, ConsoleMutex>::log(const details::log_msg &msg)
|
||||
template<typename ConsoleMutex>
|
||||
SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::log(const details::log_msg &msg)
|
||||
{
|
||||
// Wrap the originally formatted message in color codes.
|
||||
// If color is not supported in the terminal, log as is instead.
|
||||
@@ -60,36 +62,35 @@ SPDLOG_INLINE void spdlog::sinks::ansicolor_sink<TargetStream, ConsoleMutex>::lo
|
||||
fflush(target_file_);
|
||||
}
|
||||
|
||||
template<typename TargetStream, typename ConsoleMutex>
|
||||
SPDLOG_INLINE void spdlog::sinks::ansicolor_sink<TargetStream, ConsoleMutex>::flush()
|
||||
template<typename ConsoleMutex>
|
||||
SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::flush()
|
||||
{
|
||||
std::lock_guard<mutex_t> lock(mutex_);
|
||||
fflush(target_file_);
|
||||
}
|
||||
|
||||
template<typename TargetStream, typename ConsoleMutex>
|
||||
SPDLOG_INLINE void spdlog::sinks::ansicolor_sink<TargetStream, ConsoleMutex>::set_pattern(const std::string &pattern)
|
||||
template<typename ConsoleMutex>
|
||||
SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::set_pattern(const std::string &pattern)
|
||||
{
|
||||
std::lock_guard<mutex_t> lock(mutex_);
|
||||
formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern));
|
||||
}
|
||||
|
||||
template<typename TargetStream, typename ConsoleMutex>
|
||||
SPDLOG_INLINE void spdlog::sinks::ansicolor_sink<TargetStream, ConsoleMutex>::set_formatter(
|
||||
std::unique_ptr<spdlog::formatter> sink_formatter)
|
||||
template<typename ConsoleMutex>
|
||||
SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter)
|
||||
{
|
||||
std::lock_guard<mutex_t> lock(mutex_);
|
||||
formatter_ = std::move(sink_formatter);
|
||||
}
|
||||
|
||||
template<typename TargetStream, typename ConsoleMutex>
|
||||
SPDLOG_INLINE bool spdlog::sinks::ansicolor_sink<TargetStream, ConsoleMutex>::should_color()
|
||||
template<typename ConsoleMutex>
|
||||
SPDLOG_INLINE bool ansicolor_sink<ConsoleMutex>::should_color()
|
||||
{
|
||||
return should_do_colors_;
|
||||
}
|
||||
|
||||
template<typename TargetStream, typename ConsoleMutex>
|
||||
SPDLOG_INLINE void spdlog::sinks::ansicolor_sink<TargetStream, ConsoleMutex>::set_color_mode(color_mode mode)
|
||||
template<typename ConsoleMutex>
|
||||
SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::set_color_mode(color_mode mode)
|
||||
{
|
||||
switch (mode)
|
||||
{
|
||||
@@ -105,15 +106,29 @@ SPDLOG_INLINE void spdlog::sinks::ansicolor_sink<TargetStream, ConsoleMutex>::se
|
||||
}
|
||||
}
|
||||
|
||||
template<typename TargetStream, typename ConsoleMutex>
|
||||
SPDLOG_INLINE void spdlog::sinks::ansicolor_sink<TargetStream, ConsoleMutex>::print_ccode_(const std::string &color_code)
|
||||
template<typename ConsoleMutex>
|
||||
SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::print_ccode_(const std::string &color_code)
|
||||
{
|
||||
fwrite(color_code.data(), sizeof(char), color_code.size(), target_file_);
|
||||
}
|
||||
|
||||
template<typename TargetStream, typename ConsoleMutex>
|
||||
SPDLOG_INLINE void spdlog::sinks::ansicolor_sink<TargetStream, ConsoleMutex>::print_range_(
|
||||
const fmt::memory_buffer &formatted, size_t start, size_t end)
|
||||
template<typename ConsoleMutex>
|
||||
SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::print_range_(const fmt::memory_buffer &formatted, size_t start, size_t end)
|
||||
{
|
||||
fwrite(formatted.data() + start, sizeof(char), end - start, target_file_);
|
||||
}
|
||||
|
||||
// ansicolor_stdout_sink
|
||||
template<typename ConsoleMutex>
|
||||
SPDLOG_INLINE ansicolor_stdout_sink<ConsoleMutex>::ansicolor_stdout_sink(color_mode mode)
|
||||
: ansicolor_sink<ConsoleMutex>(stdout, mode)
|
||||
{}
|
||||
|
||||
// ansicolor_stderr_sink
|
||||
template<typename ConsoleMutex>
|
||||
SPDLOG_INLINE ansicolor_stderr_sink<ConsoleMutex>::ansicolor_stderr_sink(color_mode mode)
|
||||
: ansicolor_sink<ConsoleMutex>(stderr, mode)
|
||||
{}
|
||||
|
||||
} // namespace sinks
|
||||
} // namespace spdlog
|
@@ -20,12 +20,12 @@ namespace sinks {
|
||||
* of the message.
|
||||
* If no color terminal detected, omit the escape codes.
|
||||
*/
|
||||
template<typename TargetStream, typename ConsoleMutex>
|
||||
class ansicolor_sink final : public sink
|
||||
template<typename ConsoleMutex>
|
||||
class ansicolor_sink : public sink
|
||||
{
|
||||
public:
|
||||
using mutex_t = typename ConsoleMutex::mutex_t;
|
||||
explicit ansicolor_sink(color_mode mode = color_mode::automatic);
|
||||
ansicolor_sink(FILE* target_file, color_mode mode);
|
||||
~ansicolor_sink() override = default;
|
||||
|
||||
ansicolor_sink(const ansicolor_sink &other) = delete;
|
||||
@@ -77,11 +77,27 @@ private:
|
||||
void print_range_(const fmt::memory_buffer &formatted, size_t start, size_t end);
|
||||
};
|
||||
|
||||
using ansicolor_stdout_sink_mt = ansicolor_sink<details::console_stdout, details::console_mutex>;
|
||||
using ansicolor_stdout_sink_st = ansicolor_sink<details::console_stdout, details::console_nullmutex>;
|
||||
|
||||
using ansicolor_stderr_sink_mt = ansicolor_sink<details::console_stderr, details::console_mutex>;
|
||||
using ansicolor_stderr_sink_st = ansicolor_sink<details::console_stderr, details::console_nullmutex>;
|
||||
template<typename ConsoleMutex>
|
||||
class ansicolor_stdout_sink : public ansicolor_sink<ConsoleMutex>
|
||||
{
|
||||
public:
|
||||
explicit ansicolor_stdout_sink(color_mode mode = color_mode::automatic);
|
||||
};
|
||||
|
||||
template<typename ConsoleMutex>
|
||||
class ansicolor_stderr_sink : public ansicolor_sink<ConsoleMutex>
|
||||
{
|
||||
public:
|
||||
explicit ansicolor_stderr_sink(color_mode mode = color_mode::automatic);
|
||||
};
|
||||
|
||||
|
||||
using ansicolor_stdout_sink_mt = ansicolor_stdout_sink<details::console_mutex>;
|
||||
using ansicolor_stdout_sink_st = ansicolor_stdout_sink<details::console_nullmutex>;
|
||||
|
||||
using ansicolor_stderr_sink_mt = ansicolor_stderr_sink<details::console_mutex>;
|
||||
using ansicolor_stderr_sink_st = ansicolor_stderr_sink<details::console_nullmutex>;
|
||||
|
||||
} // namespace sinks
|
||||
} // namespace spdlog
|
||||
|
@@ -12,9 +12,9 @@
|
||||
namespace spdlog {
|
||||
namespace sinks {
|
||||
|
||||
template<typename TargetStream, typename ConsoleMutex>
|
||||
SPDLOG_INLINE wincolor_sink<TargetStream, ConsoleMutex>::wincolor_sink(color_mode mode)
|
||||
: out_handle_(TargetStream::handle())
|
||||
template<typename ConsoleMutex>
|
||||
SPDLOG_INLINE wincolor_sink<ConsoleMutex>::wincolor_sink(HANDLE out_handle, color_mode mode)
|
||||
: out_handle_(out_handle)
|
||||
, mutex_(ConsoleMutex::mutex())
|
||||
{
|
||||
set_color_mode(mode);
|
||||
@@ -27,22 +27,22 @@ SPDLOG_INLINE wincolor_sink<TargetStream, ConsoleMutex>::wincolor_sink(color_mod
|
||||
colors_[level::off] = 0;
|
||||
}
|
||||
|
||||
template<typename TargetStream, typename ConsoleMutex>
|
||||
SPDLOG_INLINE wincolor_sink<TargetStream, ConsoleMutex>::~wincolor_sink()
|
||||
template<typename ConsoleMutex>
|
||||
SPDLOG_INLINE wincolor_sink<ConsoleMutex>::~wincolor_sink()
|
||||
{
|
||||
this->flush();
|
||||
}
|
||||
|
||||
// change the color for the given level
|
||||
template<typename TargetStream, typename ConsoleMutex>
|
||||
void SPDLOG_INLINE wincolor_sink<TargetStream, ConsoleMutex>::set_color(level::level_enum level, WORD color)
|
||||
template<typename ConsoleMutex>
|
||||
void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_color(level::level_enum level, WORD color)
|
||||
{
|
||||
std::lock_guard<mutex_t> lock(mutex_);
|
||||
colors_[level] = color;
|
||||
}
|
||||
|
||||
template<typename TargetStream, typename ConsoleMutex>
|
||||
void SPDLOG_INLINE wincolor_sink<TargetStream, ConsoleMutex>::log(const details::log_msg &msg)
|
||||
template<typename ConsoleMutex>
|
||||
void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::log(const details::log_msg &msg)
|
||||
{
|
||||
std::lock_guard<mutex_t> lock(mutex_);
|
||||
fmt::memory_buffer formatted;
|
||||
@@ -66,28 +66,28 @@ void SPDLOG_INLINE wincolor_sink<TargetStream, ConsoleMutex>::log(const details:
|
||||
}
|
||||
}
|
||||
|
||||
template<typename TargetStream, typename ConsoleMutex>
|
||||
void SPDLOG_INLINE wincolor_sink<TargetStream, ConsoleMutex>::flush()
|
||||
template<typename ConsoleMutex>
|
||||
void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::flush()
|
||||
{
|
||||
// windows console always flushed?
|
||||
}
|
||||
|
||||
template<typename TargetStream, typename ConsoleMutex>
|
||||
void SPDLOG_INLINE wincolor_sink<TargetStream, ConsoleMutex>::set_pattern(const std::string &pattern)
|
||||
template<typename ConsoleMutex>
|
||||
void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_pattern(const std::string &pattern)
|
||||
{
|
||||
std::lock_guard<mutex_t> lock(mutex_);
|
||||
formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern));
|
||||
}
|
||||
|
||||
template<typename TargetStream, typename ConsoleMutex>
|
||||
void SPDLOG_INLINE wincolor_sink<TargetStream, ConsoleMutex>::set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter)
|
||||
template<typename ConsoleMutex>
|
||||
void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter)
|
||||
{
|
||||
std::lock_guard<mutex_t> lock(mutex_);
|
||||
formatter_ = std::move(sink_formatter);
|
||||
}
|
||||
|
||||
template<typename TargetStream, typename ConsoleMutex>
|
||||
void SPDLOG_INLINE wincolor_sink<TargetStream, ConsoleMutex>::set_color_mode(color_mode mode)
|
||||
template<typename ConsoleMutex>
|
||||
void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_color_mode(color_mode mode)
|
||||
{
|
||||
switch (mode)
|
||||
{
|
||||
@@ -104,8 +104,8 @@ void SPDLOG_INLINE wincolor_sink<TargetStream, ConsoleMutex>::set_color_mode(col
|
||||
}
|
||||
|
||||
// set color and return the orig console attributes (for resetting later)
|
||||
template<typename TargetStream, typename ConsoleMutex>
|
||||
WORD SPDLOG_INLINE wincolor_sink<TargetStream, ConsoleMutex>::set_console_attribs(WORD attribs)
|
||||
template<typename ConsoleMutex>
|
||||
WORD SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_console_attribs(WORD attribs)
|
||||
{
|
||||
CONSOLE_SCREEN_BUFFER_INFO orig_buffer_info;
|
||||
::GetConsoleScreenBufferInfo(out_handle_, &orig_buffer_info);
|
||||
@@ -118,12 +118,26 @@ WORD SPDLOG_INLINE wincolor_sink<TargetStream, ConsoleMutex>::set_console_attrib
|
||||
}
|
||||
|
||||
// print a range of formatted message to console
|
||||
template<typename TargetStream, typename ConsoleMutex>
|
||||
void SPDLOG_INLINE wincolor_sink<TargetStream, ConsoleMutex>::print_range_(const fmt::memory_buffer &formatted, size_t start, size_t end)
|
||||
template<typename ConsoleMutex>
|
||||
void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::print_range_(const fmt::memory_buffer &formatted, size_t start, size_t end)
|
||||
{
|
||||
auto size = static_cast<DWORD>(end - start);
|
||||
::WriteConsoleA(out_handle_, formatted.data() + start, size, nullptr, nullptr);
|
||||
}
|
||||
|
||||
|
||||
// wincolor_stdout_sink
|
||||
template<typename ConsoleMutex>
|
||||
SPDLOG_INLINE wincolor_stdout_sink<ConsoleMutex>::wincolor_stdout_sink(color_mode mode)
|
||||
: wincolor_sink<ConsoleMutex>(::GetStdHandle(STD_OUTPUT_HANDLE), mode)
|
||||
{}
|
||||
|
||||
// wincolor_stderr_sink
|
||||
template<typename ConsoleMutex>
|
||||
SPDLOG_INLINE wincolor_stderr_sink<ConsoleMutex>::wincolor_stderr_sink(color_mode mode)
|
||||
: wincolor_sink<ConsoleMutex>(::GetStdHandle(STD_ERROR_HANDLE), mode)
|
||||
{}
|
||||
|
||||
|
||||
} // namespace sinks
|
||||
} // namespace spdlog
|
@@ -20,7 +20,7 @@ namespace sinks {
|
||||
* Windows color console sink. Uses WriteConsoleA to write to the console with
|
||||
* colors
|
||||
*/
|
||||
template<typename TargetStream, typename ConsoleMutex>
|
||||
template<typename ConsoleMutex>
|
||||
class wincolor_sink : public sink
|
||||
{
|
||||
public:
|
||||
@@ -31,7 +31,7 @@ public:
|
||||
const WORD WHITE = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
|
||||
const WORD YELLOW = FOREGROUND_RED | FOREGROUND_GREEN;
|
||||
|
||||
wincolor_sink(color_mode mode = color_mode::automatic);
|
||||
wincolor_sink(HANDLE out_handle, color_mode mode);
|
||||
~wincolor_sink() override;
|
||||
|
||||
wincolor_sink(const wincolor_sink &other) = delete;
|
||||
@@ -58,11 +58,25 @@ private:
|
||||
void print_range_(const fmt::memory_buffer &formatted, size_t start, size_t end);
|
||||
};
|
||||
|
||||
using wincolor_stdout_sink_mt = wincolor_sink<details::console_stdout, details::console_mutex>;
|
||||
using wincolor_stdout_sink_st = wincolor_sink<details::console_stdout, details::console_nullmutex>;
|
||||
template<typename ConsoleMutex>
|
||||
class wincolor_stdout_sink : public wincolor_sink<ConsoleMutex>
|
||||
{
|
||||
public:
|
||||
explicit wincolor_stdout_sink(color_mode mode = color_mode::automatic);
|
||||
};
|
||||
|
||||
using wincolor_stderr_sink_mt = wincolor_sink<details::console_stderr, details::console_mutex>;
|
||||
using wincolor_stderr_sink_st = wincolor_sink<details::console_stderr, details::console_nullmutex>;
|
||||
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>;
|
||||
|
||||
using wincolor_stderr_sink_mt = wincolor_stderr_sink<details::console_mutex>;
|
||||
using wincolor_stderr_sink_st = wincolor_stderr_sink<details::console_nullmutex>;
|
||||
|
||||
} // namespace sinks
|
||||
} // namespace spdlog
|
||||
|
Reference in New Issue
Block a user