r5sdk/r5dev/thirdparty/spdlog/sinks/stdout_sinks-inl.h

127 lines
4.2 KiB
C
Raw Normal View History

2021-07-08 07:07:27 -07:00
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#pragma once
#ifndef SPDLOG_HEADER_ONLY
2024-02-04 13:43:17 +01:00
#include <spdlog/sinks/stdout_sinks.h>
2021-07-08 07:07:27 -07:00
#endif
2024-02-04 13:43:17 +01:00
#include <memory>
#include <spdlog/details/console_globals.h>
#include <spdlog/pattern_formatter.h>
2021-07-08 07:07:27 -07:00
#ifdef _WIN32
2024-02-04 13:43:17 +01: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>
2021-07-08 07:07:27 -07:00
2024-02-04 13:43:17 +01:00
#ifndef _USING_V110_SDK71_ // fileapi.h doesn't exist in winxp
#include <fileapi.h> // WriteFile (..)
#endif
2021-07-08 07:07:27 -07:00
2024-02-04 13:43:17 +01:00
#include <io.h> // _get_osfhandle(..)
#include <stdio.h> // _fileno(..)
#endif // WIN32
2021-07-08 07:07:27 -07:00
namespace spdlog {
namespace sinks {
2024-02-04 13:43:17 +01:00
template <typename ConsoleMutex>
2021-07-08 07:07:27 -07:00
SPDLOG_INLINE stdout_sink_base<ConsoleMutex>::stdout_sink_base(FILE *file)
2024-02-04 13:43:17 +01:00
: mutex_(ConsoleMutex::mutex()),
file_(file),
formatter_(details::make_unique<spdlog::pattern_formatter>()) {
2021-07-08 07:07:27 -07:00
#ifdef _WIN32
// get windows handle from the FILE* object
handle_ = reinterpret_cast<HANDLE>(::_get_osfhandle(::_fileno(file_)));
2021-07-08 07:07:27 -07:00
// 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).
2024-02-04 13:43:17 +01:00
if (handle_ == INVALID_HANDLE_VALUE && file != stdout && file != stderr) {
2021-07-08 07:07:27 -07:00
throw_spdlog_ex("spdlog::stdout_sink_base: _get_osfhandle() failed", errno);
}
2024-02-04 13:43:17 +01:00
#endif // WIN32
2021-07-08 07:07:27 -07:00
}
2024-02-04 13:43:17 +01:00
template <typename ConsoleMutex>
SPDLOG_INLINE void stdout_sink_base<ConsoleMutex>::log(const details::log_msg &msg) {
2021-07-08 07:07:27 -07:00
#ifdef _WIN32
2024-02-04 13:43:17 +01:00
if (handle_ == INVALID_HANDLE_VALUE) {
2021-07-08 07:07:27 -07: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;
2024-02-04 13:43:17 +01:00
if (!ok) {
throw_spdlog_ex("stdout_sink_base: WriteFile() failed. GetLastError(): " +
std::to_string(::GetLastError()));
2021-07-08 07:07:27 -07:00
}
#else
std::lock_guard<mutex_t> lock(mutex_);
memory_buf_t formatted;
formatter_->format(msg, formatted);
::fwrite(formatted.data(), sizeof(char), formatted.size(), file_);
2024-02-04 13:43:17 +01:00
#endif // WIN32
::fflush(file_); // flush every line to terminal
2021-07-08 07:07:27 -07:00
}
2024-02-04 13:43:17 +01:00
template <typename ConsoleMutex>
SPDLOG_INLINE void stdout_sink_base<ConsoleMutex>::flush() {
2021-07-08 07:07:27 -07:00
std::lock_guard<mutex_t> lock(mutex_);
fflush(file_);
}
2024-02-04 13:43:17 +01:00
template <typename ConsoleMutex>
SPDLOG_INLINE void stdout_sink_base<ConsoleMutex>::set_pattern(const std::string &pattern) {
2021-07-08 07:07:27 -07:00
std::lock_guard<mutex_t> lock(mutex_);
formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern));
}
2024-02-04 13:43:17 +01:00
template <typename ConsoleMutex>
SPDLOG_INLINE void stdout_sink_base<ConsoleMutex>::set_formatter(
std::unique_ptr<spdlog::formatter> sink_formatter) {
2021-07-08 07:07:27 -07:00
std::lock_guard<mutex_t> lock(mutex_);
formatter_ = std::move(sink_formatter);
}
// stdout sink
2024-02-04 13:43:17 +01:00
template <typename ConsoleMutex>
2021-07-08 07:07:27 -07:00
SPDLOG_INLINE stdout_sink<ConsoleMutex>::stdout_sink()
2024-02-04 13:43:17 +01:00
: stdout_sink_base<ConsoleMutex>(stdout) {}
2021-07-08 07:07:27 -07:00
// stderr sink
2024-02-04 13:43:17 +01:00
template <typename ConsoleMutex>
2021-07-08 07:07:27 -07:00
SPDLOG_INLINE stderr_sink<ConsoleMutex>::stderr_sink()
2024-02-04 13:43:17 +01:00
: stdout_sink_base<ConsoleMutex>(stderr) {}
2021-07-08 07:07:27 -07:00
2024-02-04 13:43:17 +01:00
} // namespace sinks
2021-07-08 07:07:27 -07:00
// factory methods
2024-02-04 13:43:17 +01:00
template <typename Factory>
SPDLOG_INLINE std::shared_ptr<logger> stdout_logger_mt(const std::string &logger_name) {
2021-07-08 07:07:27 -07:00
return Factory::template create<sinks::stdout_sink_mt>(logger_name);
}
2024-02-04 13:43:17 +01:00
template <typename Factory>
SPDLOG_INLINE std::shared_ptr<logger> stdout_logger_st(const std::string &logger_name) {
2021-07-08 07:07:27 -07:00
return Factory::template create<sinks::stdout_sink_st>(logger_name);
}
2024-02-04 13:43:17 +01:00
template <typename Factory>
SPDLOG_INLINE std::shared_ptr<logger> stderr_logger_mt(const std::string &logger_name) {
2021-07-08 07:07:27 -07:00
return Factory::template create<sinks::stderr_sink_mt>(logger_name);
}
2024-02-04 13:43:17 +01:00
template <typename Factory>
SPDLOG_INLINE std::shared_ptr<logger> stderr_logger_st(const std::string &logger_name) {
2021-07-08 07:07:27 -07:00
return Factory::template create<sinks::stderr_sink_st>(logger_name);
}
2024-02-04 13:43:17 +01:00
} // namespace spdlog