Photon 1.0.0
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 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{
29
30 namespace sinks
31 {
32
33 template <typename ConsoleMutex>
35 : mutex_(ConsoleMutex::mutex()), file_(file), formatter_(details::make_unique<spdlog::pattern_formatter>())
36 {
37#ifdef _WIN32
38 // get windows handle from the FILE* object
39
40 handle_ = reinterpret_cast<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
52 template <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 something 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
80 template <typename ConsoleMutex>
82 {
83 std::lock_guard<mutex_t> lock(mutex_);
84 fflush(file_);
85 }
86
87 template <typename ConsoleMutex>
89 {
90 std::lock_guard<mutex_t> lock(mutex_);
91 formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern));
92 }
93
94 template <typename ConsoleMutex>
95 SPDLOG_INLINE void stdout_sink_base<ConsoleMutex>::set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter)
96 {
97 std::lock_guard<mutex_t> lock(mutex_);
98 formatter_ = std::move(sink_formatter);
99 }
100
101 // stdout sink
102 template <typename ConsoleMutex>
107
108 // stderr sink
109 template <typename ConsoleMutex>
114
115 } // namespace sinks
116
117 // factory methods
118 template <typename Factory>
119 SPDLOG_INLINE std::shared_ptr<logger> stdout_logger_mt(const std::string& logger_name)
120 {
121 return Factory::template create<sinks::stdout_sink_mt>(logger_name);
122 }
123
124 template <typename Factory>
125 SPDLOG_INLINE std::shared_ptr<logger> stdout_logger_st(const std::string& logger_name)
126 {
127 return Factory::template create<sinks::stdout_sink_st>(logger_name);
128 }
129
130 template <typename Factory>
131 SPDLOG_INLINE std::shared_ptr<logger> stderr_logger_mt(const std::string& logger_name)
132 {
133 return Factory::template create<sinks::stderr_sink_mt>(logger_name);
134 }
135
136 template <typename Factory>
137 SPDLOG_INLINE std::shared_ptr<logger> stderr_logger_st(const std::string& logger_name)
138 {
139 return Factory::template create<sinks::stderr_sink_st>(logger_name);
140 }
141} // namespace spdlog
Definition pattern_formatter.h:79
stderr_sink()
Definition stdout_sinks-inl.h:110
Definition stdout_sinks.h:23
FILE * file_
Definition stdout_sinks.h:43
void flush() override
Definition stdout_sinks-inl.h:81
void log(const details::log_msg &msg) override
Definition stdout_sinks-inl.h:53
void set_pattern(const std::string &pattern) override
Definition stdout_sinks-inl.h:88
stdout_sink_base(FILE *file)
Definition stdout_sinks-inl.h:34
void set_formatter(std::unique_ptr< spdlog::formatter > sink_formatter) override
Definition stdout_sinks-inl.h:95
stdout_sink()
Definition stdout_sinks-inl.h:103
#define SPDLOG_INLINE
Definition common.h:47
Definition async.h:26
SPDLOG_INLINE std::shared_ptr< logger > stdout_logger_mt(const std::string &logger_name)
Definition stdout_sinks-inl.h:119
SPDLOG_INLINE std::shared_ptr< logger > stderr_logger_st(const std::string &logger_name)
Definition stdout_sinks-inl.h:137
SPDLOG_INLINE std::shared_ptr< logger > stderr_logger_mt(const std::string &logger_name)
Definition stdout_sinks-inl.h:131
SPDLOG_INLINE std::shared_ptr< logger > stdout_logger_st(const std::string &logger_name)
Definition stdout_sinks-inl.h:125
SPDLOG_INLINE void throw_spdlog_ex(const std::string &msg, int last_errno)
Definition common-inl.h:75
fmt::basic_memory_buffer< char, 250 > memory_buf_t
Definition common.h:173
Definition log_msg.h:14
annotation details
Definition tag_strings.h:125