1
0
mirror of https://github.com/gabime/spdlog.git synced 2025-04-01 02:42:41 +08:00
spdlog/src/sinks/stdout_sinks.cpp

152 lines
5.8 KiB
C++
Raw Normal View History

// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
2023-09-25 01:26:32 +08:00
#include <memory>
2023-09-25 07:35:55 +08:00
#include <spdlog/pattern_formatter.h>
#include <spdlog/sinks/stdout_sinks.h>
2023-09-25 01:26:32 +08:00
#ifdef _WIN32
2023-09-25 07:35:55 +08:00
// under windows using fwrite to non-binary stream results in \r\r\n (see issue #1675)
// so instead we use ::FileWrite
#include <spdlog/details/windows_include.h>
2023-09-25 01:26:32 +08:00
2023-09-25 21:40:05 +08:00
#ifndef _USING_V110_SDK71_ // fileapi.h doesn't exist in winxp
#include <fileapi.h> // WriteFile (..)
2023-09-25 07:35:55 +08:00
#endif
2023-09-25 01:26:32 +08:00
2023-09-25 21:40:05 +08:00
#include <io.h> // _get_osfhandle(..)
#include <stdio.h> // _fileno(..)
#endif // WIN32
2023-09-25 01:26:32 +08:00
namespace spdlog {
2023-09-25 01:43:14 +08:00
namespace sinks {
2023-09-25 07:35:55 +08:00
template <typename ConsoleMutex>
2023-09-25 01:43:14 +08:00
stdout_sink_base<ConsoleMutex>::stdout_sink_base(FILE *file)
2023-09-25 21:19:10 +08:00
: mutex_(ConsoleMutex::mutex()),
file_(file),
formatter_(std::make_unique<spdlog::pattern_formatter>()) {
2023-09-25 01:26:32 +08:00
#ifdef _WIN32
2023-09-25 01:43:14 +08:00
// get windows handle from the FILE* object
2023-09-25 01:26:32 +08:00
handle_ = reinterpret_cast<HANDLE>(::_get_osfhandle(::_fileno(file_)));
// don't throw to support cases where no console is attached,
// and let the log method to do nothing if (handle_ == INVALID_HANDLE_VALUE).
// throw only if non stdout/stderr target is requested (probably regular file and not console).
2023-09-25 07:35:55 +08:00
if (handle_ == INVALID_HANDLE_VALUE && file != stdout && file != stderr) {
2023-09-25 01:26:32 +08:00
throw_spdlog_ex("spdlog::stdout_sink_base: _get_osfhandle() failed", errno);
}
2023-09-25 21:40:05 +08:00
#endif // WIN32
2023-09-25 01:43:14 +08:00
}
2023-09-25 01:26:32 +08:00
2023-09-25 07:35:55 +08:00
template <typename ConsoleMutex>
void stdout_sink_base<ConsoleMutex>::log(const details::log_msg &msg) {
2023-09-25 01:26:32 +08:00
#ifdef _WIN32
2023-09-25 07:35:55 +08:00
if (handle_ == INVALID_HANDLE_VALUE) {
2023-09-25 01:26:32 +08:00
return;
}
std::lock_guard<mutex_t> lock(mutex_);
memory_buf_t formatted;
formatter_->format(msg, formatted);
auto size = static_cast<DWORD>(formatted.size());
DWORD bytes_written = 0;
bool ok = ::WriteFile(handle_, formatted.data(), size, &bytes_written, nullptr) != 0;
2023-09-25 07:35:55 +08:00
if (!ok) {
throw_spdlog_ex("stdout_sink_base: WriteFile() failed. GetLastError(): " +
std::to_string(::GetLastError()));
2023-09-25 01:26:32 +08:00
}
#else
2023-09-25 01:43:14 +08:00
std::lock_guard<mutex_t> lock(mutex_);
memory_buf_t formatted;
formatter_->format(msg, formatted);
::fwrite(formatted.data(), sizeof(char), formatted.size(), file_);
2023-09-25 21:40:05 +08:00
#endif // WIN32
::fflush(file_); // flush every line to terminal
2023-09-25 01:43:14 +08:00
}
2023-09-25 07:35:55 +08:00
template <typename ConsoleMutex>
void stdout_sink_base<ConsoleMutex>::flush() {
2023-09-25 01:43:14 +08:00
std::lock_guard<mutex_t> lock(mutex_);
fflush(file_);
}
2023-09-25 01:26:32 +08:00
2023-09-25 07:35:55 +08:00
template <typename ConsoleMutex>
void stdout_sink_base<ConsoleMutex>::set_pattern(const std::string &pattern) {
2023-09-25 01:43:14 +08:00
std::lock_guard<mutex_t> lock(mutex_);
formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern));
}
2023-09-25 01:26:32 +08:00
2023-09-25 07:35:55 +08:00
template <typename ConsoleMutex>
void stdout_sink_base<ConsoleMutex>::set_formatter(
std::unique_ptr<spdlog::formatter> sink_formatter) {
2023-09-25 01:43:14 +08:00
std::lock_guard<mutex_t> lock(mutex_);
formatter_ = std::move(sink_formatter);
}
2023-09-25 01:26:32 +08:00
2023-09-25 01:43:14 +08:00
// stdout sink
2023-09-25 07:35:55 +08:00
template <typename ConsoleMutex>
2023-09-25 01:43:14 +08:00
stdout_sink<ConsoleMutex>::stdout_sink()
2023-09-25 07:35:55 +08:00
: stdout_sink_base<ConsoleMutex>(stdout) {}
2023-09-25 01:26:32 +08:00
2023-09-25 01:43:14 +08:00
// stderr sink
2023-09-25 07:35:55 +08:00
template <typename ConsoleMutex>
2023-09-25 01:43:14 +08:00
stderr_sink<ConsoleMutex>::stderr_sink()
2023-09-25 07:35:55 +08:00
: stdout_sink_base<ConsoleMutex>(stderr) {}
2023-09-25 01:26:32 +08:00
2023-09-25 21:40:05 +08:00
} // namespace sinks
2023-09-25 01:26:32 +08:00
2023-09-25 01:43:14 +08:00
// factory methods
2023-09-25 07:35:55 +08:00
template <typename Factory>
std::shared_ptr<logger> stdout_logger_mt(const std::string &logger_name) {
2023-09-25 01:43:14 +08:00
return Factory::template create<sinks::stdout_sink_mt>(logger_name);
}
2023-09-25 07:35:55 +08:00
template <typename Factory>
std::shared_ptr<logger> stdout_logger_st(const std::string &logger_name) {
2023-09-25 01:43:14 +08:00
return Factory::template create<sinks::stdout_sink_st>(logger_name);
}
2023-09-25 07:35:55 +08:00
template <typename Factory>
std::shared_ptr<logger> stderr_logger_mt(const std::string &logger_name) {
2023-09-25 01:43:14 +08:00
return Factory::template create<sinks::stderr_sink_mt>(logger_name);
}
2023-09-25 07:35:55 +08:00
template <typename Factory>
std::shared_ptr<logger> stderr_logger_st(const std::string &logger_name) {
2023-09-25 01:43:14 +08:00
return Factory::template create<sinks::stderr_sink_st>(logger_name);
}
2023-09-25 21:40:05 +08:00
} // namespace spdlog
2023-09-25 01:26:32 +08:00
// template instantiations for stdout/stderr loggers
#include <spdlog/details/console_globals.h>
2020-03-10 03:02:16 +08:00
template class SPDLOG_API spdlog::sinks::stdout_sink_base<spdlog::details::console_mutex>;
template class SPDLOG_API spdlog::sinks::stdout_sink_base<spdlog::details::console_nullmutex>;
template class SPDLOG_API spdlog::sinks::stdout_sink<spdlog::details::console_mutex>;
template class SPDLOG_API spdlog::sinks::stdout_sink<spdlog::details::console_nullmutex>;
template class SPDLOG_API spdlog::sinks::stderr_sink<spdlog::details::console_mutex>;
template class SPDLOG_API spdlog::sinks::stderr_sink<spdlog::details::console_nullmutex>;
2023-09-25 01:26:32 +08:00
// template instantiations for stdout/stderr factory functions
#include <spdlog/async.h>
2023-09-25 07:35:55 +08:00
#include <spdlog/details/synchronous_factory.h>
2023-09-25 01:26:32 +08:00
2023-09-25 07:35:55 +08:00
template SPDLOG_API std::shared_ptr<spdlog::logger>
spdlog::stdout_logger_mt<spdlog::synchronous_factory>(const std::string &logger_name);
template SPDLOG_API std::shared_ptr<spdlog::logger>
spdlog::stdout_logger_st<spdlog::synchronous_factory>(const std::string &logger_name);
template SPDLOG_API std::shared_ptr<spdlog::logger>
spdlog::stderr_logger_mt<spdlog::synchronous_factory>(const std::string &logger_name);
template SPDLOG_API std::shared_ptr<spdlog::logger>
spdlog::stderr_logger_st<spdlog::synchronous_factory>(const std::string &logger_name);
2023-09-25 21:40:05 +08:00
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stdout_logger_mt<spdlog::async_factory>(
const std::string &logger_name);
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stdout_logger_st<spdlog::async_factory>(
const std::string &logger_name);
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stderr_logger_mt<spdlog::async_factory>(
const std::string &logger_name);
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stderr_logger_st<spdlog::async_factory>(
const std::string &logger_name);