1 | // |
---|
2 | // Copyright(c) 2015 Gabi Melman. |
---|
3 | // Distributed under the MIT License (http://opensource.org/licenses/MIT) |
---|
4 | // |
---|
5 | |
---|
6 | #pragma once |
---|
7 | |
---|
8 | #ifndef SPDLOG_H |
---|
9 | #include "spdlog/spdlog.h" |
---|
10 | #endif |
---|
11 | |
---|
12 | #include "spdlog/details/console_globals.h" |
---|
13 | #include "spdlog/details/null_mutex.h" |
---|
14 | |
---|
15 | #include <cstdio> |
---|
16 | #include <memory> |
---|
17 | #include <mutex> |
---|
18 | |
---|
19 | namespace spdlog { |
---|
20 | |
---|
21 | namespace sinks { |
---|
22 | |
---|
23 | template<typename TargetStream, typename ConsoleMutex> |
---|
24 | class stdout_sink final : public sink |
---|
25 | { |
---|
26 | public: |
---|
27 | using mutex_t = typename ConsoleMutex::mutex_t; |
---|
28 | stdout_sink() |
---|
29 | : mutex_(ConsoleMutex::mutex()) |
---|
30 | , file_(TargetStream::stream()) |
---|
31 | { |
---|
32 | } |
---|
33 | ~stdout_sink() override = default; |
---|
34 | |
---|
35 | stdout_sink(const stdout_sink &other) = delete; |
---|
36 | stdout_sink &operator=(const stdout_sink &other) = delete; |
---|
37 | |
---|
38 | void log(const details::log_msg &msg) override |
---|
39 | { |
---|
40 | std::lock_guard<mutex_t> lock(mutex_); |
---|
41 | fmt::memory_buffer formatted; |
---|
42 | formatter_->format(msg, formatted); |
---|
43 | fwrite(formatted.data(), sizeof(char), formatted.size(), file_); |
---|
44 | fflush(TargetStream::stream()); |
---|
45 | } |
---|
46 | |
---|
47 | void flush() override |
---|
48 | { |
---|
49 | std::lock_guard<mutex_t> lock(mutex_); |
---|
50 | fflush(file_); |
---|
51 | } |
---|
52 | |
---|
53 | void set_pattern(const std::string &pattern) override |
---|
54 | { |
---|
55 | std::lock_guard<mutex_t> lock(mutex_); |
---|
56 | formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern)); |
---|
57 | } |
---|
58 | |
---|
59 | void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) override |
---|
60 | { |
---|
61 | std::lock_guard<mutex_t> lock(mutex_); |
---|
62 | formatter_ = std::move(sink_formatter); |
---|
63 | } |
---|
64 | |
---|
65 | private: |
---|
66 | mutex_t &mutex_; |
---|
67 | FILE *file_; |
---|
68 | }; |
---|
69 | |
---|
70 | using stdout_sink_mt = stdout_sink<details::console_stdout, details::console_mutex>; |
---|
71 | using stdout_sink_st = stdout_sink<details::console_stdout, details::console_nullmutex>; |
---|
72 | |
---|
73 | using stderr_sink_mt = stdout_sink<details::console_stderr, details::console_mutex>; |
---|
74 | using stderr_sink_st = stdout_sink<details::console_stderr, details::console_nullmutex>; |
---|
75 | |
---|
76 | } // namespace sinks |
---|
77 | |
---|
78 | // factory methods |
---|
79 | template<typename Factory = default_factory> |
---|
80 | inline std::shared_ptr<logger> stdout_logger_mt(const std::string &logger_name) |
---|
81 | { |
---|
82 | return Factory::template create<sinks::stdout_sink_mt>(logger_name); |
---|
83 | } |
---|
84 | |
---|
85 | template<typename Factory = default_factory> |
---|
86 | inline std::shared_ptr<logger> stdout_logger_st(const std::string &logger_name) |
---|
87 | { |
---|
88 | return Factory::template create<sinks::stdout_sink_st>(logger_name); |
---|
89 | } |
---|
90 | |
---|
91 | template<typename Factory = default_factory> |
---|
92 | inline std::shared_ptr<logger> stderr_logger_mt(const std::string &logger_name) |
---|
93 | { |
---|
94 | return Factory::template create<sinks::stderr_sink_mt>(logger_name); |
---|
95 | } |
---|
96 | |
---|
97 | template<typename Factory = default_factory> |
---|
98 | inline std::shared_ptr<logger> stderr_logger_st(const std::string &logger_name) |
---|
99 | { |
---|
100 | return Factory::template create<sinks::stderr_sink_st>(logger_name); |
---|
101 | } |
---|
102 | } // namespace spdlog |
---|