1// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
2// Distributed under the MIT License (http://opensource.org/licenses/MIT)
3
4#pragma once
5
6#ifndef SPDLOG_HEADER_ONLY
7 #include <spdlog/sinks/stdout_sinks.h>
8#endif
9
10#include <memory>
11#include <spdlog/details/console_globals.h>
12#include <spdlog/pattern_formatter.h>
13
14#ifdef _WIN32
15 // under windows using fwrite to non-binary stream results in \r\r\n (see issue #1675)
16 // so instead we use ::FileWrite
17 #include <spdlog/details/windows_include.h>
18
19 #ifndef _USING_V110_SDK71_ // fileapi.h doesn't exist in winxp
20 #include <fileapi.h> // WriteFile (..)
21 #endif
22
23 #include <io.h> // _get_osfhandle(..)
24 #include <stdio.h> // _fileno(..)
25#endif // WIN32
26
27namespace spdlog {
28
29namespace sinks {
30
31template <typename ConsoleMutex>
32SPDLOG_INLINE stdout_sink_base<ConsoleMutex>::stdout_sink_base(FILE *file)
33 : mutex_(ConsoleMutex::mutex()),
34 file_(file),
35 formatter_(details::make_unique<spdlog::pattern_formatter>()) {
36#ifdef _WIN32
37 // get windows handle from the FILE* object
38
39 handle_ = reinterpret_cast<HANDLE>(::_get_osfhandle(::_fileno(file_)));
40
41 // don't throw to support cases where no console is attached,
42 // and let the log method to do nothing if (handle_ == INVALID_HANDLE_VALUE).
43 // throw only if non stdout/stderr target is requested (probably regular file and not console).
44 if (handle_ == INVALID_HANDLE_VALUE && file != stdout && file != stderr) {
45 throw_spdlog_ex("spdlog::stdout_sink_base: _get_osfhandle() failed", errno);
46 }
47#endif // WIN32
48}
49
50template <typename ConsoleMutex>
51SPDLOG_INLINE void stdout_sink_base<ConsoleMutex>::log(const details::log_msg &msg) {
52#ifdef _WIN32
53 if (handle_ == INVALID_HANDLE_VALUE) {
54 return;
55 }
56 std::lock_guard<mutex_t> lock(mutex_);
57 memory_buf_t formatted;
58 formatter_->format(msg, formatted);
59 auto size = static_cast<DWORD>(formatted.size());
60 DWORD bytes_written = 0;
61 bool ok = ::WriteFile(handle_, formatted.data(), size, &bytes_written, nullptr) != 0;
62 if (!ok) {
63 throw_spdlog_ex("stdout_sink_base: WriteFile() failed. GetLastError(): " +
64 std::to_string(::GetLastError()));
65 }
66#else
67 std::lock_guard<mutex_t> lock(mutex_);
68 memory_buf_t formatted;
69 formatter_->format(msg, dest&: formatted);
70 ::fwrite(ptr: formatted.data(), size: sizeof(char), n: formatted.size(), s: file_);
71#endif // WIN32
72 ::fflush(stream: file_); // flush every line to terminal
73}
74
75template <typename ConsoleMutex>
76SPDLOG_INLINE void stdout_sink_base<ConsoleMutex>::flush() {
77 std::lock_guard<mutex_t> lock(mutex_);
78 fflush(stream: file_);
79}
80
81template <typename ConsoleMutex>
82SPDLOG_INLINE void stdout_sink_base<ConsoleMutex>::set_pattern(const std::string &pattern) {
83 std::lock_guard<mutex_t> lock(mutex_);
84 formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern));
85}
86
87template <typename ConsoleMutex>
88SPDLOG_INLINE void stdout_sink_base<ConsoleMutex>::set_formatter(
89 std::unique_ptr<spdlog::formatter> sink_formatter) {
90 std::lock_guard<mutex_t> lock(mutex_);
91 formatter_ = std::move(sink_formatter);
92}
93
94// stdout sink
95template <typename ConsoleMutex>
96SPDLOG_INLINE stdout_sink<ConsoleMutex>::stdout_sink()
97 : stdout_sink_base<ConsoleMutex>(stdout) {}
98
99// stderr sink
100template <typename ConsoleMutex>
101SPDLOG_INLINE stderr_sink<ConsoleMutex>::stderr_sink()
102 : stdout_sink_base<ConsoleMutex>(stderr) {}
103
104} // namespace sinks
105
106// factory methods
107template <typename Factory>
108SPDLOG_INLINE std::shared_ptr<logger> stdout_logger_mt(const std::string &logger_name) {
109 return Factory::template create<sinks::stdout_sink_mt>(logger_name);
110}
111
112template <typename Factory>
113SPDLOG_INLINE std::shared_ptr<logger> stdout_logger_st(const std::string &logger_name) {
114 return Factory::template create<sinks::stdout_sink_st>(logger_name);
115}
116
117template <typename Factory>
118SPDLOG_INLINE std::shared_ptr<logger> stderr_logger_mt(const std::string &logger_name) {
119 return Factory::template create<sinks::stderr_sink_mt>(logger_name);
120}
121
122template <typename Factory>
123SPDLOG_INLINE std::shared_ptr<logger> stderr_logger_st(const std::string &logger_name) {
124 return Factory::template create<sinks::stderr_sink_st>(logger_name);
125}
126} // namespace spdlog
127