mirror of
https://github.com/gabime/spdlog.git
synced 2025-10-02 11:29:01 +08:00
Fixed bugs in stdout_sinks and in msvc
This commit is contained in:
@@ -38,7 +38,7 @@ public:
|
||||
|
||||
void log(const details::log_msg &msg) override
|
||||
{
|
||||
const android_LogPriority priority = convert_to_android(msg.level);
|
||||
const android_LogPriority priority = convert_to_android_(msg.level);
|
||||
fmt::memory_buffer formatted;
|
||||
if (use_raw_msg_)
|
||||
{
|
||||
@@ -70,7 +70,7 @@ public:
|
||||
void flush() override {}
|
||||
|
||||
private:
|
||||
static android_LogPriority convert_to_android(spdlog::level::level_enum level)
|
||||
static android_LogPriority convert_to_android_(spdlog::level::level_enum level)
|
||||
{
|
||||
switch (level)
|
||||
{
|
||||
|
@@ -5,9 +5,9 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "spdlog/details/console_globals.h"
|
||||
#include "spdlog/details/null_mutex.h"
|
||||
#include "spdlog/details/os.h"
|
||||
#include "spdlog/details/console_globals.h"
|
||||
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
@@ -22,14 +22,14 @@ namespace sinks {
|
||||
* of the message.
|
||||
* If no color terminal detected, omit the escape codes.
|
||||
*/
|
||||
template<class StreamTrait, class ConsoleMutexTrait>
|
||||
template<class TargetStream, class ConsoleMutex>
|
||||
class ansicolor_sink : public sink
|
||||
{
|
||||
public:
|
||||
using mutex_t = typename ConsoleMutexTrait::mutex_t;
|
||||
using mutex_t = typename ConsoleMutex::mutex_t;
|
||||
ansicolor_sink()
|
||||
: target_file_(StreamTrait::stream())
|
||||
, mutex_(ConsoleMutexTrait::console_mutex())
|
||||
: target_file_(TargetStream::stream())
|
||||
, mutex_(ConsoleMutex::console_mutex())
|
||||
|
||||
{
|
||||
should_do_colors_ = details::os::in_terminal(target_file_) && details::os::is_color_terminal();
|
||||
|
@@ -27,9 +27,9 @@ public:
|
||||
explicit msvc_sink() {}
|
||||
|
||||
protected:
|
||||
void sink_it_(const details::log_msg &msg) override
|
||||
void sink_it_(const details::log_msg &, const fmt::memory_buffer &formatted) override
|
||||
{
|
||||
OutputDebugStringA(msg.formatted.c_str());
|
||||
OutputDebugStringA(fmt::to_string(formatted).c_str());
|
||||
}
|
||||
|
||||
void flush_() override {}
|
||||
@@ -38,6 +38,9 @@ protected:
|
||||
using msvc_sink_mt = msvc_sink<std::mutex>;
|
||||
using msvc_sink_st = msvc_sink<details::null_mutex>;
|
||||
|
||||
using windebug_sink_mt = msvc_sink_mt;
|
||||
using windebug_sink_st = msvc_sink_st;
|
||||
|
||||
} // namespace sinks
|
||||
} // namespace spdlog
|
||||
|
||||
|
@@ -6,6 +6,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "spdlog/details/log_msg.h"
|
||||
#include "spdlog/details/pattern_formatter_impl.h"
|
||||
#include "spdlog/formatter.h"
|
||||
|
||||
namespace spdlog {
|
||||
|
@@ -5,26 +5,27 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "spdlog/details/console_globals.h"
|
||||
#include "spdlog/details/null_mutex.h"
|
||||
#include "spdlog/details/traits.h"
|
||||
#include "spdlog/spdlog.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <spdlog/details/console_globals.h>
|
||||
|
||||
namespace spdlog {
|
||||
|
||||
namespace sinks {
|
||||
|
||||
template<class StdoutTrait, class ConsoleMutexTrait>
|
||||
template<class TargetStream, class ConsoleMutex>
|
||||
class stdout_sink : public sink
|
||||
{
|
||||
public:
|
||||
using mutex_t = typename ConsoleMutexTrait::mutex_t;
|
||||
using mutex_t = typename ConsoleMutex::mutex_t;
|
||||
stdout_sink()
|
||||
: mutex_(ConsoleMutexTrait::console_mutex())
|
||||
, file_(StdoutTrait::stream())
|
||||
: mutex_(ConsoleMutex::console_mutex())
|
||||
, file_(TargetStream::stream())
|
||||
{
|
||||
}
|
||||
~stdout_sink() = default;
|
||||
@@ -35,14 +36,16 @@ public:
|
||||
void log(const details::log_msg &msg) override
|
||||
{
|
||||
std::lock_guard<mutex_t> lock(mutex_);
|
||||
fwrite(msg.formatted.data(), sizeof(char), msg.formatted.size(), file_);
|
||||
fflush(StdoutTrait::stream());
|
||||
fmt::memory_buffer formatted;
|
||||
formatter_->format(msg, formatted);
|
||||
fwrite(formatted.data(), sizeof(char), formatted.size(), file_);
|
||||
fflush(TargetStream::stream());
|
||||
}
|
||||
|
||||
void flush() override
|
||||
{
|
||||
std::lock_guard<mutex_t> lock(mutex_);
|
||||
fflush(StdoutTrait::stream());
|
||||
fflush(TargetStream::stream());
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -50,10 +53,11 @@ private:
|
||||
FILE *file_;
|
||||
};
|
||||
|
||||
using stdout_sink_mt = stdout_sink<details::console_stdout_trait, details::console_mutex_trait>;
|
||||
using stdout_sink_st = stdout_sink<details::console_stdout_trait, details::console_null_mutex_trait>;
|
||||
using stderr_sink_mt = stdout_sink<details::console_stderr_trait, details::console_mutex_trait>;
|
||||
using stderr_sink_st = stdout_sink<details::console_stderr_trait, details::console_null_mutex_trait>;
|
||||
using stdout_sink_mt = stdout_sink<details::console_stdout_stream, details::console_global_mutex>;
|
||||
using stdout_sink_st = stdout_sink<details::console_stdout_stream, details::console_global_nullmutex>;
|
||||
|
||||
using stderr_sink_mt = stdout_sink<details::console_stderr_stream, details::console_global_mutex>;
|
||||
using stderr_sink_st = stdout_sink<details::console_stderr_stream, details::console_global_nullmutex>;
|
||||
|
||||
} // namespace sinks
|
||||
|
||||
|
@@ -7,8 +7,8 @@
|
||||
|
||||
#include "../fmt/fmt.h"
|
||||
#include "spdlog/common.h"
|
||||
#include "spdlog/details/null_mutex.h"
|
||||
#include "spdlog/details/console_globals.h"
|
||||
#include "spdlog/details/null_mutex.h"
|
||||
#include "spdlog/sinks/sink.h"
|
||||
|
||||
#include <memory>
|
||||
@@ -22,7 +22,7 @@ namespace sinks {
|
||||
/*
|
||||
* Windows color console sink. Uses WriteConsoleA to write to the console with colors
|
||||
*/
|
||||
template<class HandleTrait, class ConsoleMutexTrait>
|
||||
template<class OutHandle, class ConsoleMutex>
|
||||
class wincolor_sink : public sink
|
||||
{
|
||||
public:
|
||||
@@ -34,8 +34,8 @@ public:
|
||||
const WORD YELLOW = FOREGROUND_RED | FOREGROUND_GREEN;
|
||||
|
||||
wincolor_sink()
|
||||
: out_handle_(HandleTrait::handle())
|
||||
, mutex_(ConsoleMutexTrait::console_mutex())
|
||||
: out_handle_(OutHandle::handle())
|
||||
, mutex_(ConsoleMutex::console_mutex())
|
||||
{
|
||||
colors_[level::trace] = WHITE;
|
||||
colors_[level::debug] = CYAN;
|
||||
@@ -90,7 +90,7 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
using mutex_t = typename ConsoleMutexTrait::mutex_t;
|
||||
using mutex_t = typename ConsoleMutex::mutex_t;
|
||||
// set color and return the orig console attributes (for resetting later)
|
||||
WORD set_console_attribs(WORD attribs)
|
||||
{
|
||||
|
@@ -1,27 +0,0 @@
|
||||
//
|
||||
// Copyright(c) 2017 Alexander Dalshov.
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#if defined(_WIN32)
|
||||
|
||||
#include "spdlog/sinks/msvc_sink.h"
|
||||
|
||||
namespace spdlog {
|
||||
namespace sinks {
|
||||
|
||||
/*
|
||||
* Windows debug sink (logging using OutputDebugStringA, synonym for msvc_sink)
|
||||
*/
|
||||
template<class Mutex>
|
||||
using windebug_sink = msvc_sink<Mutex>;
|
||||
|
||||
using windebug_sink_mt = msvc_sink_mt;
|
||||
using windebug_sink_st = msvc_sink_st;
|
||||
|
||||
} // namespace sinks
|
||||
} // namespace spdlog
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user