ostream_sink.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 
13 #include "spdlog/sinks/base_sink.h"
14 
15 #include <mutex>
16 #include <ostream>
17 
18 namespace spdlog {
19 namespace sinks {
20 template<typename Mutex>
21 class ostream_sink final : public base_sink<Mutex>
22 {
23 public:
24  explicit ostream_sink(std::ostream &os, bool force_flush = false)
25  : ostream_(os)
26  , force_flush_(force_flush)
27  {
28  }
29  ostream_sink(const ostream_sink &) = delete;
30  ostream_sink &operator=(const ostream_sink &) = delete;
31 
32 protected:
33  void sink_it_(const details::log_msg &msg) override
34  {
35  fmt::memory_buffer formatted;
36  sink::formatter_->format(msg, formatted);
37  ostream_.write(formatted.data(), static_cast<std::streamsize>(formatted.size()));
38  if (force_flush_)
39  {
40  ostream_.flush();
41  }
42  }
43 
44  void flush_() override
45  {
46  ostream_.flush();
47  }
48 
49  std::ostream &ostream_;
51 };
52 
55 
56 } // namespace sinks
57 } // namespace spdlog
basic_memory_buffer< char > memory_buffer
Definition: format.h:553
void msg(const char *fmt,...)
Definition: message.cpp:107
std::unique_ptr< spdlog::formatter > formatter_
Definition: sink.h:55
ostream_sink(std::ostream &os, bool force_flush=false)
Definition: ostream_sink.h:24
void sink_it_(const details::log_msg &msg) override
Definition: ostream_sink.h:33
Definition: async.h:27
ostream_sink & operator=(const ostream_sink &)=delete