Skip to content
Snippets Groups Projects
Verified Commit 30be911e authored by Stephan Lachnit's avatar Stephan Lachnit
Browse files

log: ensure __LINE__ is evaluated in log var

See allpix-squared/allpix-squared!954

 for details.

Signed-off-by: default avatarStephan Lachnit <stephanlachnit@debian.org>
parent 6141f0fe
No related branches found
No related tags found
No related merge requests found
...@@ -273,13 +273,18 @@ namespace corryvreckan { ...@@ -273,13 +273,18 @@ namespace corryvreckan {
*/ */
#define LOG_ONCE(level) LOG_N(level, 1) #define LOG_ONCE(level) LOG_N(level, 1)
///@{
/** /**
* @brief Generator for a local variable to hold the logging count of a message * @brief Macros to generate and retrieve line-specific local variables to hold the logging count of a message
* @param Count Number of allowed counts *
* @return Local counter variable * Note: the double concat macro is needed to ensure __LINE__ is evaluated, see https://stackoverflow.com/a/19666216/17555746
*/ */
#define GENERATE_LOG_VAR(Count) static std::atomic<int> local___FUNCTION__##Count##__LINE__(Count) #define CONCAT_IMPL(x, y) x##y
#define GET_LOG_VARIABLE(Count) local___FUNCTION__##Count##__LINE__ #define CONCAT(x, y) CONCAT_IMPL(x, y)
#define GENERATE_LOG_VAR(Count) \
static std::atomic<int> CONCAT(local___FUNCTION__, __LINE__) { Count }
#define GET_LOG_VARIABLE() CONCAT(local___FUNCTION__, __LINE__)
///@}
/** /**
* @brief Create a logging stream if the reporting level is high enough and this message has not yet been logged more than * @brief Create a logging stream if the reporting level is high enough and this message has not yet been logged more than
...@@ -289,12 +294,12 @@ namespace corryvreckan { ...@@ -289,12 +294,12 @@ namespace corryvreckan {
*/ */
#define LOG_N(level, max_log_count) \ #define LOG_N(level, max_log_count) \
GENERATE_LOG_VAR(max_log_count); \ GENERATE_LOG_VAR(max_log_count); \
if(GET_LOG_VARIABLE(max_log_count) > 0) \ if(GET_LOG_VARIABLE() > 0) \
if(corryvreckan::LogLevel::level <= corryvreckan::Log::getReportingLevel() && \ if(corryvreckan::LogLevel::level <= corryvreckan::Log::getReportingLevel() && \
!corryvreckan::Log::getStreams().empty()) \ !corryvreckan::Log::getStreams().empty()) \
corryvreckan::Log().getStream( \ corryvreckan::Log().getStream( \
corryvreckan::LogLevel::level, __FILE_NAME__, std::string(static_cast<const char*>(__func__)), __LINE__) \ corryvreckan::LogLevel::level, __FILE_NAME__, std::string(static_cast<const char*>(__func__)), __LINE__) \
<< std::string(--GET_LOG_VARIABLE(max_log_count) == 0 ? "[further messages suppressed] " : "") << ((--GET_LOG_VARIABLE() == 0) ? "[further messages suppressed] " : "")
/** /**
* @brief Suppress a stream from writing any output * @brief Suppress a stream from writing any output
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment