我编写了自己的简单日志类。我知道我最好使用某种库(boost.log,log4cpp?),但无论如何让我们讨论一下我的简单类:
#include "stdafx.h"
#include "Logger.h"
Logger::Logger(std::string fileName)
{
logFile.open(fileName);
}
Logger::~Logger(void)
{
logFile.close();
}
void Logger::Error(std::string message) {
logFile << message << std::endl;
}
void Logger::Debug(std::string message) {
logFile << message << std::endl;
}
- 我希望我的方法接受可变数量的参数,所以我可以传递这样的参数
"Error code: %x", code
。怎么做? - 如果未设置编译符号,我希望
Debug
排除方法。LOG_DEBUG
在 C# 中,我可以[Conditional("LOG_DEBUG")]
在方法声明之前添加,但现在要在 C++ 中执行此操作?
upd关于1我已经尝试过并且它有效:
void Logger::Debug(std::string message, ...) {
va_list arglist;
fprintf(pFile, message.c_str(), arglist);