31 #ifndef OPENVDB_UTIL_LOGGING_HAS_BEEN_INCLUDED 32 #define OPENVDB_UTIL_LOGGING_HAS_BEEN_INCLUDED 34 #ifdef OPENVDB_USE_LOG4CPLUS 36 #include <log4cplus/appender.h> 37 #include <log4cplus/configurator.h> 38 #include <log4cplus/consoleappender.h> 39 #include <log4cplus/layout.h> 40 #include <log4cplus/logger.h> 41 #include <log4cplus/spi/loggingevent.h> 57 Debug = log4cplus::DEBUG_LOG_LEVEL,
58 Info = log4cplus::INFO_LOG_LEVEL,
59 Warn = log4cplus::WARN_LOG_LEVEL,
60 Error = log4cplus::ERROR_LOG_LEVEL,
61 Fatal = log4cplus::FATAL_LOG_LEVEL
73 : log4cplus::PatternLayout(
74 progName_.empty() ? std::string{
"%5p: %m%n"} : (progName_ +
" %5p: %m%n"))
76 , mProgName(progName_)
82 const std::string&
progName()
const {
return mProgName; }
85 const log4cplus::spi::InternalLoggingEvent& event)
override 88 log4cplus::PatternLayout::formatAndAppend(strm, event);
91 log4cplus::tostringstream s;
92 switch (event.getLogLevel()) {
93 case log4cplus::DEBUG_LOG_LEVEL: s <<
"\033[32m";
break;
94 case log4cplus::ERROR_LOG_LEVEL:
95 case log4cplus::FATAL_LOG_LEVEL: s <<
"\033[31m";
break;
96 case log4cplus::INFO_LOG_LEVEL: s <<
"\033[36m";
break;
97 case log4cplus::WARN_LOG_LEVEL: s <<
"\033[35m";
break;
99 log4cplus::PatternLayout::formatAndAppend(s, event);
100 strm << s.str() <<
"\033[0m" << std::flush;
106 #pragma warning disable:1478 107 #elif defined(__clang__) 108 #pragma clang diagnostic push 109 #pragma clang diagnostic ignored "-Wdeprecated-declarations" 110 #elif defined(__GNUC__) 111 #pragma GCC diagnostic push 112 #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 115 #if defined(LOG4CPLUS_VERSION) && defined(LOG4CPLUS_MAKE_VERSION) 116 #if LOG4CPLUS_VERSION >= LOG4CPLUS_MAKE_VERSION(2, 0, 0) 118 using Ptr = std::unique_ptr<log4cplus::Layout>;
120 using Ptr = std::auto_ptr<log4cplus::Layout>;
123 using Ptr = std::auto_ptr<log4cplus::Layout>;
126 static Ptr create(
const std::string& progName_,
bool useColor =
true)
133 #elif defined(__clang__) 134 #pragma clang diagnostic pop 135 #elif defined(__GNUC__) 136 #pragma GCC diagnostic pop 140 bool mUseColor =
true;
141 std::string mProgName;
145 inline log4cplus::Logger
148 return log4cplus::Logger::getInstance(LOG4CPLUS_TEXT(
"openvdb"));
152 inline log4cplus::SharedAppenderPtr
155 return getLogger().getAppender(LOG4CPLUS_TEXT(
"OPENVDB"));
166 case log4cplus::DEBUG_LOG_LEVEL:
return Level::Debug;
167 case log4cplus::INFO_LOG_LEVEL:
return Level::Info;
168 case log4cplus::WARN_LOG_LEVEL:
return Level::Warn;
169 case log4cplus::ERROR_LOG_LEVEL:
return Level::Error;
170 case log4cplus::FATAL_LOG_LEVEL:
break;
190 for (
int i = 1; i < argc; ++i) {
191 const std::string arg{argv[i]};
193 if (arg ==
"-debug") {
setLevel(Level::Debug); }
194 else if (arg ==
"-error") {
setLevel(Level::Error); }
195 else if (arg ==
"-fatal") {
setLevel(Level::Fatal); }
196 else if (arg ==
"-info") {
setLevel(Level::Info); }
197 else if (arg ==
"-warn") {
setLevel(Level::Warn); }
198 else {
remove =
false; }
199 if (
remove) argv[i] =
nullptr;
201 auto end = std::remove(argv + 1, argv + argc,
nullptr);
202 argc =
static_cast<int>(end - argv);
213 appender->setLayout(internal::ColoredPatternLayout::create(progName, useColor));
231 logger.setAdditivity(
false);
234 if (
auto appender = log4cplus::SharedAppenderPtr{
new log4cplus::ConsoleAppender}) {
235 appender->setName(LOG4CPLUS_TEXT(
"OPENVDB"));
236 logger.addAppender(appender);
255 auto progName = (argc > 0 ? argv[0] :
"");
256 if (
const char* ptr = ::strrchr(progName,
'/')) progName = ptr + 1;
265 #define OPENVDB_LOG(level, message) \ 267 auto _log = openvdb::logging::internal::getLogger(); \ 268 if (_log.isEnabledFor(log4cplus::level##_LOG_LEVEL)) { \ 269 std::ostringstream _buf; \ 271 _log.forcedLog(log4cplus::level##_LOG_LEVEL, _buf.str(), __FILE__, __LINE__); \ 276 #define OPENVDB_LOG_INFO(message) OPENVDB_LOG(INFO, message) 277 #define OPENVDB_LOG_WARN(message) OPENVDB_LOG(WARN, message) 279 #define OPENVDB_LOG_ERROR(message) OPENVDB_LOG(ERROR, message) 281 #define OPENVDB_LOG_FATAL(message) OPENVDB_LOG(FATAL, message) 284 #define OPENVDB_LOG_DEBUG(message) OPENVDB_LOG(DEBUG, message) 287 #define OPENVDB_LOG_DEBUG(message) 290 #define OPENVDB_LOG_DEBUG_RUNTIME(message) OPENVDB_LOG(DEBUG, message) 294 #else // ifdef OPENVDB_USE_LOG4CPLUS 298 #define OPENVDB_LOG_INFO(mesg) 299 #define OPENVDB_LOG_WARN(mesg) do { std::cerr << "WARNING: " << mesg << std::endl; } while (0); 300 #define OPENVDB_LOG_ERROR(mesg) do { std::cerr << "ERROR: " << mesg << std::endl; } while (0); 301 #define OPENVDB_LOG_FATAL(mesg) do { std::cerr << "FATAL: " << mesg << std::endl; } while (0); 302 #define OPENVDB_LOG_DEBUG(mesg) 303 #define OPENVDB_LOG_DEBUG_RUNTIME(mesg) 310 enum class Level { Debug, Info, Warn, Error, Fatal };
314 inline void setLevel(
int&,
char*[]) {}
317 inline void initialize(
int&,
char*[],
bool =
true) {}
323 #endif // OPENVDB_USE_LOG4CPLUS 344 #endif // OPENVDB_UTIL_LOGGING_HAS_BEEN_INCLUDED log4cplus layout that outputs text in different colors for different log levels, using ANSI escape co...
Definition: logging.h:69
~LevelScope()
Definition: logging.h:337
LevelScope(Level newLevel)
Definition: logging.h:336
log4cplus::Logger getLogger()
Definition: logging.h:146
void initialize(int &argc, char *argv[], bool useColor=true)
Initialize the logging system from command-line arguments.
Definition: logging.h:249
Level level
Definition: logging.h:335
void setProgramName(const std::string &progName, bool useColor=true)
Specify a program name to be displayed in log messages.
Definition: logging.h:208
A LevelScope object sets the logging level to a given level and restores it to the current level when...
Definition: logging.h:333
#define OPENVDB_VERSION_NAME
Definition: version.h:43
std::auto_ptr< log4cplus::Layout > Ptr
Definition: logging.h:123
Definition: Exceptions.h:39
Level getLevel()
Return the current logging level.
Definition: logging.h:163
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:188
const std::string & progName() const
Definition: logging.h:82
Level
Message severity level.
Definition: logging.h:56
static Ptr create(const std::string &progName_, bool useColor=true)
Definition: logging.h:126
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
void formatAndAppend(log4cplus::tostream &strm, const log4cplus::spi::InternalLoggingEvent &event) override
Definition: logging.h:84
log4cplus::SharedAppenderPtr getAppender()
Definition: logging.h:153
ColoredPatternLayout(const std::string &progName_, bool useColor=true)
Definition: logging.h:72
~ColoredPatternLayout() override
Definition: logging.h:80