From 74c33f637146e63b895c716a8f6db12873eef5fe Mon Sep 17 00:00:00 2001 From: nojhan Date: Sat, 29 May 2021 18:25:54 +0200 Subject: [PATCH] add the CLUTCHCODE macro --- README.md | 12 ++++++++++++ clutchlog/clutchlog.h | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9f6c88a..c7268fd 100644 --- a/README.md +++ b/README.md @@ -313,6 +313,18 @@ Thus, any call like `ASSERT(error, x > 3);` will be declutchable with the same configuration than a call to `CLUTCHLOG`. +(De)clutch any code section +--------------------------- + +The `CLUTCHCODE` macro allows to wrap any code within the current logger. + +For instance: +```cpp +CLUTCHCODE(info, + std::clog << "We are clutched!\n"; +); +``` + Log level semantics =================== diff --git a/clutchlog/clutchlog.h b/clutchlog/clutchlog.h index bef5257..b0e2ce2 100644 --- a/clutchlog/clutchlog.h +++ b/clutchlog/clutchlog.h @@ -116,7 +116,7 @@ } #endif // NDEBUG -//! Call an assert at the given level. +//! Call any function if the scope matches. #ifndef NDEBUG #define CLUTCHFUNC( LEVEL, FUNC, ... ) { \ auto& logger = clutchlog::logger(); \ @@ -126,16 +126,46 @@ } \ } #else // not Debug build. -#define CLUTCHFUNC( LEVEL, FUNC, ... ) { do {/*nothing*/} while(false); } +#define CLUTCHFUNC( LEVEL, FUNC, ... ) { \ + if(clutchlog::level::LEVEL <= CLUTCHLOG_DEFAULT_DEPTH_BUILT_NODEBUG) { \ + auto& logger = clutchlog::logger(); \ + clutchlog::scope_t scope = logger.locate(clutchlog::level::LEVEL, CLUTCHLOC); \ + if(scope.matches) { \ + FUNC(__VA_ARGS__); \ + } \ + } \ +} +#endif // NDEBUG + +//! Run any code if the scope matches. +#ifndef NDEBUG +#define CLUTCHCODE( LEVEL, CODE ) { \ + auto& logger = clutchlog::logger(); \ + clutchlog::scope_t scope = logger.locate(clutchlog::level::LEVEL, CLUTCHLOC); \ + if(scope.matches) { \ + CODE \ + } \ +} +#else // not Debug build. +#define CLUTCHCODE( LEVEL, CODE ) { \ + if(clutchlog::level::LEVEL <= CLUTCHLOG_DEFAULT_DEPTH_BUILT_NODEBUG) { \ + auto& logger = clutchlog::logger(); \ + clutchlog::scope_t scope = logger.locate(clutchlog::level::LEVEL, CLUTCHLOC); \ + if(scope.matches) { \ + CODE \ + } \ + } \ +} #endif // NDEBUG /** @} */ #else // not WITH_CLUTCHLOG -// Disabled macros can still be used in Release builds. +// Disabled macros can still be called in Release builds. #define CLUTCHLOG( LEVEL, WHAT ) { do {/*nothing*/} while(false); } #define CLUTCHDUMP( LEVEL, CONTAINER, FILENAME ) { do {/*nothing*/} while(false); } #define CLUTCHFUNC( LEVEL, FUNC, ... ) { do {/*nothing*/} while(false); } +#define CLUTCHCODE( LEVEL, CODE ) { do {/*nothing*/} while(false); } #endif // WITH_CLUTCHLOG /**********************************************************************