mirror of
https://github.com/gabime/spdlog.git
synced 2025-01-27 07:59:03 +08:00
Fixed bugs in stdout_sinks and in msvc
This commit is contained in:
parent
bde4c7149f
commit
9dc44c39e7
@ -1,5 +1,5 @@
|
|||||||
CXX = g++
|
CXX = g++
|
||||||
CXX_FLAGS = -Wall -Wextra -pedantic -std=c++11 -pthread -I../include
|
CXX_FLAGS = -Wall -Wextra -pedantic -std=c++11 -pthread -I../include -fmax-errors=1
|
||||||
CXX_RELEASE_FLAGS = -O3 -march=native
|
CXX_RELEASE_FLAGS = -O3 -march=native
|
||||||
CXX_DEBUG_FLAGS= -g
|
CXX_DEBUG_FLAGS= -g
|
||||||
|
|
||||||
|
@ -55,16 +55,16 @@ int main(int, char *[])
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#include "spdlog/sinks/stdout_color_sinks.h" // or "/sinks/stdout_sinks.h" if no colors needed
|
#include "spdlog/sinks/stdout_sinks.h" // or "/sinks/stdout_sinks.h" if no colors needed
|
||||||
|
|
||||||
void stdout_example()
|
void stdout_example()
|
||||||
{
|
{
|
||||||
// create color multi threaded logger
|
// create color multi threaded logger
|
||||||
auto console = spdlog::stdout_color_mt("console");
|
auto console = spdlog::stdout_logger_mt("console");
|
||||||
console->info("Welcome to spdlog!");
|
console->info("Welcome to spdlog!");
|
||||||
console->error("Some error message with arg: {}", 1);
|
console->error("Some error message with arg: {}", 1);
|
||||||
|
|
||||||
auto err_logger = spdlog::stderr_color_mt("error_logger");
|
auto err_logger = spdlog::stderr_logger_mt("error_logger");
|
||||||
err_logger->error("Some error message");
|
err_logger->error("Some error message");
|
||||||
// Formatting examples
|
// Formatting examples
|
||||||
console->warn("Easy padding in numbers like {:08d}", 12);
|
console->warn("Easy padding in numbers like {:08d}", 12);
|
||||||
|
@ -88,6 +88,10 @@ inline void pad3(int n, fmt::memory_buffer &dest)
|
|||||||
|
|
||||||
inline void pad6(size_t n, fmt::memory_buffer &dest)
|
inline void pad6(size_t n, fmt::memory_buffer &dest)
|
||||||
{
|
{
|
||||||
|
// todo: maybe replace this implementation with
|
||||||
|
// pad3(n / 1000, dest);
|
||||||
|
// pad3(n % 1000, dest);
|
||||||
|
|
||||||
if (n > 99999)
|
if (n > 99999)
|
||||||
{
|
{
|
||||||
append_int(n, dest);
|
append_int(n, dest);
|
||||||
|
@ -533,7 +533,7 @@ public:
|
|||||||
, last_log_secs_(0)
|
, last_log_secs_(0)
|
||||||
{
|
{
|
||||||
std::memset(&cached_tm_, 0, sizeof(cached_tm_));
|
std::memset(&cached_tm_, 0, sizeof(cached_tm_));
|
||||||
compile_pattern(pattern);
|
compile_pattern_(pattern);
|
||||||
}
|
}
|
||||||
|
|
||||||
pattern_formatter(const pattern_formatter &) = default;
|
pattern_formatter(const pattern_formatter &) = default;
|
||||||
@ -544,7 +544,7 @@ public:
|
|||||||
auto secs = std::chrono::duration_cast<std::chrono::seconds>(msg.time.time_since_epoch());
|
auto secs = std::chrono::duration_cast<std::chrono::seconds>(msg.time.time_since_epoch());
|
||||||
if (secs != last_log_secs_)
|
if (secs != last_log_secs_)
|
||||||
{
|
{
|
||||||
cached_tm_ = get_time(msg);
|
cached_tm_ = get_time_(msg);
|
||||||
last_log_secs_ = secs;
|
last_log_secs_ = secs;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@ -563,7 +563,7 @@ private:
|
|||||||
std::chrono::seconds last_log_secs_;
|
std::chrono::seconds last_log_secs_;
|
||||||
|
|
||||||
std::vector<std::unique_ptr<details::flag_formatter>> formatters_;
|
std::vector<std::unique_ptr<details::flag_formatter>> formatters_;
|
||||||
std::tm get_time(const details::log_msg &msg)
|
std::tm get_time_(const details::log_msg &msg)
|
||||||
{
|
{
|
||||||
if (pattern_time_ == pattern_time_type::local)
|
if (pattern_time_ == pattern_time_type::local)
|
||||||
{
|
{
|
||||||
@ -572,7 +572,7 @@ private:
|
|||||||
return details::os::gmtime(log_clock::to_time_t(msg.time));
|
return details::os::gmtime(log_clock::to_time_t(msg.time));
|
||||||
}
|
}
|
||||||
|
|
||||||
void handle_flag(char flag)
|
void handle_flag_(char flag)
|
||||||
{
|
{
|
||||||
switch (flag)
|
switch (flag)
|
||||||
{
|
{
|
||||||
@ -717,7 +717,7 @@ private:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void compile_pattern(const std::string &pattern)
|
void compile_pattern_(const std::string &pattern)
|
||||||
{
|
{
|
||||||
auto end = pattern.end();
|
auto end = pattern.end();
|
||||||
std::unique_ptr<details::aggregate_formatter> user_chars;
|
std::unique_ptr<details::aggregate_formatter> user_chars;
|
||||||
@ -732,7 +732,7 @@ private:
|
|||||||
// if(
|
// if(
|
||||||
if (++it != end)
|
if (++it != end)
|
||||||
{
|
{
|
||||||
handle_flag(*it);
|
handle_flag_(*it);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -36,7 +36,7 @@ public:
|
|||||||
{
|
{
|
||||||
std::lock_guard<Mutex> lock(mutex_);
|
std::lock_guard<Mutex> lock(mutex_);
|
||||||
auto logger_name = new_logger->name();
|
auto logger_name = new_logger->name();
|
||||||
throw_if_exists(logger_name);
|
throw_if_exists_(logger_name);
|
||||||
loggers_[logger_name] = new_logger;
|
loggers_[logger_name] = new_logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -44,7 +44,7 @@ public:
|
|||||||
{
|
{
|
||||||
std::lock_guard<Mutex> lock(mutex_);
|
std::lock_guard<Mutex> lock(mutex_);
|
||||||
auto logger_name = new_logger->name();
|
auto logger_name = new_logger->name();
|
||||||
throw_if_exists(logger_name);
|
throw_if_exists_(logger_name);
|
||||||
|
|
||||||
// create default formatter if not exists
|
// create default formatter if not exists
|
||||||
|
|
||||||
@ -162,7 +162,7 @@ public:
|
|||||||
private:
|
private:
|
||||||
registry_t<Mutex>() = default;
|
registry_t<Mutex>() = default;
|
||||||
|
|
||||||
void throw_if_exists(const std::string &logger_name)
|
void throw_if_exists_(const std::string &logger_name)
|
||||||
{
|
{
|
||||||
if (loggers_.find(logger_name) != loggers_.end())
|
if (loggers_.find(logger_name) != loggers_.end())
|
||||||
{
|
{
|
||||||
|
@ -95,7 +95,7 @@ public:
|
|||||||
}
|
}
|
||||||
for (size_t i = 0; i < threads_n; i++)
|
for (size_t i = 0; i < threads_n; i++)
|
||||||
{
|
{
|
||||||
threads_.emplace_back(std::bind(&thread_pool::worker_loop, this));
|
threads_.emplace_back(std::bind(&thread_pool::worker_loop_, this));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,7 +106,7 @@ public:
|
|||||||
{
|
{
|
||||||
for (size_t i = 0; i < threads_.size(); i++)
|
for (size_t i = 0; i < threads_.size(); i++)
|
||||||
{
|
{
|
||||||
post_async_msg(async_msg(async_msg_type::terminate), async_overflow_policy::block_retry);
|
post_async_msg_(async_msg(async_msg_type::terminate), async_overflow_policy::block_retry);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto &t : threads_)
|
for (auto &t : threads_)
|
||||||
@ -123,12 +123,12 @@ public:
|
|||||||
void post_log(async_logger_ptr &&worker_ptr, details::log_msg &&msg, async_overflow_policy overflow_policy)
|
void post_log(async_logger_ptr &&worker_ptr, details::log_msg &&msg, async_overflow_policy overflow_policy)
|
||||||
{
|
{
|
||||||
async_msg async_m(std::forward<async_logger_ptr>(worker_ptr), async_msg_type::log, std::forward<log_msg>(msg));
|
async_msg async_m(std::forward<async_logger_ptr>(worker_ptr), async_msg_type::log, std::forward<log_msg>(msg));
|
||||||
post_async_msg(std::move(async_m), overflow_policy);
|
post_async_msg_(std::move(async_m), overflow_policy);
|
||||||
}
|
}
|
||||||
|
|
||||||
void post_flush(async_logger_ptr &&worker_ptr, async_overflow_policy overflow_policy)
|
void post_flush(async_logger_ptr &&worker_ptr, async_overflow_policy overflow_policy)
|
||||||
{
|
{
|
||||||
post_async_msg(async_msg(std::move(worker_ptr), async_msg_type::flush), overflow_policy);
|
post_async_msg_(async_msg(std::move(worker_ptr), async_msg_type::flush), overflow_policy);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -136,7 +136,7 @@ private:
|
|||||||
|
|
||||||
std::vector<std::thread> threads_;
|
std::vector<std::thread> threads_;
|
||||||
|
|
||||||
void post_async_msg(async_msg &&new_msg, async_overflow_policy overflow_policy)
|
void post_async_msg_(async_msg &&new_msg, async_overflow_policy overflow_policy)
|
||||||
{
|
{
|
||||||
if (overflow_policy == async_overflow_policy::block_retry)
|
if (overflow_policy == async_overflow_policy::block_retry)
|
||||||
{
|
{
|
||||||
@ -148,14 +148,14 @@ private:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void worker_loop()
|
void worker_loop_()
|
||||||
{
|
{
|
||||||
while (process_next_msg()) {};
|
while (process_next_msg_()) {};
|
||||||
}
|
}
|
||||||
|
|
||||||
// process next message in the queue
|
// process next message in the queue
|
||||||
// return true if this thread should still be active (while no terminate msg was received)
|
// return true if this thread should still be active (while no terminate msg was received)
|
||||||
bool process_next_msg()
|
bool process_next_msg_()
|
||||||
{
|
{
|
||||||
async_msg incoming_async_msg;
|
async_msg incoming_async_msg;
|
||||||
bool dequeued = q_.dequeue_for(incoming_async_msg, std::chrono::seconds(10));
|
bool dequeued = q_.dequeue_for(incoming_async_msg, std::chrono::seconds(10));
|
||||||
|
@ -17,5 +17,3 @@ public:
|
|||||||
virtual void format(const details::log_msg &msg, fmt::memory_buffer &dest) = 0;
|
virtual void format(const details::log_msg &msg, fmt::memory_buffer &dest) = 0;
|
||||||
};
|
};
|
||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
|
||||||
#include "details/pattern_formatter_impl.h"
|
|
||||||
|
@ -38,7 +38,7 @@ public:
|
|||||||
|
|
||||||
void log(const details::log_msg &msg) override
|
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;
|
fmt::memory_buffer formatted;
|
||||||
if (use_raw_msg_)
|
if (use_raw_msg_)
|
||||||
{
|
{
|
||||||
@ -70,7 +70,7 @@ public:
|
|||||||
void flush() override {}
|
void flush() override {}
|
||||||
|
|
||||||
private:
|
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)
|
switch (level)
|
||||||
{
|
{
|
||||||
|
@ -5,9 +5,9 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "spdlog/details/console_globals.h"
|
||||||
#include "spdlog/details/null_mutex.h"
|
#include "spdlog/details/null_mutex.h"
|
||||||
#include "spdlog/details/os.h"
|
#include "spdlog/details/os.h"
|
||||||
#include "spdlog/details/console_globals.h"
|
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
@ -22,14 +22,14 @@ namespace sinks {
|
|||||||
* of the message.
|
* of the message.
|
||||||
* If no color terminal detected, omit the escape codes.
|
* If no color terminal detected, omit the escape codes.
|
||||||
*/
|
*/
|
||||||
template<class StreamTrait, class ConsoleMutexTrait>
|
template<class TargetStream, class ConsoleMutex>
|
||||||
class ansicolor_sink : public sink
|
class ansicolor_sink : public sink
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using mutex_t = typename ConsoleMutexTrait::mutex_t;
|
using mutex_t = typename ConsoleMutex::mutex_t;
|
||||||
ansicolor_sink()
|
ansicolor_sink()
|
||||||
: target_file_(StreamTrait::stream())
|
: target_file_(TargetStream::stream())
|
||||||
, mutex_(ConsoleMutexTrait::console_mutex())
|
, mutex_(ConsoleMutex::console_mutex())
|
||||||
|
|
||||||
{
|
{
|
||||||
should_do_colors_ = details::os::in_terminal(target_file_) && details::os::is_color_terminal();
|
should_do_colors_ = details::os::in_terminal(target_file_) && details::os::is_color_terminal();
|
||||||
|
@ -27,9 +27,9 @@ public:
|
|||||||
explicit msvc_sink() {}
|
explicit msvc_sink() {}
|
||||||
|
|
||||||
protected:
|
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 {}
|
void flush_() override {}
|
||||||
@ -38,6 +38,9 @@ protected:
|
|||||||
using msvc_sink_mt = msvc_sink<std::mutex>;
|
using msvc_sink_mt = msvc_sink<std::mutex>;
|
||||||
using msvc_sink_st = msvc_sink<details::null_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 sinks
|
||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "spdlog/details/log_msg.h"
|
#include "spdlog/details/log_msg.h"
|
||||||
|
#include "spdlog/details/pattern_formatter_impl.h"
|
||||||
#include "spdlog/formatter.h"
|
#include "spdlog/formatter.h"
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
|
@ -5,26 +5,27 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "spdlog/details/console_globals.h"
|
||||||
#include "spdlog/details/null_mutex.h"
|
#include "spdlog/details/null_mutex.h"
|
||||||
#include "spdlog/details/traits.h"
|
|
||||||
#include "spdlog/spdlog.h"
|
#include "spdlog/spdlog.h"
|
||||||
|
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
#include <spdlog/details/console_globals.h>
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
|
|
||||||
namespace sinks {
|
namespace sinks {
|
||||||
|
|
||||||
template<class StdoutTrait, class ConsoleMutexTrait>
|
template<class TargetStream, class ConsoleMutex>
|
||||||
class stdout_sink : public sink
|
class stdout_sink : public sink
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using mutex_t = typename ConsoleMutexTrait::mutex_t;
|
using mutex_t = typename ConsoleMutex::mutex_t;
|
||||||
stdout_sink()
|
stdout_sink()
|
||||||
: mutex_(ConsoleMutexTrait::console_mutex())
|
: mutex_(ConsoleMutex::console_mutex())
|
||||||
, file_(StdoutTrait::stream())
|
, file_(TargetStream::stream())
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
~stdout_sink() = default;
|
~stdout_sink() = default;
|
||||||
@ -35,14 +36,16 @@ public:
|
|||||||
void log(const details::log_msg &msg) override
|
void log(const details::log_msg &msg) override
|
||||||
{
|
{
|
||||||
std::lock_guard<mutex_t> lock(mutex_);
|
std::lock_guard<mutex_t> lock(mutex_);
|
||||||
fwrite(msg.formatted.data(), sizeof(char), msg.formatted.size(), file_);
|
fmt::memory_buffer formatted;
|
||||||
fflush(StdoutTrait::stream());
|
formatter_->format(msg, formatted);
|
||||||
|
fwrite(formatted.data(), sizeof(char), formatted.size(), file_);
|
||||||
|
fflush(TargetStream::stream());
|
||||||
}
|
}
|
||||||
|
|
||||||
void flush() override
|
void flush() override
|
||||||
{
|
{
|
||||||
std::lock_guard<mutex_t> lock(mutex_);
|
std::lock_guard<mutex_t> lock(mutex_);
|
||||||
fflush(StdoutTrait::stream());
|
fflush(TargetStream::stream());
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -50,10 +53,11 @@ private:
|
|||||||
FILE *file_;
|
FILE *file_;
|
||||||
};
|
};
|
||||||
|
|
||||||
using stdout_sink_mt = stdout_sink<details::console_stdout_trait, details::console_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_trait, details::console_null_mutex_trait>;
|
using stdout_sink_st = stdout_sink<details::console_stdout_stream, details::console_global_nullmutex>;
|
||||||
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 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
|
} // namespace sinks
|
||||||
|
|
||||||
|
@ -7,8 +7,8 @@
|
|||||||
|
|
||||||
#include "../fmt/fmt.h"
|
#include "../fmt/fmt.h"
|
||||||
#include "spdlog/common.h"
|
#include "spdlog/common.h"
|
||||||
#include "spdlog/details/null_mutex.h"
|
|
||||||
#include "spdlog/details/console_globals.h"
|
#include "spdlog/details/console_globals.h"
|
||||||
|
#include "spdlog/details/null_mutex.h"
|
||||||
#include "spdlog/sinks/sink.h"
|
#include "spdlog/sinks/sink.h"
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
@ -22,7 +22,7 @@ namespace sinks {
|
|||||||
/*
|
/*
|
||||||
* Windows color console sink. Uses WriteConsoleA to write to the console with colors
|
* 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
|
class wincolor_sink : public sink
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -34,8 +34,8 @@ public:
|
|||||||
const WORD YELLOW = FOREGROUND_RED | FOREGROUND_GREEN;
|
const WORD YELLOW = FOREGROUND_RED | FOREGROUND_GREEN;
|
||||||
|
|
||||||
wincolor_sink()
|
wincolor_sink()
|
||||||
: out_handle_(HandleTrait::handle())
|
: out_handle_(OutHandle::handle())
|
||||||
, mutex_(ConsoleMutexTrait::console_mutex())
|
, mutex_(ConsoleMutex::console_mutex())
|
||||||
{
|
{
|
||||||
colors_[level::trace] = WHITE;
|
colors_[level::trace] = WHITE;
|
||||||
colors_[level::debug] = CYAN;
|
colors_[level::debug] = CYAN;
|
||||||
@ -90,7 +90,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
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)
|
// set color and return the orig console attributes (for resetting later)
|
||||||
WORD set_console_attribs(WORD attribs)
|
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
|
|
Loading…
Reference in New Issue
Block a user