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 | #include <spdlog/details/console_globals.h> |
7 | #include <spdlog/details/synchronous_factory.h> |
8 | #include <spdlog/sinks/sink.h> |
9 | #include <cstdio> |
10 | |
11 | #ifdef _WIN32 |
12 | # include <spdlog/details/windows_include.h> |
13 | #endif |
14 | |
15 | namespace spdlog { |
16 | |
17 | namespace sinks { |
18 | |
19 | template<typename ConsoleMutex> |
20 | class stdout_sink_base : public sink |
21 | { |
22 | public: |
23 | using mutex_t = typename ConsoleMutex::mutex_t; |
24 | explicit stdout_sink_base(FILE *file); |
25 | ~stdout_sink_base() override = default; |
26 | |
27 | stdout_sink_base(const stdout_sink_base &other) = delete; |
28 | stdout_sink_base(stdout_sink_base &&other) = delete; |
29 | |
30 | stdout_sink_base &operator=(const stdout_sink_base &other) = delete; |
31 | stdout_sink_base &operator=(stdout_sink_base &&other) = delete; |
32 | |
33 | void log(const details::log_msg &msg) override; |
34 | void flush() override; |
35 | void set_pattern(const std::string &pattern) override; |
36 | |
37 | void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) override; |
38 | |
39 | protected: |
40 | mutex_t &mutex_; |
41 | FILE *file_; |
42 | std::unique_ptr<spdlog::formatter> formatter_; |
43 | #ifdef _WIN32 |
44 | HANDLE handle_; |
45 | #endif // WIN32 |
46 | }; |
47 | |
48 | template<typename ConsoleMutex> |
49 | class stdout_sink : public stdout_sink_base<ConsoleMutex> |
50 | { |
51 | public: |
52 | stdout_sink(); |
53 | }; |
54 | |
55 | template<typename ConsoleMutex> |
56 | class stderr_sink : public stdout_sink_base<ConsoleMutex> |
57 | { |
58 | public: |
59 | stderr_sink(); |
60 | }; |
61 | |
62 | using stdout_sink_mt = stdout_sink<details::console_mutex>; |
63 | using stdout_sink_st = stdout_sink<details::console_nullmutex>; |
64 | |
65 | using stderr_sink_mt = stderr_sink<details::console_mutex>; |
66 | using stderr_sink_st = stderr_sink<details::console_nullmutex>; |
67 | |
68 | } // namespace sinks |
69 | |
70 | // factory methods |
71 | template<typename Factory = spdlog::synchronous_factory> |
72 | std::shared_ptr<logger> stdout_logger_mt(const std::string &logger_name); |
73 | |
74 | template<typename Factory = spdlog::synchronous_factory> |
75 | std::shared_ptr<logger> stdout_logger_st(const std::string &logger_name); |
76 | |
77 | template<typename Factory = spdlog::synchronous_factory> |
78 | std::shared_ptr<logger> stderr_logger_mt(const std::string &logger_name); |
79 | |
80 | template<typename Factory = spdlog::synchronous_factory> |
81 | std::shared_ptr<logger> stderr_logger_st(const std::string &logger_name); |
82 | |
83 | } // namespace spdlog |
84 | |
85 | #ifdef SPDLOG_HEADER_ONLY |
86 | # include "stdout_sinks-inl.h" |
87 | #endif |
88 | |