Our C/C++ project is using a new internal script that liberally wraps every function in SWIG to make available to python scripts. It chokes on our logger function since SWIG cannot wrap variadic functions well.
So I decided to hide the variadic functionality in a macro that SWIG will never trip over, shown in a simplified example below:
#include <iostream>
void LogPrint(char *file, int line, char* msg)
{
std::cout << file << ":" << line;
std::cout << " [ " << msg << " ] ";
std::cout << std::endl;
}
#define MAX_LOG 256
#define LogPrintf(msg, args...) \
{ \
char *msg_buffer = new char[MAX_LOG]; \
snprintf(msg_buffer, MAX_LOG, msg, ##args); \
Log(__FILE__, __LINE__, msg_buffer); \
delete [] msg_buffer; \
}
main()
{
LogPrintf("%s = %f", "tau", 6.28318);
LogPrintf("%s = %f", "pi", 3.14159);
}
Is this okay? Are there better solutions to this problem? Thanks.