我有我的應用程序的不同部分調用記錄器功能來記錄詳細信息。C++連接不同類型爲函數的字符串
Logger類
std::string filename = "blahblah"; // variable to store the location of the properties file
log4cpp::PropertyConfigurator::configure(filename);
void Logger::logging(const std::string& msg)
{
Log4cpp::Category& myLogger = log4cpp::Category::getRoot();
myLogger.log(log4cpp::Priority::INFO, msg);//only takes in string as input
}
調用類
Logger logMe;
int a = 5;
double b = 6;
logMe.logging("log this msg" + a + "," + b);
我意識到,上面給我的錯誤,因爲a
和b
是不同類型的。解決這個問題的方法之一是使用std::to_string
logMe.logging("log this msg" + std::to_string(a) + "," + std::to_string(b));
不過,我有上百個電話到日誌功能,這將是耗時每個調用編輯到std::to_string
。是否有更簡單的方法可以做到這一點?
呵呵並且爲了澄清,代碼之前的工作方式是通過定義#define函數。
#Define logging(FLAG, X)\
do {\
...
clog << x; \
}while(0)
logging(LogFlag::Warning, "log this msg" << a << "," << b << endl);
但我現在正在重寫部分代碼以符合靜態測試。
在此先感謝。
應該以標準庫爲例 - 實現'operator <<'。 – LogicStuff