C++临时打日志

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值