OSVR-Core
LogRegistry.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_LogRegistry_h_GUID_09DDD840_389E_430C_8CBD_9AC4EE3F93FE
27 #define INCLUDED_LogRegistry_h_GUID_09DDD840_389E_430C_8CBD_9AC4EE3F93FE
28 
29 // Internal Includes
30 #include <osvr/Util/Log.h> // for LoggerPtr forward declaration
31 #include <osvr/Util/LogLevel.h>
32 #include <osvr/Util/Export.h>
33 
34 // Library/third-party includes
35 // - none
36 
37 // Standard includes
38 #include <memory> // for std::shared_ptr
39 #include <string> // for std::string
40 #include <vector> // for std::vector
41 
42 // Forward declarations
43 namespace spdlog {
44 
45 namespace sinks {
46  class sink;
47  class filter_sink;
48 } // namespace sinks
49 
50 using sink_ptr = std::shared_ptr<spdlog::sinks::sink>;
51 
52 } // namespace spdlog
53 
54 namespace osvr {
55 namespace util {
56  namespace log {
57  class filter_sink;
58 
59  class LogRegistry {
60  public:
61  LogRegistry(LogRegistry const &) = delete; // copy construct
62  LogRegistry(LogRegistry &&) = delete; // move construct
63  LogRegistry &operator=(LogRegistry const &) = delete; // copy assign
64  LogRegistry &operator=(LogRegistry &&) = delete; // move assign
65 
66  OSVR_UTIL_EXPORT static LogRegistry &instance(std::string const * = nullptr);
67 
77  OSVR_UTIL_EXPORT LoggerPtr getOrCreateLogger(const std::string &logger_name);
78 
86  OSVR_UTIL_EXPORT void drop(const std::string &name);
87 
94  OSVR_UTIL_EXPORT void dropAll();
95 
99  OSVR_UTIL_EXPORT void flush();
100 
104  OSVR_UTIL_EXPORT void setPattern(const std::string &pattern);
105 
110  OSVR_UTIL_EXPORT void setLevel(LogLevel severity);
111 
116  OSVR_UTIL_EXPORT void setConsoleLevel(LogLevel severity);
117 
118  std::string const &getLogFileBaseName() const {
119  return logFileBaseName_;
120  }
121 
122  bool couldOpenLogFile() const { return sinks_.size() > 1; }
123 
124  protected:
125  OSVR_UTIL_EXPORT LogRegistry(std::string const &logFileBaseName);
126  OSVR_UTIL_EXPORT ~LogRegistry();
127 
128  private:
129  void setLevelImpl(LogLevel severity);
130  void setConsoleLevelImpl(LogLevel severity);
131  void createFileSink();
132  LogLevel minLevel_;
133  LogLevel consoleLevel_;
134 
135  std::vector<spdlog::sink_ptr> sinks_;
136  std::shared_ptr<filter_sink> console_filter_;
137  LoggerPtr consoleOnlyLog_;
138  LoggerPtr generalLog_;
139  Logger *generalPurposeLog_ = nullptr;
140  std::string logFileBaseName_;
141  };
142 
143  } // namespace log
144 } // namespace util
145 } // namespace osvr
146 
147 #endif // INCLUDED_LogRegistry_h_GUID_09DDD840_389E_430C_8CBD_9AC4EE3F93FE
Definition: RunLoopManager.h:42
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
OSVR_UTIL_EXPORT void dropAll()
Removes all the registered loggers from the registry.
Definition: Log.cpp:96
OSVR_UTIL_EXPORT void flush()
For implementations with a centralized logger registry, flush all logger sinks.
Definition: Log.cpp:100
Header for basic internal log reference.
LogLevel
Log message severity levels.
Definition: LogLevel.h:45
Definition: Logger.h:47
Definition: LogRegistry.h:59
An object allowing you to log messages with a given log source name.
Definition: Logger.h:67
Header.
OSVR_UTIL_EXPORT void drop(const std::string &logger_name)
Drops a logger from the registry.
Definition: Log.cpp:92