I have a legacy logging printf-like function in C:
namespace legacy
{
void DoLog(const char* format,...);
}
If is only visible if a corresponding macro is defined (this is legacy too):
#ifdef LOG
# define Log legacy::DoLog
#else
# define Log /* Nothing */
#endif
I have some C++ code, which calls this log like:
Log("Logging started!");
When the LOG
macro is not defined, Log("Something");
turns into ("Something");
which is unused code.
I need to suppress these warnings in GCC, and do it only once, of course, so I wrap Log
into MyLog
as #define MyLog Log
and try this:
#define MyLog(...) \
_Pragma("GCC diagnostic push"); \
_Pragma("GCC diagnostic ignored \"-Wunused-value\""); \
Log(__VA_ARGS__); \
_Pragma("GCC diagnostic pop");
But these pragmas for some reason do not have any effect
https://gcc.gnu.org/onlinedocs/cpp/Pragmas.html
https://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html
I would better use inline C++ wrapper with templates for the perfect wrapping, but I cannot cope with it yet...
Any suggestions how to suppress these warnings?
Thank you in advance!
#define Log(...)
?how to suppress these warnings?
I think it is preferable to have printf warnings even with disabled logging. Then, in case you enable logging, your code is valid and you don't have to deal with a wall of warnings. Consider adding__attribute__((__format__(__printf__, 1, 2)))
and doing#define Log(...) 0&&legacy::DoLog(__VA_ARGS__)