stdout_sinks.h
Go to the documentation of this file.
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 
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;
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:
67  FILE *file_;
68 };
69 
72 
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
basic_memory_buffer< char > memory_buffer
Definition: format.h:553
void msg(const char *fmt,...)
Definition: message.cpp:107
std::string string
Definition: nybbler.cc:12
std::unique_ptr< spdlog::formatter > formatter_
Definition: sink.h:55
void log(const details::log_msg &msg) override
Definition: stdout_sinks.h:38
Definition: async.h:27
std::shared_ptr< logger > stderr_logger_st(const std::string &logger_name)
Definition: stdout_sinks.h:98
~stdout_sink() override=default
def move(depos, offset)
Definition: depos.py:107
typename ConsoleMutex::mutex_t mutex_t
Definition: stdout_sinks.h:27
void set_pattern(const std::string &pattern) override
Definition: stdout_sinks.h:53
std::string pattern
Definition: regex_t.cc:33
std::shared_ptr< logger > stderr_logger_mt(const std::string &logger_name)
Definition: stdout_sinks.h:92
std::shared_ptr< logger > stdout_logger_mt(const std::string &logger_name)
Definition: stdout_sinks.h:80
stdout_sink & operator=(const stdout_sink &other)=delete
std::shared_ptr< logger > stdout_logger_st(const std::string &logger_name)
Definition: stdout_sinks.h:86
void set_formatter(std::unique_ptr< spdlog::formatter > sink_formatter) override
Definition: stdout_sinks.h:59