OpenVDB  5.0.0
logging.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2017 DreamWorks Animation LLC
4 //
5 // All rights reserved. This software is distributed under the
6 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
7 //
8 // Redistributions of source code must retain the above copyright
9 // and license notice and the following restrictions and disclaimer.
10 //
11 // * Neither the name of DreamWorks Animation nor the names of
12 // its contributors may be used to endorse or promote products derived
13 // from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 // IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
27 // LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
28 //
30 
31 #ifndef OPENVDB_UTIL_LOGGING_HAS_BEEN_INCLUDED
32 #define OPENVDB_UTIL_LOGGING_HAS_BEEN_INCLUDED
33 
34 #ifdef OPENVDB_USE_LOG4CPLUS
35 
36 #include <openvdb/version.h>
37 #include <log4cplus/appender.h>
38 #include <log4cplus/configurator.h>
39 #include <log4cplus/consoleappender.h>
40 #include <log4cplus/layout.h>
41 #include <log4cplus/logger.h>
42 #include <log4cplus/spi/loggingevent.h>
43 #include <algorithm> // for std::remove()
44 #include <cstring> // for ::strrchr()
45 #include <memory>
46 #include <sstream>
47 #include <string>
48 #include <vector>
49 
50 
51 namespace openvdb {
53 namespace OPENVDB_VERSION_NAME {
54 namespace logging {
55 
57 enum class Level {
58  Debug = log4cplus::DEBUG_LOG_LEVEL,
59  Info = log4cplus::INFO_LOG_LEVEL,
60  Warn = log4cplus::WARN_LOG_LEVEL,
61  Error = log4cplus::ERROR_LOG_LEVEL,
62  Fatal = log4cplus::FATAL_LOG_LEVEL
63 };
64 
65 
66 namespace internal {
67 
70 class ColoredPatternLayout: public log4cplus::PatternLayout
71 {
72 public:
73  explicit ColoredPatternLayout(const std::string& progName_, bool useColor = true)
74  : log4cplus::PatternLayout(
75  progName_.empty() ? std::string{"%5p: %m%n"} : (progName_ + " %5p: %m%n"))
76  , mUseColor(useColor)
77  , mProgName(progName_)
78  {
79  }
80 
81  ~ColoredPatternLayout() override {}
82 
83  const std::string& progName() const { return mProgName; }
84 
85  void formatAndAppend(log4cplus::tostream& strm,
86  const log4cplus::spi::InternalLoggingEvent& event) override
87  {
88  if (!mUseColor) {
89  log4cplus::PatternLayout::formatAndAppend(strm, event);
90  return;
91  }
92  log4cplus::tostringstream s;
93  switch (event.getLogLevel()) {
94  case log4cplus::DEBUG_LOG_LEVEL: s << "\033[32m"; break; // green
95  case log4cplus::ERROR_LOG_LEVEL:
96  case log4cplus::FATAL_LOG_LEVEL: s << "\033[31m"; break; // red
97  case log4cplus::INFO_LOG_LEVEL: s << "\033[36m"; break; // cyan
98  case log4cplus::WARN_LOG_LEVEL: s << "\033[35m"; break; // magenta
99  }
100  log4cplus::PatternLayout::formatAndAppend(s, event);
101  strm << s.str() << "\033[0m" << std::flush;
102  }
103 
104 // Disable deprecation warnings for std::auto_ptr.
105 #if defined(__ICC)
106  #pragma warning push
107  #pragma warning disable:1478
108 #elif defined(__clang__)
109  #pragma clang diagnostic push
110  #pragma clang diagnostic ignored "-Wdeprecated-declarations"
111 #elif defined(__GNUC__)
112  #pragma GCC diagnostic push
113  #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
114 #endif
115 
116 #if defined(LOG4CPLUS_VERSION) && defined(LOG4CPLUS_MAKE_VERSION)
117  #if LOG4CPLUS_VERSION >= LOG4CPLUS_MAKE_VERSION(2, 0, 0)
118  // In log4cplus 2.0.0, std::auto_ptr was replaced with std::unique_ptr.
119  using Ptr = std::unique_ptr<log4cplus::Layout>;
120  #else
121  using Ptr = std::auto_ptr<log4cplus::Layout>;
122  #endif
123 #else
124  using Ptr = std::auto_ptr<log4cplus::Layout>;
125 #endif
126 
127  static Ptr create(const std::string& progName_, bool useColor = true)
128  {
129  return Ptr{new ColoredPatternLayout{progName_, useColor}};
130  }
131 
132 #if defined(__ICC)
133  #pragma warning pop
134 #elif defined(__clang__)
135  #pragma clang diagnostic pop
136 #elif defined(__GNUC__)
137  #pragma GCC diagnostic pop
138 #endif
139 
140 private:
141  bool mUseColor = true;
142  std::string mProgName;
143 }; // class ColoredPatternLayout
144 
145 
146 inline log4cplus::Logger
148 {
149  return log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("openvdb"));
150 }
151 
152 
153 inline log4cplus::SharedAppenderPtr
155 {
156  return getLogger().getAppender(LOG4CPLUS_TEXT("OPENVDB"));
157 }
158 
159 } // namespace internal
160 
161 
163 inline Level
165 {
166  switch (internal::getLogger().getLogLevel()) {
167  case log4cplus::DEBUG_LOG_LEVEL: return Level::Debug;
168  case log4cplus::INFO_LOG_LEVEL: return Level::Info;
169  case log4cplus::WARN_LOG_LEVEL: return Level::Warn;
170  case log4cplus::ERROR_LOG_LEVEL: return Level::Error;
171  case log4cplus::FATAL_LOG_LEVEL: break;
172  }
173  return Level::Fatal;
174 }
175 
176 
178 inline void
180 {
181  internal::getLogger().setLogLevel(static_cast<log4cplus::LogLevel>(lvl));
182 }
183 
184 
188 inline void
189 setLevel(int& argc, char* argv[])
190 {
191  for (int i = 1; i < argc; ++i) { // note: skip argv[0]
192  const std::string arg{argv[i]};
193  bool remove = true;
194  if (arg == "-debug") { setLevel(Level::Debug); }
195  else if (arg == "-error") { setLevel(Level::Error); }
196  else if (arg == "-fatal") { setLevel(Level::Fatal); }
197  else if (arg == "-info") { setLevel(Level::Info); }
198  else if (arg == "-warn") { setLevel(Level::Warn); }
199  else { remove = false; }
200  if (remove) argv[i] = nullptr;
201  }
202  auto end = std::remove(argv + 1, argv + argc, nullptr);
203  argc = static_cast<int>(end - argv);
204 }
205 
206 
208 inline void
209 setProgramName(const std::string& progName, bool useColor = true)
210 {
211  // Change the layout of the OpenVDB appender to use colored text
212  // and to incorporate the supplied program name.
213  if (auto appender = internal::getAppender()) {
214  appender->setLayout(internal::ColoredPatternLayout::create(progName, useColor));
215  }
216 }
217 
218 
220 inline void
221 initialize(bool useColor = true)
222 {
224 
225  if (internal::getAppender()) return; // already initialized
226 
227  // Create the OpenVDB logger if it doesn't already exist.
228  auto logger = internal::getLogger();
229 
230  // Disable "additivity", so that OpenVDB-related messages are directed
231  // to the OpenVDB logger only and are not forwarded up the logger tree.
232  logger.setAdditivity(false);
233 
234  // Attach a console appender to the OpenVDB logger.
235  if (auto appender = log4cplus::SharedAppenderPtr{new log4cplus::ConsoleAppender}) {
236  appender->setName(LOG4CPLUS_TEXT("OPENVDB"));
237  logger.addAppender(appender);
238  }
239 
240  setLevel(Level::Warn);
241  setProgramName("", useColor);
242 }
243 
244 
249 inline void
250 initialize(int& argc, char* argv[], bool useColor = true)
251 {
252  initialize();
253 
254  setLevel(argc, argv);
255 
256  auto progName = (argc > 0 ? argv[0] : "");
257  if (const char* ptr = ::strrchr(progName, '/')) progName = ptr + 1;
258  setProgramName(progName, useColor);
259 }
260 
261 } // namespace logging
262 } // namespace OPENVDB_VERSION_NAME
263 } // namespace openvdb
264 
265 
266 #define OPENVDB_LOG(level, message) \
267  do { \
268  auto _log = openvdb::logging::internal::getLogger(); \
269  if (_log.isEnabledFor(log4cplus::level##_LOG_LEVEL)) { \
270  std::ostringstream _buf; \
271  _buf << message; \
272  _log.forcedLog(log4cplus::level##_LOG_LEVEL, _buf.str(), __FILE__, __LINE__); \
273  } \
274  } while (0);
275 
277 #define OPENVDB_LOG_INFO(message) OPENVDB_LOG(INFO, message)
278 #define OPENVDB_LOG_WARN(message) OPENVDB_LOG(WARN, message)
280 #define OPENVDB_LOG_ERROR(message) OPENVDB_LOG(ERROR, message)
282 #define OPENVDB_LOG_FATAL(message) OPENVDB_LOG(FATAL, message)
284 #ifdef DEBUG
285 #define OPENVDB_LOG_DEBUG(message) OPENVDB_LOG(DEBUG, message)
287 #else
288 #define OPENVDB_LOG_DEBUG(message)
290 #endif
291 #define OPENVDB_LOG_DEBUG_RUNTIME(message) OPENVDB_LOG(DEBUG, message)
294 
295 #else // ifdef OPENVDB_USE_LOG4CPLUS
296 
297 #include <iostream>
298 
299 #define OPENVDB_LOG_INFO(mesg)
300 #define OPENVDB_LOG_WARN(mesg) do { std::cerr << "WARNING: " << mesg << std::endl; } while (0);
301 #define OPENVDB_LOG_ERROR(mesg) do { std::cerr << "ERROR: " << mesg << std::endl; } while (0);
302 #define OPENVDB_LOG_FATAL(mesg) do { std::cerr << "FATAL: " << mesg << std::endl; } while (0);
303 #define OPENVDB_LOG_DEBUG(mesg)
304 #define OPENVDB_LOG_DEBUG_RUNTIME(mesg)
305 
306 namespace openvdb {
308 namespace OPENVDB_VERSION_NAME {
309 namespace logging {
310 
311 enum class Level { Debug, Info, Warn, Error, Fatal };
312 
313 inline Level getLevel() { return Level::Warn; }
314 inline void setLevel(Level) {}
315 inline void setLevel(int&, char*[]) {}
316 inline void setProgramName(const std::string&, bool = true) {}
317 inline void initialize() {}
318 inline void initialize(int&, char*[], bool = true) {}
319 
320 } // namespace logging
321 } // namespace OPENVDB_VERSION_NAME
322 } // namespace openvdb
323 
324 #endif // OPENVDB_USE_LOG4CPLUS
325 
326 
327 namespace openvdb {
329 namespace OPENVDB_VERSION_NAME {
330 namespace logging {
331 
335 {
337  explicit LevelScope(Level newLevel): level(getLevel()) { setLevel(newLevel); }
338  ~LevelScope() { setLevel(level); }
339 };
340 
341 } // namespace logging
342 } // namespace OPENVDB_VERSION_NAME
343 } // namespace openvdb
344 
345 #endif // OPENVDB_UTIL_LOGGING_HAS_BEEN_INCLUDED
346 
347 // Copyright (c) 2012-2017 DreamWorks Animation LLC
348 // All rights reserved. This software is distributed under the
349 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
void setProgramName(const std::string &progName, bool useColor=true)
Specify a program name to be displayed in log messages.
Definition: logging.h:209
Level
Message severity level.
Definition: logging.h:57
std::auto_ptr< log4cplus::Layout > Ptr
Definition: logging.h:124
log4cplus::Logger getLogger()
Definition: logging.h:147
void initialize(int &argc, char *argv[], bool useColor=true)
Initialize the logging system from command-line arguments.
Definition: logging.h:250
log4cplus layout that outputs text in different colors for different log levels, using ANSI escape co...
Definition: logging.h:70
Level getLevel()
Return the current logging level.
Definition: logging.h:164
log4cplus::SharedAppenderPtr getAppender()
Definition: logging.h:154
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:136
void setLevel(int &argc, char *argv[])
If "-debug", "-info", "-warn", "-error" or "-fatal" is found in the given array of command-line argum...
Definition: logging.h:189
A LevelScope object sets the logging level to a given level and restores it to the current level when...
Definition: logging.h:334
Definition: Exceptions.h:39
const std::string & progName() const
Definition: logging.h:83
void formatAndAppend(log4cplus::tostream &strm, const log4cplus::spi::InternalLoggingEvent &event) override
Definition: logging.h:85
Library and file format version numbers.
~LevelScope()
Definition: logging.h:338
LevelScope(Level newLevel)
Definition: logging.h:337
static Ptr create(const std::string &progName_, bool useColor=true)
Definition: logging.h:127
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:188
Level level
Definition: logging.h:336
ColoredPatternLayout(const std::string &progName_, bool useColor=true)
Definition: logging.h:73
~ColoredPatternLayout() override
Definition: logging.h:81