Skip to content

Commit

Permalink
Add mute logger option
Browse files Browse the repository at this point in the history
  • Loading branch information
nadrino committed May 18, 2023
1 parent b602ee4 commit ba20c49
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
32 changes: 17 additions & 15 deletions include/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@


// Here is what you want to use
#define LogFatal (Logger{Logger::LogLevel::FATAL, FILENAME, __LINE__})
#define LogError (Logger{Logger::LogLevel::ERROR, FILENAME, __LINE__})
#define LogAlert (Logger{Logger::LogLevel::ALERT, FILENAME, __LINE__})
#define LogWarning (Logger{Logger::LogLevel::WARNING, FILENAME, __LINE__})
#define LogInfo (Logger{Logger::LogLevel::INFO, FILENAME, __LINE__})
#define LogDebug (Logger{Logger::LogLevel::DEBUG, FILENAME, __LINE__})
#define LogTrace (Logger{Logger::LogLevel::TRACE, FILENAME, __LINE__})
#define LogFatal (Logger::isMuted() ? LogInvalidImpl : LogFatalImpl)
#define LogError (Logger::isMuted() ? LogInvalidImpl : LogErrorImpl)
#define LogAlert (Logger::isMuted() ? LogInvalidImpl : LogAlertImpl)
#define LogWarning (Logger::isMuted() ? LogInvalidImpl : LogWarningImpl)
#define LogInfo (Logger::isMuted() ? LogInvalidImpl : LogInfoImpl)
#define LogDebug (Logger::isMuted() ? LogInvalidImpl : LogDebugImpl)
#define LogTrace (Logger::isMuted() ? LogInvalidImpl : LogTraceImpl)

// conditional
#define LogFatalIf(isPrint_) (isPrint_ ? LogFatal : LogInvalid)
#define LogErrorIf(isPrint_) (isPrint_ ? LogError : LogInvalid)
#define LogAlertIf(isPrint_) (isPrint_ ? LogAlert : LogInvalid)
#define LogWarningIf(isPrint_) (isPrint_ ? LogWarning : LogInvalid)
#define LogInfoIf(isPrint_) (isPrint_ ? LogInfo : LogInvalid)
#define LogDebugIf(isPrint_) (isPrint_ ? LogDebug : LogInvalid)
#define LogTraceIf(isPrint_) (isPrint_ ? LogTrace : LogInvalid)
#define LogFatalIf(isPrint_) (isPrint_ ? LogFatal : LogInvalidImpl)
#define LogErrorIf(isPrint_) (isPrint_ ? LogError : LogInvalidImpl)
#define LogAlertIf(isPrint_) (isPrint_ ? LogAlert : LogInvalidImpl)
#define LogWarningIf(isPrint_) (isPrint_ ? LogWarning : LogInvalidImpl)
#define LogInfoIf(isPrint_) (isPrint_ ? LogInfo : LogInvalidImpl)
#define LogDebugIf(isPrint_) (isPrint_ ? LogDebug : LogInvalidImpl)
#define LogTraceIf(isPrint_) (isPrint_ ? LogTrace : LogInvalidImpl)

// once
#define LogFatalOnce (Logger{Logger::LogLevel::FATAL, FILENAME, __LINE__, true})
Expand Down Expand Up @@ -108,6 +108,7 @@ namespace {
// It is an inherent feature as a **header-only** library
inline static void setMaxLogLevel(const Logger& logger_); // Example: Logger::setMaxLogLevel(LogDebug);
inline static void setMaxLogLevel(); // Example: LogDebug.setMaxLogLevel();
inline static void setIsMuted(bool isMuted_);
inline static void setEnableColors(bool enableColors_);
inline static void setCleanLineBeforePrint(bool cleanLineBeforePrint);
inline static void setPropagateColorsOnUserHeader(bool propagateColorsOnUserHeader_);
Expand All @@ -119,7 +120,7 @@ namespace {

//! Getters
inline static bool isCleanLineBeforePrint();

inline static bool isMuted();
inline static int getMaxLogLevelInt();
inline static const LogLevel & getMaxLogLevel();
inline static std::string getPrefixString(); // LogWarning.getPrefixString()
Expand Down Expand Up @@ -180,6 +181,7 @@ namespace {
static inline bool _propagateColorsOnUserHeader_{LOGGER_ENABLE_COLORS_ON_USER_HEADER};
static inline bool _cleanLineBeforePrint_{LOGGER_WRITE_OUTFILE};
static inline bool _writeInOutputFile_{false};
static inline bool _isMuted_{false};
static inline std::string _prefixFormat_{};
static inline std::string _indentStr_{};
static inline std::stringstream _userHeaderStr_{};
Expand Down
6 changes: 6 additions & 0 deletions include/implementation/Logger.impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ namespace {
// same technique as other, but this time with no arguments
_maxLogLevel_ = _currentLogLevel_;
}
inline void Logger::setIsMuted(bool isMuted_){
_isMuted_ = isMuted_;
}
inline void Logger::setEnableColors(bool enableColors_) {
_enableColors_ = enableColors_;
}
Expand Down Expand Up @@ -82,6 +85,9 @@ namespace {
inline bool Logger::isCleanLineBeforePrint() {
return _cleanLineBeforePrint_;
}
inline bool Logger::isMuted(){
return _isMuted_;
}
inline int Logger::getMaxLogLevelInt() {
return static_cast<int>(_maxLogLevel_);
}
Expand Down
12 changes: 11 additions & 1 deletion include/implementation/LoggerUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,21 @@
#define CAT(a, b) CAT_(a, b)
#define MAKE_VARNAME_LINE(Var) CAT(Var, __LINE__)

#define LogInvalid (Logger{Logger::LogLevel::INVALID, FILENAME, __LINE__})
#define LogFatalImpl (Logger{Logger::LogLevel::FATAL, FILENAME, __LINE__})
#define LogErrorImpl (Logger{Logger::LogLevel::ERROR, FILENAME, __LINE__})
#define LogAlertImpl (Logger{Logger::LogLevel::ALERT, FILENAME, __LINE__})
#define LogWarningImpl (Logger{Logger::LogLevel::WARNING, FILENAME, __LINE__})
#define LogInfoImpl (Logger{Logger::LogLevel::INFO, FILENAME, __LINE__})
#define LogDebugImpl (Logger{Logger::LogLevel::DEBUG, FILENAME, __LINE__})
#define LogTraceImpl (Logger{Logger::LogLevel::TRACE, FILENAME, __LINE__})
#define LogInvalidImpl (Logger{Logger::LogLevel::INVALID, FILENAME, __LINE__})

#define GET_OVERLOADED_MACRO2(_1,_2,NAME,...) NAME
#define GET_OVERLOADED_MACRO3(_1,_2,_3,NAME,...) NAME




// Header
namespace LoggerUtils{

Expand Down

0 comments on commit ba20c49

Please sign in to comment.