实现 CLogClass 来进行体面的日志记录后,我还定义了宏,但它仅适用于一个参数......

class CLogClass
{ 
public:
       static void DoLog(LPCTSTR sMessage, ...);
};
#define DebugLog(sMessage, x) ClogClass::DoLog(__FILE__, __LINE__, sMessage, x)

好吧,当使用超过 2 个参数调用时它会失败:( ...有可能完全避免吗?可以以某种方式将其翻译为模板吗?

编辑:可变参数宏是在 VS 2005 中引入的(但我目前使用的是 VS 2003...)。有什么建议吗?

有帮助吗?

解决方案

你可以让一个MYLOG宏返回一个自定义函子对象,它接受可变数量的参数。

#include <string>
#include <cstdarg>

struct CLogObject {

  void operator()( const char* pFormat, ... ) const {
    printf( "[%s:%d] ", filename.c_str(), linenumber );
    va_list args;
    va_start( args, pFormat );
    vfprintf( stderr, pFormat, args );
    va_end( args );
  }

  CLogObject( std::string filename, const int linenumber )
    : filename( filename ), linenumber( linenumber )
  {}
  std::string filename;
  int linenumber;
};

#define MYLOG CLogObject( __FILE__, __LINE__ )


int _tmain(int argc, _TCHAR* argv[])
{

  MYLOG( "%s, %d", "string", 5 );
  return 0;
}

请注意,步入这个答案:由于operator<<的链接效应,你不需要任何可变参数。

struct CTSLogObject {

  template< typename T >
  std::ostream& operator<<( const T& t ) const {
    return std::cout << "[" << filename << ":" << linenumber << "] ";
  }

  CTSLogObject( std::string filename, const int linenumber )
    : filename( filename ), linenumber( linenumber )
  {}
  std::string filename;
  int linenumber;
};
#define typesafelog CTSLogObject( __FILE__, __LINE__ )

int _tmain(int argc, _TCHAR* argv[])
{
  typesafelog << "typesafe" << ", " << 5 << std::endl;
  return 0;
}

其他提示

你的问题实际上有两个答案。您想要执行通用日志记录功能,其工作方式类似于 printf 但可以完全自定义。所以你需要:

  • 带有可变数量参数的宏
  • 接受可变数量参数的函数

这是您的代码示例:

#include <stdio.h>
#include <stdarg.h>


class CLogClass
{
public:
    static void DoLogWithFileLineInfo( const char * fmt, ... )
    {
        va_list ap;
        va_start( ap, fmt );
        vfprintf( stderr, fmt, ap );
        va_end( ap );
    }

};


#define MYLOG(format, ...) CLogClass::DoLogWithFileLineInfo("%s:%d " format , __FILE__, __LINE__, __VA_ARGS__)

int main()
{
    MYLOG("Hello world!\n", 3); // you need at least format + one argument to your macro
    MYLOG("%s\n", "Hello world!");
    MYLOG("%s %d\n", "Hello world!", 3);
}

C99 中引入了可变参数宏,因此它可以在支持 C99 或 C++0x 的编译器上运行。我用 gcc 3.4.2 和 Visual Studio 2005 成功测试了它。

函数的可变参数一直存在,所以这里不用担心兼容性。

可能可以通过一些模板元编程来做到这一点,但考虑到上面代码的简单性,我没有看到它的兴趣。

最后一点,为什么在空类中使用静态方法而不是函数?

class Log {
    stringstream buffer;
    public:
        class Proxy {
            public:
                Proxy(Log& p) : parent(p) {}
                template<class T>
                Proxy& operator,(T const& t) {
                    parent.buffer << t;
                    return *this;
                }
                ~Proxy() {
                    parent.buffer << endl;
                    cout << parent.buffer.str();
                    parent.buffer.str("");
                }
            private:
                CLog& parent;
        };

        template<class T>
        Proxy operator<<(T const& t) {
            buffer << t;
            return Proxy(*this);
        }
};

可以简单地扩展为写入时间戳,检查loglevel,写入文件等。

或者,更简单但不那么灵活:

class Log {
    public:
        class Proxy {
            public:
                template<class T>
                Proxy& operator,(T const& t) {
                    cout << t;
                    return *this;
                }
                ~Proxy() {
                    cout << endl;
                }
        };

        template<class T>
        Proxy operator<<(T const& t) {
            cout << t;
            return Proxy();
        }
};

用法:

Log log;
void f() {
     log << "hey, my age is ", age;
}

在这个实例中,我倾向于使用全局可见的extern函数而不是宏,并使用va_list解析此函数中的省略号。请参阅我之前发布的如何实现此目标的示例

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top