4 #ifndef OPENVDB_UTIL_LOGGING_HAS_BEEN_INCLUDED 5 #define OPENVDB_UTIL_LOGGING_HAS_BEEN_INCLUDED 7 #include <openvdb/version.h> 9 #ifdef OPENVDB_USE_LOG4CPLUS 11 #include <log4cplus/appender.h> 12 #include <log4cplus/configurator.h> 13 #include <log4cplus/consoleappender.h> 14 #include <log4cplus/layout.h> 15 #include <log4cplus/logger.h> 16 #include <log4cplus/spi/loggingevent.h> 32 Debug = log4cplus::DEBUG_LOG_LEVEL,
33 Info = log4cplus::INFO_LOG_LEVEL,
34 Warn = log4cplus::WARN_LOG_LEVEL,
35 Error = log4cplus::ERROR_LOG_LEVEL,
36 Fatal = log4cplus::FATAL_LOG_LEVEL
45 class ColoredPatternLayout:
public log4cplus::PatternLayout
48 explicit ColoredPatternLayout(
const std::string& progName_,
bool useColor =
true)
49 : log4cplus::PatternLayout(
50 progName_.empty() ? std::string{
"%5p: %m%n"} : (progName_ +
" %5p: %m%n"))
52 , mProgName(progName_)
56 ~ColoredPatternLayout()
override {}
58 const std::string& progName()
const {
return mProgName; }
60 void formatAndAppend(log4cplus::tostream& strm,
61 const log4cplus::spi::InternalLoggingEvent& event)
override 64 log4cplus::PatternLayout::formatAndAppend(strm, event);
67 log4cplus::tostringstream s;
68 switch (event.getLogLevel()) {
69 case log4cplus::DEBUG_LOG_LEVEL: s <<
"\033[32m";
break;
70 case log4cplus::ERROR_LOG_LEVEL:
71 case log4cplus::FATAL_LOG_LEVEL: s <<
"\033[31m";
break;
72 case log4cplus::INFO_LOG_LEVEL: s <<
"\033[36m";
break;
73 case log4cplus::WARN_LOG_LEVEL: s <<
"\033[35m";
break;
75 log4cplus::PatternLayout::formatAndAppend(s, event);
76 strm << s.str() <<
"\033[0m" << std::flush;
82 #pragma warning disable:1478 83 #elif defined(__clang__) 84 #pragma clang diagnostic push 85 #pragma clang diagnostic ignored "-Wdeprecated-declarations" 86 #elif defined(__GNUC__) 87 #pragma GCC diagnostic push 88 #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 91 #if defined(LOG4CPLUS_VERSION) && defined(LOG4CPLUS_MAKE_VERSION) 92 #if LOG4CPLUS_VERSION >= LOG4CPLUS_MAKE_VERSION(2, 0, 0) 94 using Ptr = std::unique_ptr<log4cplus::Layout>;
96 using Ptr = std::auto_ptr<log4cplus::Layout>;
99 using Ptr = std::auto_ptr<log4cplus::Layout>;
102 static Ptr create(
const std::string& progName_,
bool useColor =
true)
104 return Ptr{
new ColoredPatternLayout{progName_, useColor}};
109 #elif defined(__clang__) 110 #pragma clang diagnostic pop 111 #elif defined(__GNUC__) 112 #pragma GCC diagnostic pop 116 bool mUseColor =
true;
117 std::string mProgName;
121 inline log4cplus::Logger
124 return log4cplus::Logger::getInstance(LOG4CPLUS_TEXT(
"openvdb"));
128 inline log4cplus::SharedAppenderPtr
131 return getLogger().getAppender(LOG4CPLUS_TEXT(
"OPENVDB"));
143 switch (internal::getLogger().getLogLevel()) {
144 case log4cplus::DEBUG_LOG_LEVEL:
return Level::Debug;
145 case log4cplus::INFO_LOG_LEVEL:
return Level::Info;
146 case log4cplus::WARN_LOG_LEVEL:
return Level::Warn;
147 case log4cplus::ERROR_LOG_LEVEL:
return Level::Error;
148 case log4cplus::FATAL_LOG_LEVEL:
break;
158 internal::getLogger().setLogLevel(static_cast<log4cplus::LogLevel>(lvl));
168 for (
int i = 1; i < argc; ++i) {
169 const std::string arg{argv[i]};
171 if (arg ==
"-debug") {
setLevel(Level::Debug); }
172 else if (arg ==
"-error") {
setLevel(Level::Error); }
173 else if (arg ==
"-fatal") {
setLevel(Level::Fatal); }
174 else if (arg ==
"-info") {
setLevel(Level::Info); }
175 else if (arg ==
"-warn") {
setLevel(Level::Warn); }
176 else {
remove =
false; }
177 if (
remove) argv[i] =
nullptr;
179 auto end = std::remove(argv + 1, argv + argc,
nullptr);
180 argc =
static_cast<int>(end - argv);
190 if (
auto appender = internal::getAppender()) {
191 appender->setLayout(internal::ColoredPatternLayout::create(progName, useColor));
202 if (internal::getAppender())
return;
205 auto logger = internal::getLogger();
209 logger.setAdditivity(
false);
212 if (
auto appender = log4cplus::SharedAppenderPtr{
new log4cplus::ConsoleAppender}) {
213 appender->setName(LOG4CPLUS_TEXT(
"OPENVDB"));
214 logger.addAppender(appender);
233 auto progName = (argc > 0 ? argv[0] :
"");
234 if (
const char* ptr = ::strrchr(progName,
'/')) progName = ptr + 1;
243 #define OPENVDB_LOG(level, message) \ 245 auto _log = openvdb::logging::internal::getLogger(); \ 246 if (_log.isEnabledFor(log4cplus::level##_LOG_LEVEL)) { \ 247 std::ostringstream _buf; \ 249 _log.forcedLog(log4cplus::level##_LOG_LEVEL, _buf.str(), __FILE__, __LINE__); \ 254 #define OPENVDB_LOG_INFO(message) OPENVDB_LOG(INFO, message) 256 #define OPENVDB_LOG_WARN(message) OPENVDB_LOG(WARN, message) 258 #define OPENVDB_LOG_ERROR(message) OPENVDB_LOG(ERROR, message) 260 #define OPENVDB_LOG_FATAL(message) OPENVDB_LOG(FATAL, message) 263 #define OPENVDB_LOG_DEBUG(message) OPENVDB_LOG(DEBUG, message) 266 #define OPENVDB_LOG_DEBUG(message) 270 #define OPENVDB_LOG_DEBUG_RUNTIME(message) OPENVDB_LOG(DEBUG, message) 272 #else // ifdef OPENVDB_USE_LOG4CPLUS 276 #define OPENVDB_LOG_INFO(mesg) 277 #define OPENVDB_LOG_WARN(mesg) do { std::cerr << "WARNING: " << mesg << std::endl; } while (0); 278 #define OPENVDB_LOG_ERROR(mesg) do { std::cerr << "ERROR: " << mesg << std::endl; } while (0); 279 #define OPENVDB_LOG_FATAL(mesg) do { std::cerr << "FATAL: " << mesg << std::endl; } while (0); 280 #define OPENVDB_LOG_DEBUG(mesg) 281 #define OPENVDB_LOG_DEBUG_RUNTIME(mesg) 288 enum class Level { Debug, Info, Warn, Error, Fatal };
292 inline void setLevel(
int&,
char*[]) {}
295 inline void initialize(
int&,
char*[],
bool =
true) {}
301 #endif // OPENVDB_USE_LOG4CPLUS 322 #endif // OPENVDB_UTIL_LOGGING_HAS_BEEN_INCLUDED
LevelScope(Level newLevel)
Definition: logging.h:314
Level level
Definition: logging.h:313
A LevelScope object sets the logging level to a given level and restores it to the current level when...
Definition: logging.h:311
Definition: Exceptions.h:13
Level
Message severity level.
Definition: logging.h:31
Level getLevel()
Return the current logging level.
Definition: logging.h:141
void setProgramName(const std::string &progName, bool useColor=true)
Specify a program name to be displayed in log messages.
Definition: logging.h:186
void initialize(int &argc, char *argv[], bool useColor=true)
Initialize the logging system from command-line arguments.
Definition: logging.h:227
~LevelScope()
Definition: logging.h:315
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:166
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:121
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:218