Loguru: encapsulate loguru

This commit is contained in:
Vicente Adolfo Bolea Sanchez 2022-02-17 15:54:34 -05:00
parent 65063ca485
commit 26c8252dc5
4 changed files with 158 additions and 30 deletions

@ -225,8 +225,7 @@ InitializeResult Initialize(int& argc, char* argv[], InitializeOptions opts)
<< std::string{ options[opt::OptionIndex::DEPRECATED_LOGLEVEL].name } << ", use "
<< loggingFlag << " instead.");
#ifdef VTKM_ENABLE_LOGGING
loguru::g_stderr_verbosity =
loguru::get_verbosity_from_name(options[opt::OptionIndex::DEPRECATED_LOGLEVEL].arg);
vtkm::cont::SetStderrLogLevel(options[opt::OptionIndex::DEPRECATED_LOGLEVEL].arg);
#endif // VTKM_ENABLE_LOGGING
}

@ -18,6 +18,10 @@
#pragma warning(disable : 4722)
#endif // VTKM_MSVC
#define LOGURU_USE_ANONYMOUS_NAMESPACE
#define LOGURU_WITH_STREAMS 1
#define LOGURU_SCOPE_TIME_PRECISION 6
#include <vtkm/thirdparty/loguru/vtkmloguru/loguru.cpp>
#ifdef VTKM_MSVC
@ -148,6 +152,16 @@ void InitLogging()
InitLogging(argc, argv);
}
VTKM_CONT
void SetStderrLogLevel(const char* verbosity)
{
#ifdef VTKM_ENABLE_LOGGING
loguru::g_stderr_verbosity = loguru::get_verbosity_from_name(verbosity);
#else // VTKM_ENABLE_LOGGING
(void)verbosity;
#endif // VTKM_ENABLE_LOGGING
}
VTKM_CONT
void SetStderrLogLevel(LogLevel level)
{
@ -319,5 +333,54 @@ VTKM_CONT std::string TypeToString(const std::type_index& t)
#endif // VTKM_ENABLE_LOGGING
}
#ifdef VTKM_ENABLE_LOGGING
VTKM_CONT
int getVerbosityByLevel(LogLevel level)
{
return static_cast<loguru::Verbosity>(level);
}
VTKM_CONT
void LogScope(LogLevel level, const char* file, unsigned line, const char* format...)
{
auto verbosity = getVerbosityByLevel(level);
if (verbosity > loguru::current_verbosity_cutoff())
{
loguru::LogScopeRAII();
}
else
{
va_list args;
va_start(args, format);
loguru::LogScopeRAII(verbosity, file, line, format, args);
va_end(args);
}
}
VTKM_CONT
void LogCond(LogLevel level, bool cond, const char* file, unsigned line, const char* format...)
{
if (cond)
{
auto verbosity = getVerbosityByLevel(level);
if (verbosity <= loguru::current_verbosity_cutoff())
{
va_list args;
va_start(args, format);
loguru::vlog(verbosity, file, line, format, args);
va_end(args);
}
}
}
VTKM_CONT
LogCondStream::~LogCondStream() noexcept(false)
{
LogCond(this->Level, this->Condition, this->File, this->Line, this->SStream.str().c_str());
}
#endif // VTKM_ENABLE_LOGGING
}
} // end namespace vtkm::cont

@ -17,27 +17,8 @@
#include <vtkm/cont/vtkm_cont_export.h>
#ifdef VTKM_ENABLE_LOGGING
// disable MSVC warnings in loguru.hpp
#ifdef VTKM_MSVC
#pragma warning(push)
#pragma warning(disable : 4722)
#endif // VTKM_MSVC
#define LOGURU_EXPORT VTKM_CONT_EXPORT
#define LOGURU_WITH_STREAMS 1
#define LOGURU_SCOPE_TIME_PRECISION 6
#include <vtkm/thirdparty/loguru/vtkmloguru/loguru.hpp>
#ifdef VTKM_MSVC
#pragma warning(pop)
#endif // VTKM_MSVC
#else // VTKM_ENABLE_LOGGING
#include <iostream>
#endif // VTKM_ENABLE_LOGGING
#include <sstream>
#include <string>
#include <typeindex>
#include <typeinfo>
@ -208,18 +189,24 @@
#if defined(VTKM_ENABLE_LOGGING)
#define VTKM_LOG_S(level, ...) VLOG_S(static_cast<loguru::Verbosity>(level)) << __VA_ARGS__
#define VTKM_LOG_F(level, ...) VLOG_F(static_cast<loguru::Verbosity>(level), __VA_ARGS__)
#define VTKM_LOG_IF_S(level, cond, ...) \
VLOG_IF_S(static_cast<loguru::Verbosity>(level), cond) << __VA_ARGS__
vtkm::cont::LogCondStream(level, cond, __FILE__, __LINE__) << __VA_ARGS__
#define VTKM_LOG_IF_F(level, cond, ...) \
VLOG_IF_F(static_cast<loguru::Verbosity>(level), cond, __VA_ARGS__)
#define VTKM_LOG_SCOPE(level, ...) VLOG_SCOPE_F(static_cast<loguru::Verbosity>(level), __VA_ARGS__)
#define VTKM_LOG_SCOPE_FUNCTION(level) \
VTKM_LOG_SCOPE(static_cast<loguru::Verbosity>(level), __func__)
#define VTKM_LOG_ERROR_CONTEXT(desc, data) ERROR_CONTEXT(desc, data)
vtkm::cont::LogCond(level, cond, __FILE__, __LINE__, __VA_ARGS__)
#define VTKM_LOG_S(level, ...) VTKM_LOG_IF_S(level, true, __VA_ARGS__)
#define VTKM_LOG_F(level, ...) VTKM_LOG_IF_F(level, true, __VA_ARGS__)
#define VTKM_LOG_SCOPE(level, ...) vtkm::cont::LogScope(level, __FILE__, __LINE__, __VA_ARGS__)
#define VTKM_LOG_SCOPE_FUNCTION(level) VTKM_LOG_SCOPE(level, __func__)
#define VTKM_LOG_ALWAYS_S(level, ...) VTKM_LOG_S(level, __VA_ARGS__)
// VTKM_LOG_ERROR_CONTEXT is disabled as it is deprecated
#define VTKM_LOG_ERROR_CONTEXT(desc, data)
// Convenience macros:
// Cast success:
@ -352,6 +339,7 @@ enum class LogLevel
UserVerboseLast = 2047
};
/**
* This shouldn't be called directly -- prefer calling vtkm::cont::Initialize,
* which takes care of logging as well as other initializations.
@ -379,10 +367,16 @@ void InitLogging();
/**
* Set the range of log levels that will be printed to stderr. All levels
* with an enum value less-than-or-equal-to \a level will be printed.
* @{
*/
VTKM_CONT_EXPORT
VTKM_CONT
void SetStderrLogLevel(const char* verbosity);
VTKM_CONT_EXPORT
VTKM_CONT
void SetStderrLogLevel(vtkm::cont::LogLevel level);
/**@}*/
/**
* Get the active highest log level that will be printed to stderr.
@ -486,6 +480,76 @@ inline VTKM_CONT std::string TypeToString(const T&)
return TypeToString(typeid(T));
}
/**@}*/
#ifdef VTKM_ENABLE_LOGGING
/**
* \brief Conditionally logs a message with a printf-like format.
*
* \param level Desired LogLevel value for the log message.
* \param cond When false this function is no-op.
* \param format Printf like format string.
*/
VTKM_CONT_EXPORT
VTKM_CONT
void LogCond(LogLevel level, bool cond, const char* file, unsigned line, const char* format...);
/**
* \brief Logs a scoped message with a printf-like format.
*
* The indentation level will be determined based on its LogLevel and it will
* print out its wall time upon exiting its scope.
*
* \param level Desired LogLevel value for the log message.
* \param cond When false this function is no-op.
* \param format Printf like format string.
*/
VTKM_CONT_EXPORT
VTKM_CONT
void LogScope(LogLevel level, const char* file, unsigned line, const char* format...);
/**
* \brief Conditionally logs a message with a stream-like interface.
*
* Messages are flushed to output by the destructor.
*/
struct VTKM_CONT_EXPORT LogCondStream
{
VTKM_CONT
LogCondStream(LogLevel level, bool cond, const char* file, int line)
: Level(level)
, Condition(cond)
, File(file)
, Line(line)
{
}
VTKM_CONT
~LogCondStream() noexcept(false);
template <typename T>
VTKM_CONT LogCondStream& operator<<(const T& in)
{
SStream << in;
return *this;
}
VTKM_CONT
LogCondStream& operator<<(std::ostream& (*f)(std::ostream&))
{
f(SStream);
return *this;
}
private:
LogLevel Level;
bool Condition;
const char* File;
int Line;
std::ostringstream SStream;
};
#endif // VTKM_ENABLE_LOGGING
}
} // end namespace vtkm::cont

@ -45,6 +45,8 @@ void Scopes(int level = 0)
}
}
// VTKM_LOG_ERROR_CONTEXT is no longer implemented as it is
// deprecated
void ErrorContext()
{
// These variables are only logged if a crash occurs.