OSVR-Core  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
Logger.h
Go to the documentation of this file.
1 
14 // Copyright 2016 Sensics, Inc.
15 //
16 // Licensed under the Apache License, Version 2.0 (the "License");
17 // you may not use this file except in compliance with the License.
18 // You may obtain a copy of the License at
19 //
20 // http://www.apache.org/licenses/LICENSE-2.0
21 //
22 // Unless required by applicable law or agreed to in writing, software
23 // distributed under the License is distributed on an "AS IS" BASIS,
24 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 // See the License for the specific language governing permissions and
26 // limitations under the License.
27 
28 #ifndef INCLUDED_Logger_h_GUID_D8ADC0E7_A358_4FF2_960F_10F098A22F4E
29 #define INCLUDED_Logger_h_GUID_D8ADC0E7_A358_4FF2_960F_10F098A22F4E
30 
31 // Internal Includes
32 #include <osvr/Util/Export.h>
33 #include <osvr/Util/Log.h> // for LoggerPtr
34 #include <osvr/Util/LogLevel.h>
35 
36 // Library/third-party includes
37 // - none
38 
39 // Standard includes
40 #include <initializer_list>
41 #include <memory> // for std::shared_ptr
42 #include <sstream> // for std::ostringstream
43 #include <string> // for std::string
44 
45 // Forward declarations
46 
47 namespace spdlog {
48 class logger;
49 namespace sinks {
50  class sink;
51 } // namespace sinks
52 
53 using sink_ptr = std::shared_ptr<spdlog::sinks::sink>;
54 using sinks_init_list = std::initializer_list<sink_ptr>;
55 } // namespace spdlog
56 
57 namespace osvr {
58 namespace util {
59  namespace log {
60 
67  class Logger {
68  private:
69  struct PrivateConstructor;
70 
71  public:
74  Logger(std::string const &name,
75  std::shared_ptr<spdlog::logger> logger,
76  PrivateConstructor *);
77 
82  OSVR_UTIL_EXPORT static LoggerPtr makeFromExistingImplementation(
83  std::string const &name,
84  std::shared_ptr<spdlog::logger> logger);
85 
91  OSVR_UTIL_EXPORT static LoggerPtr
92  makeWithSink(std::string const &name, spdlog::sink_ptr sink);
93 
99  OSVR_UTIL_EXPORT static LoggerPtr
100  makeWithSinks(std::string const &name,
101  spdlog::sinks_init_list sinks);
102 
104  OSVR_UTIL_EXPORT Logger(const Logger &) = delete;
105 
107  OSVR_UTIL_EXPORT Logger &operator=(const Logger &) = delete;
108 
110  OSVR_UTIL_EXPORT ~Logger();
111 
114  OSVR_UTIL_EXPORT LogLevel getLogLevel() const;
115 
118  OSVR_UTIL_EXPORT void setLogLevel(LogLevel level);
119 
121  OSVR_UTIL_EXPORT void flushOn(LogLevel level);
122 
127  class StreamProxy {
128  public:
129  StreamProxy(Logger &logger, LogLevel level)
130  : logger_(logger), level_(level),
131  os_(new std::ostringstream) {}
132 
133  StreamProxy(Logger &logger, LogLevel level,
134  const std::string &msg)
135  : logger_(logger), level_(level),
136  os_(new std::ostringstream) {
137  (*os_) << msg;
138  }
139 
143  if (active_ && os_) {
144  logger_.write(level_, os_->str().c_str());
145  }
146  }
147 
150  : logger_(other.logger_), level_(other.level_),
151  os_(std::move(other.os_)), active_(other.active_) {
152  other.active_ = false;
153  }
154 
155  StreamProxy(StreamProxy const &) = delete;
156  StreamProxy &operator=(StreamProxy const &) = delete;
157 
158  operator std::ostream &() { return (*os_); }
159 
160  template <typename T> std::ostream &operator<<(T &&what) {
161  (*os_) << std::forward<T>(what);
162  return (*os_);
163  }
164 
165  private:
166  Logger &logger_;
167  LogLevel level_;
168  std::unique_ptr<std::ostringstream> os_;
169  bool active_ = true;
170  };
171 
175  OSVR_UTIL_EXPORT StreamProxy trace(const char *msg);
176  OSVR_UTIL_EXPORT StreamProxy debug(const char *msg);
177  OSVR_UTIL_EXPORT StreamProxy info(const char *msg);
178  OSVR_UTIL_EXPORT StreamProxy notice(const char *msg);
179  OSVR_UTIL_EXPORT StreamProxy warn(const char *msg);
180  OSVR_UTIL_EXPORT StreamProxy error(const char *msg);
181  OSVR_UTIL_EXPORT StreamProxy critical(const char *msg);
183 
186  OSVR_UTIL_EXPORT StreamProxy trace();
187  OSVR_UTIL_EXPORT StreamProxy debug();
188  OSVR_UTIL_EXPORT StreamProxy info();
189  OSVR_UTIL_EXPORT StreamProxy notice();
190  OSVR_UTIL_EXPORT StreamProxy warn();
191  OSVR_UTIL_EXPORT StreamProxy error();
192  OSVR_UTIL_EXPORT StreamProxy critical();
194 
197  OSVR_UTIL_EXPORT StreamProxy log(LogLevel level, const char *msg);
198 
200  OSVR_UTIL_EXPORT StreamProxy log(LogLevel level);
201 
203  OSVR_UTIL_EXPORT void flush();
204 
206  OSVR_UTIL_EXPORT std::string const &getName() const {
207  return name_;
208  }
209 
210  private:
211  static LoggerPtr
212  makeLogger(std::string const &name,
213  std::shared_ptr<spdlog::logger> const &logger);
214 
217  static LoggerPtr makeFallback(std::string const &name);
218 
220  OSVR_UTIL_EXPORT void write(LogLevel level, const char* msg);
221 
222  const std::string name_;
223  std::shared_ptr<spdlog::logger> logger_;
224  };
225 
226  } // namespace log
227 } // namespace util
228 } // namespace osvr
229 
230 #endif // INCLUDED_Logger_h_GUID_D8ADC0E7_A358_4FF2_960F_10F098A22F4E
The main namespace for all C++ elements of the framework, internal and external.
Definition: ClientKit.h:31
STL namespace.
Header for basic internal log reference. To actually log to the produced loggers, include
void setLogLevel(LogLevel level)
Definition: Logger.h:47
std::string const & getName() const
Get the logger name.
Definition: Logger.h:206
static LoggerPtr makeWithSinks(std::string const &name, spdlog::sinks_init_list sinks)
LogLevel getLogLevel() const
An object allowing you to log messages with a given log source name.
Definition: Logger.h:67
Header.
static LoggerPtr makeWithSink(std::string const &name, spdlog::sink_ptr sink)
StreamProxy(StreamProxy &&other)
move construction
Definition: Logger.h:149
StreamProxy log(LogLevel level, const char *msg)
Logger(std::string const &name, std::shared_ptr< spdlog::logger > logger, PrivateConstructor *)
Logger & operator=(const Logger &)=delete
Non-copy-assignable.
void flushOn(LogLevel level)
Set the log level at which this logger will trigger a flush.
static LoggerPtr makeFromExistingImplementation(std::string const &name, std::shared_ptr< spdlog::logger > logger)
void flush()
Make sure this logger has written out its data.