31 #ifndef OPENVDB_UTIL_LOGGING_HAS_BEEN_INCLUDED
32 #define OPENVDB_UTIL_LOGGING_HAS_BEEN_INCLUDED
36 #ifdef OPENVDB_USE_LOG4CPLUS
38 #include <log4cplus/appender.h>
39 #include <log4cplus/configurator.h>
40 #include <log4cplus/consoleappender.h>
41 #include <log4cplus/layout.h>
42 #include <log4cplus/logger.h>
43 #include <log4cplus/spi/loggingevent.h>
59 Debug = log4cplus::DEBUG_LOG_LEVEL,
60 Info = log4cplus::INFO_LOG_LEVEL,
61 Warn = log4cplus::WARN_LOG_LEVEL,
62 Error = log4cplus::ERROR_LOG_LEVEL,
63 Fatal = log4cplus::FATAL_LOG_LEVEL
75 : log4cplus::PatternLayout(
76 progName_.empty() ?
std::string{
"%5p: %m%n"} : (progName_ +
" %5p: %m%n"))
78 , mProgName(progName_)
84 const std::string&
progName()
const {
return mProgName; }
87 const log4cplus::spi::InternalLoggingEvent& event)
override
90 log4cplus::PatternLayout::formatAndAppend(strm, event);
93 log4cplus::tostringstream s;
94 switch (event.getLogLevel()) {
95 case log4cplus::DEBUG_LOG_LEVEL: s <<
"\033[32m";
break;
96 case log4cplus::ERROR_LOG_LEVEL:
97 case log4cplus::FATAL_LOG_LEVEL: s <<
"\033[31m";
break;
98 case log4cplus::INFO_LOG_LEVEL: s <<
"\033[36m";
break;
99 case log4cplus::WARN_LOG_LEVEL: s <<
"\033[35m";
break;
101 log4cplus::PatternLayout::formatAndAppend(s, event);
102 strm << s.str() <<
"\033[0m" << std::flush;
108 #pragma warning disable:1478
109 #elif defined(__clang__)
110 #pragma clang diagnostic push
111 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
112 #elif defined(__GNUC__)
113 #pragma GCC diagnostic push
114 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
117 #if defined(LOG4CPLUS_VERSION) && defined(LOG4CPLUS_MAKE_VERSION)
118 #if LOG4CPLUS_VERSION >= LOG4CPLUS_MAKE_VERSION(2, 0, 0)
120 using Ptr = std::unique_ptr<log4cplus::Layout>;
122 using Ptr = std::auto_ptr<log4cplus::Layout>;
125 using Ptr = std::auto_ptr<log4cplus::Layout>;
128 static Ptr create(
const std::string& progName_,
bool useColor =
true)
135 #elif defined(__clang__)
136 #pragma clang diagnostic pop
137 #elif defined(__GNUC__)
138 #pragma GCC diagnostic pop
142 bool mUseColor =
true;
143 std::string mProgName;
147 inline log4cplus::Logger
150 return log4cplus::Logger::getInstance(LOG4CPLUS_TEXT(
"openvdb"));
154 inline log4cplus::SharedAppenderPtr
157 return getLogger().getAppender(LOG4CPLUS_TEXT(
"OPENVDB"));
168 case log4cplus::DEBUG_LOG_LEVEL:
return Level::Debug;
169 case log4cplus::INFO_LOG_LEVEL:
return Level::Info;
170 case log4cplus::WARN_LOG_LEVEL:
return Level::Warn;
171 case log4cplus::ERROR_LOG_LEVEL:
return Level::Error;
172 case log4cplus::FATAL_LOG_LEVEL:
break;
192 for (
int i = 1; i < argc; ++i) {
193 const std::string arg{argv[i]};
195 if (arg ==
"-debug") {
setLevel(Level::Debug); }
196 else if (arg ==
"-error") {
setLevel(Level::Error); }
197 else if (arg ==
"-fatal") {
setLevel(Level::Fatal); }
198 else if (arg ==
"-info") {
setLevel(Level::Info); }
199 else if (arg ==
"-warn") {
setLevel(Level::Warn); }
200 else { remove =
false; }
201 if (remove) argv[i] =
nullptr;
203 auto end = std::remove(argv + 1, argv + argc,
nullptr);
204 argc = static_cast<int>(end - argv);
215 appender->setLayout(internal::ColoredPatternLayout::create(progName, useColor));
233 logger.setAdditivity(
false);
236 if (
auto appender = log4cplus::SharedAppenderPtr{
new log4cplus::ConsoleAppender}) {
237 appender->setName(LOG4CPLUS_TEXT(
"OPENVDB"));
238 logger.addAppender(appender);
257 auto progName = (argc > 0 ? argv[0] :
"");
258 if (
const char* ptr = ::strrchr(progName,
'/')) progName = ptr + 1;
267 #define OPENVDB_LOG(level, message) \
269 auto _log = openvdb::logging::internal::getLogger(); \
270 if (_log.isEnabledFor(log4cplus::level##_LOG_LEVEL)) { \
271 std::ostringstream _buf; \
273 _log.forcedLog(log4cplus::level##_LOG_LEVEL, _buf.str(), __FILE__, __LINE__); \
278 #define OPENVDB_LOG_INFO(message) OPENVDB_LOG(INFO, message)
279 #define OPENVDB_LOG_WARN(message) OPENVDB_LOG(WARN, message)
281 #define OPENVDB_LOG_ERROR(message) OPENVDB_LOG(ERROR, message)
283 #define OPENVDB_LOG_FATAL(message) OPENVDB_LOG(FATAL, message)
286 #define OPENVDB_LOG_DEBUG(message) OPENVDB_LOG(DEBUG, message)
289 #define OPENVDB_LOG_DEBUG(message)
292 #define OPENVDB_LOG_DEBUG_RUNTIME(message) OPENVDB_LOG(DEBUG, message)
296 #else // ifdef OPENVDB_USE_LOG4CPLUS
300 #define OPENVDB_LOG_INFO(mesg)
301 #define OPENVDB_LOG_WARN(mesg) do { std::cerr << "WARNING: " << mesg << std::endl; } while (0);
302 #define OPENVDB_LOG_ERROR(mesg) do { std::cerr << "ERROR: " << mesg << std::endl; } while (0);
303 #define OPENVDB_LOG_FATAL(mesg) do { std::cerr << "FATAL: " << mesg << std::endl; } while (0);
304 #define OPENVDB_LOG_DEBUG(mesg)
305 #define OPENVDB_LOG_DEBUG_RUNTIME(mesg)
312 enum class Level { Debug, Info, Warn, Error, Fatal };
316 inline void setLevel(
int&,
char*[]) {}
319 inline void initialize(
int&,
char*[],
bool =
true) {}
325 #endif // OPENVDB_USE_LOG4CPLUS
346 #endif // OPENVDB_UTIL_LOGGING_HAS_BEEN_INCLUDED