此博客均属原创或译文,欢迎转载但请注明出处 GithubPage:https://liukang325.github.io
#include <stdio.h>
#include <stdarg.h>
#include <fstream>
#include <iostream>
#include <sstream>
#define MAX_MSG 1000
//#define __D(fmt, args...) printf("debug: " fmt, ## args)
#define LOG(fmt,...) logOut(__FILE__,__FUNCTION__,__LINE__,fmt"",##__VA_ARGS__)
static void logOut(const char *file, const char *func, const int line, const char *fmt, ...)
{
char msg[MAX_MSG] = { 0 };
if (NULL != fmt)
{
va_list ap;
va_start(ap, fmt);
vsnprintf(msg, sizeof(msg), fmt, ap);
va_end(ap);
}
std::string fileStr(file);
int backslashIndex = fileStr.find_last_of('\\');
std::ofstream writeFile("C:\\wps.log", std::ios::app);
if (writeFile.is_open())
{
writeFile << "[DBG] " << fileStr.substr(backslashIndex + 1, -1).c_str() << " " << func << "():" << line << " " << msg << "\r\n";
writeFile.close();
}
}
也可以用QT的库,写入log日志用下面的代码
QFile logFile("C:\\wps.log");
if (logFile.open(QIODevice::Append))
{
QTextStream in(&logFile);
in << "[DBG] " << fileStr.substr(backslashIndex + 1, -1).c_str() << " " << func << "():" << line << " " << msg << "\r\n";
logFile.close();
}