bitz-server  2.0.3
stdout_sinks.h
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 #include "../details/null_mutex.h"
9 #include "base_sink.h"
10 
11 #include <cstdio>
12 #include <memory>
13 #include <mutex>
14 
15 namespace spdlog {
16 namespace sinks {
17 
18 template<class Mutex>
19 class stdout_sink SPDLOG_FINAL : public base_sink<Mutex>
20 {
21  using MyType = stdout_sink<Mutex>;
22 
23 public:
24  explicit stdout_sink() = default;
25 
26  static std::shared_ptr<MyType> instance()
27  {
28  static std::shared_ptr<MyType> instance = std::make_shared<MyType>();
29  return instance;
30  }
31 
32 protected:
33  void _sink_it(const details::log_msg &msg) override
34  {
35  fwrite(msg.formatted.data(), sizeof(char), msg.formatted.size(), stdout);
36  _flush();
37  }
38 
39  void _flush() override
40  {
41  fflush(stdout);
42  }
43 };
44 
45 using stdout_sink_mt = stdout_sink<std::mutex>;
46 using stdout_sink_st = stdout_sink<details::null_mutex>;
47 
48 template<class Mutex>
49 class stderr_sink SPDLOG_FINAL : public base_sink<Mutex>
50 {
51  using MyType = stderr_sink<Mutex>;
52 
53 public:
54  explicit stderr_sink() = default;
55 
56  static std::shared_ptr<MyType> instance()
57  {
58  static std::shared_ptr<MyType> instance = std::make_shared<MyType>();
59  return instance;
60  }
61 
62 protected:
63  void _sink_it(const details::log_msg &msg) override
64  {
65  fwrite(msg.formatted.data(), sizeof(char), msg.formatted.size(), stderr);
66  _flush();
67  }
68 
69  void _flush() override
70  {
71  fflush(stderr);
72  }
73 };
74 
75 using stderr_sink_mt = stderr_sink<std::mutex>;
76 using stderr_sink_st = stderr_sink<details::null_mutex>;
77 
78 } // namespace sinks
79 } // namespace spdlog
Definition: async_logger.h:26