1、更新(2019-12-02):支持流方式打简单日志
#include <sstream>
#include <atltime.h>
#include <iomanip>
#include <string>
using namespace std;
std::string GetExeDir(void)
{
char szFilePath[MAX_PATH + 1] = { 0 };
GetModuleFileNameA(NULL, szFilePath, MAX_PATH);
(strrchr(szFilePath, '\\'))[0] = 0; // 删除文件名,只获得路径字串
string path = szFilePath;
return path;
}
// 日志输出
inline void WriteLog(const string& strLog)
{
CTime time = CTime::GetCurrentTime();
std::stringstream ssmsg;
ssmsg << time.GetYear() << "-" << setw(2) << setfill('0') << time.GetMonth() << "-" << setw(2) << setfill('0') << time.GetDay() << " "
<< setw(2) << setfill('0') << time.GetHour() << ":" << setw(2) << setfill('0') << time.GetMinute() << ":" << setw(2) << setfill('0') << time.GetSecond() << " "
<< "\t" << strLog << std::endl;
std::string strLogPath = GetExeDir() + "\\log";
if (_access(strLogPath.c_str(), 0) == -1)
{
// 不存在则创建
CreateDirectory(strLogPath.c_str(), NULL);
}
std::string strDate = time.Format("%Y-%m-%d").GetBuffer();
strLogPath += "\\GB28181HignLevel_" + strDate + ".log";
FILE * pFile = fopen(strLogPath.c_str(), "ab");
if (NULL != pFile)
{
fprintf(pFile, "%s", ssmsg.str().c_str());
fclose(pFile);
}
}
// 日志宏
#define TRACELOG(message) \
{ \
std::stringstream ssmsg;\
ssmsg << message;\
WriteLog(ssmsg.str());\
}
1.主要是优化宏中的代码量,提高编译速度
2.未测试,可能有坑
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~我是分割线~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1、更新(2018-11-08):支持流方式打简单日志
#include <sstream>
#include <atltime.h>
#include <iomanip>
#include <string>
using namespace std;
std::string GetExeDir(void)
{
char szFilePath[MAX_PATH + 1] = { 0 };
GetModuleFileNameA(NULL, szFilePath, MAX_PATH);
(strrchr(szFilePath, '\\'))[0] = 0; // 删除文件名,只获得路径字串
string path = szFilePath;
return path;
}
static std::string g_strLogPath = GetExeDir() + "\\SearchDemo.txt";
#define TRACE_EXT(message) \
{ \
CTime time = CTime::GetCurrentTime(); \
std::stringstream ssmsg; \
ssmsg << time.GetYear() << "-" << setw(2) << setfill('0') << time.GetMonth() << "-" << setw(2) << setfill('0') << time.GetDay() << " " \
<< setw(2) << setfill('0') << time.GetHour() << ":" << setw(2) << setfill('0') << time.GetMinute() << ":" << setw(2) << setfill('0') << time.GetSecond() << " " \
<< "\t" << message << std::endl;\
FILE* pFile = fopen(g_strLogPath.c_str(), "ab"); \
if (NULL != pFile) \
{ \
fprintf(pFile, "%s", ssmsg.str().c_str()); \
fclose(pFile); \
} \
}
2、使用示例:
TRACE_EXT("根据关键字[" << strKey.GetBuffer() << "]搜索到" << m_vctSignal.size() << "条结果,耗时" << GetTickCount() - nTickTime << "毫秒");
3、输出样式:
2018-11-08 15:25:11 根据关键字[f]搜索到87条结果,耗时78毫秒
2018-11-08 15:25:11 根据关键字[ff]搜索到2条结果,耗时94毫秒
2018-11-08 15:25:11 根据关键字[ffe]搜索到0条结果,耗时78毫秒
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~我是分割线~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1、说明:支持格式化字符串方式打日志
#define TRACE_EXT(xFormat, ...)\
{\
CTime time = CTime::GetCurrentTime();\
FILE *pFile;\
pFile = fopen("C:\\CPGServerLog.txt", "ab");\
static char pszBuf[512];\
sprintf_s(pszBuf, 512, xFormat, __VA_ARGS__);\
std::stringstream ssmsg;\
ssmsg << time.GetYear() << "." << time.GetMonth() << "." << time.GetDay() << " " << time.GetHour() << ":" << time.GetMinute() << ":" << time.GetSecond() << " " << __FILE__ << " [" << __LINE__ << "] " << std::endl << " " << pszBuf << std::endl;\
if (NULL != pFile)\
{\
fprintf(pFile, "%s\n", ssmsg.str().c_str());\
fclose(pFile);\
}\
}
2、使用示例:
string str = "ddddddddddddd";
TRACE_EXT("开始加载数据:%s", str.c_str());
3、输出样式:
2018.11.8 15:34:48 c:\users\administrator\desktop\consoleapplication1\consoleapplication1\consoleapplication1.cpp [41]
开始加载数据:ddddddddddddd