4 #ifndef OPENVDB_UTIL_LOGGING_HAS_BEEN_INCLUDED
5 #define OPENVDB_UTIL_LOGGING_HAS_BEEN_INCLUDED
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;
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()) {
145 case log4cplus::INFO_LOG_LEVEL:
return Level::Info;
146 case log4cplus::WARN_LOG_LEVEL:
return Level::Warn;
148 case log4cplus::FATAL_LOG_LEVEL:
break;
158 internal::getLogger().setLogLevel(static_cast<log4cplus::LogLevel>(lvl));
168 for (
int i = 1; i < argc; ++i) {
176 else {
remove =
false; }
177 if (
remove) argv[i] =
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"));
227 initialize(
int& argc,
char* argv[],
bool useColor =
true)
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)
301 #endif // OPENVDB_USE_LOG4CPLUS
322 #endif // OPENVDB_UTIL_LOGGING_HAS_BEEN_INCLUDED
OIIO_UTIL_API bool remove(string_view path, std::string &err)
GLsizei const GLchar *const * string
#define OPENVDB_USE_VERSION_NAMESPACE
auto arg(const Char *name, const T &arg) -> detail::named_arg< Char, T >
A LevelScope object sets the logging level to a given level and restores it to the current level when...
void initialize(int &, char *[], bool=true)
void setProgramName(const std::string &, bool=true)
Library and file format version numbers.
LevelScope(Level newLevel)
void initialize()
Global registration of native Grid, Transform, Metadata and Point attribute types. Also initializes blosc (if enabled).
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.