Conditionally return from function with optional logging.
Conditionally return from function with optional loggingEvaluates a condition and if true, optionally logs a message and returns the specified value. For void functions, use 'void' as the return value.
- Parameters
-
| condition | The condition to evaluate |
| value | The value to return (use ',,' for void functions) |
| ... | Optional format string and arguments for logging |
return_if(ptr == nullptr,,"E::Null pointer encountered");
return_if(error_code != 0, -1, "E::Operation failed with code {}", error_code);
return_if(done, result);
#pragma once
#define return_if(condition, value, ...) \
if (condition) { \
__VA_OPT__(logger(__VA_ARGS__);) \
return value; \
}
#define break_if(condition, ...) \
if (condition) { \
__VA_OPT__(logger(__VA_ARGS__);) \
break; \
}
#define continue_if(condition, ...) \
if (condition) { \
__VA_OPT__(logger(__VA_ARGS__);) \
continue; \
}
#define log_if(condition, ...) \
if (condition) { \
logger(__VA_ARGS__); \
}
A library for file logging.