a solution for Variadic Macros when using old compilers

following is a classic debug helper macro, it could be compiled with VC2005 and above.

#define LOG_DEBUG(pszfmt, …) {if (LOG_LEVEL_DEBUG < = STATIC_LOG_LEVEL) GetLogger()->Log(LOG_LEVEL_DEBUG,LOG_TYPE_DEFAULT,__FILEW__,__LINE__,__FUNCTIONW__,pszfmt,__VA_ARGS__);}

there’re two points in this macro.
1. use a constant condition to remove the code from the final binary when the condition doesn’t match
2. use variadic macro to pass all user parameters.

this is a workaround ,tested with VC2003

template< bool generate_code = 1 >
class CDbgVarArgHelper_t
{
protected:
LOG_LEVEL m_nLevel;
LOG_TYPES m_nType;
const wchar_t* m_szfile;
unsigned int m_nline;
const wchar_t* m_szfunc;
public:
explicit CDbgVarArgHelper_t(LOG_LEVEL nLevel,LOG_TYPES nType,const wchar_t* szfile,unsigned int nline,const wchar_t* szfunc):
m_nLevel(nLevel),m_nType(nType),m_szfile(szfile),m_nline(nline),m_szfunc(szfunc)
{
}
inline void Log(const char* pszfmt, …)
{
va_list vlArgs;
va_start(vlArgs, pszfmt);
GetLogger()->LogV(m_nLevel,m_nType,m_szfile,m_nline,m_szfunc,pszfmt,vlArgs);
va_end(vlArgs);
}
inline void Log(const wchar_t* pszfmt, …)
{
va_list vlArgs;
va_start(vlArgs, pszfmt);
GetLogger()->LogV(m_nLevel,m_nType,m_szfile,m_nline,m_szfunc,pszfmt,vlArgs);
va_end(vlArgs);
}
#ifdef _INC_UTF8CHAR
inline void Log(const utf8char* pszfmt, …)
{
va_list vlArgs;
va_start(vlArgs, pszfmt);
GetLogger()->LogV(m_nLevel,m_nType,m_szfile,m_nline,m_szfunc,pszfmt,vlArgs);
va_end(vlArgs);
}
#endif
};
template< >
class CDbgVarArgHelper_t< false >
{
public:
explicit CDbgVarArgHelper_t(LOG_LEVEL nLevel,LOG_TYPES nType,const wchar_t* szfile,unsigned int nline,const wchar_t* szfunc)
{
}
inline void Log(const char* pszfmt, …)
{
}
inline void Log(const wchar_t* pszfmt, …)
{
}
#ifdef _INC_UTF8CHAR
inline void Log(const utf8char* pszfmt, …)
{
}
#endif
};
#define LOG_DEBUG CDbgVarArgHelper_t < LOG_LEVEL_DEBUG<=STATIC_LOG_LEVEL > (LOG_LEVEL_DEBUG,LOG_TYPE_DEFAULT,__FILEW__,__LINE__,__FUNCTIONW__).Log

1. it use a template specialization to remove code fron the final binary
2. use a class to hold all internal parameters while sent all other user parameter directly to the final logger.

wordpress removed some code contains template,the code in this article is incomplete, please fix if you use it.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s