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
48 : log4cplus::PatternLayout(
49 progName_.empty() ?
std::string{
"%5p: %m%n"} : (progName_ +
" %5p: %m%n"))
51 , mProgName(progName_)
57 const std::string&
progName()
const {
return mProgName; }
60 const log4cplus::spi::InternalLoggingEvent& event)
override
63 log4cplus::PatternLayout::formatAndAppend(strm, event);
66 log4cplus::tostringstream s;
67 switch (event.getLogLevel()) {
68 case log4cplus::DEBUG_LOG_LEVEL: s <<
"\033[32m";
break;
69 case log4cplus::ERROR_LOG_LEVEL:
70 case log4cplus::FATAL_LOG_LEVEL: s <<
"\033[31m";
break;
71 case log4cplus::INFO_LOG_LEVEL: s <<
"\033[36m";
break;
72 case log4cplus::WARN_LOG_LEVEL: s <<
"\033[35m";
break;
74 log4cplus::PatternLayout::formatAndAppend(s, event);
75 strm << s.str() <<
"\033[0m" << std::flush;
81 #pragma warning disable:1478
82 #elif defined(__clang__)
83 #pragma clang diagnostic push
84 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
85 #elif defined(__GNUC__)
86 #pragma GCC diagnostic push
87 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
90 #if defined(LOG4CPLUS_VERSION) && defined(LOG4CPLUS_MAKE_VERSION)
91 #if LOG4CPLUS_VERSION >= LOG4CPLUS_MAKE_VERSION(2, 0, 0)
93 using Ptr = std::unique_ptr<log4cplus::Layout>;
95 using Ptr = std::auto_ptr<log4cplus::Layout>;
98 using Ptr = std::auto_ptr<log4cplus::Layout>;
101 static Ptr create(
const std::string& progName_,
bool useColor =
true)
108 #elif defined(__clang__)
109 #pragma clang diagnostic pop
110 #elif defined(__GNUC__)
111 #pragma GCC diagnostic pop
115 bool mUseColor =
true;
116 std::string mProgName;
120 inline log4cplus::Logger
123 return log4cplus::Logger::getInstance(LOG4CPLUS_TEXT(
"openvdb"));
127 inline log4cplus::SharedAppenderPtr
130 return getLogger().getAppender(LOG4CPLUS_TEXT(
"OPENVDB"));
141 case log4cplus::DEBUG_LOG_LEVEL:
return Level::Debug;
142 case log4cplus::INFO_LOG_LEVEL:
return Level::Info;
143 case log4cplus::WARN_LOG_LEVEL:
return Level::Warn;
144 case log4cplus::ERROR_LOG_LEVEL:
return Level::Error;
145 case log4cplus::FATAL_LOG_LEVEL:
break;
165 for (
int i = 1; i < argc; ++i) {
166 const std::string arg{argv[i]};
168 if (arg ==
"-debug") {
setLevel(Level::Debug); }
169 else if (arg ==
"-error") {
setLevel(Level::Error); }
170 else if (arg ==
"-fatal") {
setLevel(Level::Fatal); }
171 else if (arg ==
"-info") {
setLevel(Level::Info); }
172 else if (arg ==
"-warn") {
setLevel(Level::Warn); }
173 else { remove =
false; }
174 if (remove) argv[i] =
nullptr;
176 auto end = std::remove(argv + 1, argv + argc,
nullptr);
177 argc =
static_cast<int>(end - argv);
188 appender->setLayout(internal::ColoredPatternLayout::create(progName, useColor));
206 logger.setAdditivity(
false);
209 if (
auto appender = log4cplus::SharedAppenderPtr{
new log4cplus::ConsoleAppender}) {
210 appender->setName(LOG4CPLUS_TEXT(
"OPENVDB"));
211 logger.addAppender(appender);
230 auto progName = (argc > 0 ? argv[0] :
"");
231 if (
const char* ptr = ::strrchr(progName,
'/')) progName = ptr + 1;
240 #define OPENVDB_LOG(level, message) \
242 auto _log = openvdb::logging::internal::getLogger(); \
243 if (_log.isEnabledFor(log4cplus::level##_LOG_LEVEL)) { \
244 std::ostringstream _buf; \
246 _log.forcedLog(log4cplus::level##_LOG_LEVEL, _buf.str(), __FILE__, __LINE__); \
251 #define OPENVDB_LOG_INFO(message) OPENVDB_LOG(INFO, message)
253 #define OPENVDB_LOG_WARN(message) OPENVDB_LOG(WARN, message)
255 #define OPENVDB_LOG_ERROR(message) OPENVDB_LOG(ERROR, message)
257 #define OPENVDB_LOG_FATAL(message) OPENVDB_LOG(FATAL, message)
260 #define OPENVDB_LOG_DEBUG(message) OPENVDB_LOG(DEBUG, message)
263 #define OPENVDB_LOG_DEBUG(message)
267 #define OPENVDB_LOG_DEBUG_RUNTIME(message) OPENVDB_LOG(DEBUG, message)
273 #define OPENVDB_LOG_INFO(mesg)
274 #define OPENVDB_LOG_WARN(mesg) do { std::cerr << "WARNING: " << mesg << std::endl; } while (0);
275 #define OPENVDB_LOG_ERROR(mesg) do { std::cerr << "ERROR: " << mesg << std::endl; } while (0);
276 #define OPENVDB_LOG_FATAL(mesg) do { std::cerr << "FATAL: " << mesg << std::endl; } while (0);
277 #define OPENVDB_LOG_DEBUG(mesg)
278 #define OPENVDB_LOG_DEBUG_RUNTIME(mesg)
285 enum class Level { Debug, Info, Warn, Error, Fatal };
289 inline void setLevel(
int&,
char*[]) {}
292 inline void initialize(
int&,
char*[],
bool =
true) {}
log4cplus layout that outputs text in different colors for different log levels, using ANSI escape co...
Definition: logging.h:45
static Ptr create(const std::string &progName_, bool useColor=true)
Definition: logging.h:101
std::auto_ptr< log4cplus::Layout > Ptr
Definition: logging.h:98
ColoredPatternLayout(const std::string &progName_, bool useColor=true)
Definition: logging.h:47
void formatAndAppend(log4cplus::tostream &strm, const log4cplus::spi::InternalLoggingEvent &event) override
Definition: logging.h:59
const std::string & progName() const
Definition: logging.h:57
~ColoredPatternLayout() override
Definition: logging.h:55
log4cplus::SharedAppenderPtr getAppender()
Definition: logging.h:128
log4cplus::Logger getLogger()
Definition: logging.h:121
Level
Message severity level.
Definition: logging.h:31
void setProgramName(const std::string &progName, bool useColor=true)
Specify a program name to be displayed in log messages.
Definition: logging.h:183
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:163
void initialize(int &argc, char *argv[], bool useColor=true)
Initialize the logging system from command-line arguments.
Definition: logging.h:224
Level getLevel()
Return the current logging level.
Definition: logging.h:138
Definition: openvdb/Exceptions.h:13
A LevelScope object sets the logging level to a given level and restores it to the current level when...
Definition: logging.h:309
Level level
Definition: logging.h:310
LevelScope(Level newLevel)
Definition: logging.h:311
~LevelScope()
Definition: logging.h:312
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:116
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:178