OSVR-Core
LogSinks.h
Go to the documentation of this file.
1 
12 // Copyright 2016 Sensics, Inc.
13 //
14 // Licensed under the Apache License, Version 2.0 (the "License");
15 // you may not use this file except in compliance with the License.
16 // You may obtain a copy of the License at
17 //
18 // http://www.apache.org/licenses/LICENSE-2.0
19 //
20 // Unless required by applicable law or agreed to in writing, software
21 // distributed under the License is distributed on an "AS IS" BASIS,
22 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 // See the License for the specific language governing permissions and
24 // limitations under the License.
25 
26 #ifndef INCLUDED_LogSinks_h_GUID_C582966D_EA04_42D2_83FF_19483537D704
27 #define INCLUDED_LogSinks_h_GUID_C582966D_EA04_42D2_83FF_19483537D704
28 
29 // Internal Includes
30 #include "LogDefaults.h"
31 #include <osvr/Util/PlatformConfig.h>
32 
33 #if defined(OSVR_LINUX) || defined(OSVR_MACOSX)
34 #define OSVR_GET_ANSICOLOR_SINK
35 #elif defined(OSVR_WINDOWS)
36 #define OSVR_GET_WINCOLOR_SINK
37 #endif
38 
39 // Library/third-party includes
40 #include <spdlog/sinks/ostream_sink.h>
41 #ifdef OSVR_GET_ANSICOLOR_SINK
42 #include <spdlog/sinks/ansicolor_sink.h>
43 #endif
44 #ifdef OSVR_GET_WINCOLOR_SINK
45 #include <spdlog/sinks/wincolor_sink.h>
46 #endif
47 #include <spdlog/common.h>
48 #include <spdlog/sinks/base_sink.h>
49 #include <spdlog/sinks/android_sink.h>
50 #include <spdlog/spdlog.h>
51 
52 // Standard includes
53 #include <iostream>
54 #include <mutex>
55 #include <utility> // for std::move
56 
57 namespace osvr {
58 namespace util {
59  namespace log {
60 
66  template <typename Mutex>
67  class stdout_sink : public ::spdlog::sinks::ostream_sink<Mutex> {
68  public:
69  stdout_sink()
70  : ::spdlog::sinks::ostream_sink<Mutex>(std::cout, true) {
71  // do nothing
72  }
73  };
74 
77 
83  template <typename Mutex>
84  class stderr_sink : public ::spdlog::sinks::ostream_sink<Mutex> {
85  public:
86  stderr_sink()
87  : ::spdlog::sinks::ostream_sink<Mutex>(std::cerr, true) {
88  // do nothing
89  }
90  };
91 
94 
97  class filter_sink : public ::spdlog::sinks::sink {
98  public:
101  filter_sink(spdlog::sink_ptr &&wrapped_sink,
102  spdlog::level::level_enum filter_level)
103  : sink_(std::move(wrapped_sink)), level_(filter_level) {}
104 
105  virtual ~filter_sink() {}
106 
109  void set_level(spdlog::level::level_enum filter_level) {
110  level_ = filter_level;
111  }
112 
116  void log(const spdlog::details::log_msg &msg) override {
117  if (msg.level < level_) {
118  return;
119  }
120  sink_->log(msg);
121  }
122 
124  void flush() override { sink_->flush(); }
125 
126  private:
127  spdlog::sink_ptr sink_;
128  spdlog::level::level_enum level_;
129  };
130 
131  static inline spdlog::sink_ptr getUnfilteredConsoleSink() {
132  // Console sink
133  auto console_out = spdlog::sinks::stderr_sink_mt::instance();
134 #if defined(OSVR_GET_ANSICOLOR_SINK)
135  auto color_sink = std::make_shared<spdlog::sinks::ansicolor_sink>(
136  console_out); // taste the rainbow!
137  return color_sink;
138 #elif defined(OSVR_GET_WINCOLOR_SINK)
139  auto color_sink = std::make_shared<spdlog::sinks::wincolor_stderr_sink_mt>();
140  return color_sink;
141 #else
142  // Assumes color not supported on platforms other than Windows,
143  // Linux, and OS X.
144  return console_out;
145 #endif
146  }
147 
148  static inline spdlog::sink_ptr getDefaultUnfilteredSink() {
149 #if defined(OSVR_ANDROID)
150  // Android doesn't have a console, it has logcat.
151  auto android_sink =
152  std::make_shared<spdlog::sinks::android_sink>(
153  ANDROID_LOG_TAG);
154  return android_sink;
155 #else
156  return getUnfilteredConsoleSink();
157 #endif
158  }
159 
160  static inline std::shared_ptr<filter_sink>
161  getDefaultFilteredSink(spdlog::level::level_enum filter_level) {
162  return std::make_shared<filter_sink>(getDefaultUnfilteredSink(),
163  filter_level);
164  }
165 
166  } // end namespace log
167 } // end namespace util
168 } // end namespace osvr
169 
170 #endif // INCLUDED_LogSinks_h_GUID_C582966D_EA04_42D2_83FF_19483537D704
Definition: RunLoopManager.h:42
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
Definition: TypeSafeIdHash.h:44
A sink which sends its output to std::cerr.
Definition: LogSinks.h:84
void set_level(spdlog::level::level_enum filter_level)
Change our internal level threshold, below which we will not pass messages on to the wrapped sink...
Definition: LogSinks.h:109
filter_sink(spdlog::sink_ptr &&wrapped_sink, spdlog::level::level_enum filter_level)
Construct from a wrapped sink rvalue-ref and a filter level threshold.
Definition: LogSinks.h:101
A decorator around another sink that applies a custom log level filter.
Definition: LogSinks.h:97
void log(const spdlog::details::log_msg &msg) override
This is the guts of the implementation: we check the level against our internal level setting and ear...
Definition: LogSinks.h:116
A sink which sends its output to std::cout.
Definition: LogSinks.h:67
void flush() override
Obligatory implementation, just passed through.
Definition: LogSinks.h:124