spdlog
Loading...
Searching...
No Matches
stdout_sinks-inl.h
Go to the documentation of this file.
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
8#endif
9
12#include <memory>
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
18
19# ifndef _USING_V110_SDK71_ // fileapi.h doesnt 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>
33 : mutex_(ConsoleMutex::mutex())
34 , file_(file)
35 , formatter_(details::make_unique<spdlog::pattern_formatter>())
36{
37#ifdef _WIN32
38 // get windows handle from the FILE* object
39
40 handle_ = (HANDLE)::_get_osfhandle(::_fileno(file_));
41
42 // don't throw to support cases where no console is attached,
43 // and let the log method to do nothing if (handle_ == INVALID_HANDLE_VALUE).
44 // throw only if non stdout/stderr target is requested (probably regular file and not console).
45 if (handle_ == INVALID_HANDLE_VALUE && file != stdout && file != stderr)
46 {
47 throw_spdlog_ex("spdlog::stdout_sink_base: _get_osfhandle() failed", errno);
48 }
49#endif // WIN32
50}
51
52template<typename ConsoleMutex>
54{
55#ifdef _WIN32
56 if (handle_ == INVALID_HANDLE_VALUE)
57 {
58 return;
59 }
60 std::lock_guard<mutex_t> lock(mutex_);
61 memory_buf_t formatted;
62 formatter_->format(msg, formatted);
63 ::fflush(file_); // flush in case there is somthing in this file_ already
64 auto size = static_cast<DWORD>(formatted.size());
65 DWORD bytes_written = 0;
66 bool ok = ::WriteFile(handle_, formatted.data(), size, &bytes_written, nullptr) != 0;
67 if (!ok)
68 {
69 throw_spdlog_ex("stdout_sink_base: WriteFile() failed. GetLastError(): " + std::to_string(::GetLastError()));
70 }
71#else
72 std::lock_guard<mutex_t> lock(mutex_);
73 memory_buf_t formatted;
74 formatter_->format(msg, formatted);
75 ::fwrite(formatted.data(), sizeof(char), formatted.size(), file_);
76 ::fflush(file_); // flush every line to terminal
77#endif // WIN32
78}
79
80template<typename ConsoleMutex>
82{
83 std::lock_guard<mutex_t> lock(mutex_);
84 fflush(file_);
85}
86
87template<typename ConsoleMutex>
93
94template<typename ConsoleMutex>
96{
97 std::lock_guard<mutex_t> lock(mutex_);
98 formatter_ = std::move(sink_formatter);
99}
100
101// stdout sink
102template<typename ConsoleMutex>
106
107// stderr sink
108template<typename ConsoleMutex>
112
113} // namespace sinks
114
115// factory methods
116template<typename Factory>
118{
119 return Factory::template create<sinks::stdout_sink_mt>(logger_name);
120}
121
122template<typename Factory>
124{
125 return Factory::template create<sinks::stdout_sink_st>(logger_name);
126}
127
128template<typename Factory>
130{
131 return Factory::template create<sinks::stderr_sink_mt>(logger_name);
132}
133
134template<typename Factory>
136{
137 return Factory::template create<sinks::stderr_sink_st>(logger_name);
138}
139} // namespace spdlog
void log(const details::log_msg &msg) override
void set_pattern(const std::string &pattern) override
void set_formatter(std::unique_ptr< spdlog::formatter > sink_formatter) override
#define SPDLOG_INLINE
Definition common.h:33
Definition async.h:25
SPDLOG_INLINE std::shared_ptr< logger > stdout_logger_mt(const std::string &logger_name)
SPDLOG_INLINE std::shared_ptr< logger > stderr_logger_st(const std::string &logger_name)
SPDLOG_INLINE std::shared_ptr< logger > stderr_logger_mt(const std::string &logger_name)
SPDLOG_INLINE std::shared_ptr< logger > stdout_logger_st(const std::string &logger_name)
SPDLOG_INLINE void throw_spdlog_ex(const std::string &msg, int last_errno)
Definition common-inl.h:68
fmt::basic_memory_buffer< char, 250 > memory_buf_t
Definition common.h:116
T to_string(T... args)