简单的C++log类,持续更新

这个是个单独的类,Windows,vs2015以上可以使用

使用方法:包含了这个Log.h后,你就可以用LOG(x)来使用了,还可以LOGDETAIL(x); dll中使用有问题

#pragma once
#include <sys/timeb.h>
#include <time.h>
#include <atlstr.h>
#include <Windows.h>
#include <ShellAPI.h>
#include <iostream>
#include <string>
using namespace std;



class InfoLog
{
private:
	InfoLog()
	{
		
		InitializeCriticalSection(&m_CriticalFileWrite);
		const int buffSize = 1024;
		char buffFilePath[buffSize] = { 0 };
		GetModuleFileNameA(GetModuleHandleA(NULL), buffFilePath, buffSize);
		//std::cout<<buff<<endl;
		//PathRemoveExtensionA(buff);
		PathRenameExtensionA(buffFilePath, ".log");
		//std::cout<<PathFindFileNameA(buffFilePath)<<endl;
		
		struct tm newtime = { 0 };
		__int64 ltime = 0;

		_time64(&ltime);
		this->m_logFileName = buffFilePath;;
		// Obtain coordinated universal time:
		errno_t errNum = _gmtime64_s(&newtime, &ltime); // C4996
		if (errNum == 0)
		{
			char buffFileName[buffSize] = { 0 };
			sprintf_s(buffFileName, buffSize, "%d-%d-%d-", 1900 + newtime.tm_year, newtime.tm_mon + 1, newtime.tm_mday);
			std::string tmFileName = buffFileName;
			tmFileName += PathFindFileNameA(buffFilePath);
			
#ifdef _DEBUG
			this->m_logFileName = tmFileName;
#else
			char OutPath[MAX_PATH];
			int Strlen = GetTempPathA(MAX_PATH, OutPath);
			this->m_logFileName = OutPath + tmFileName;
#endif
				
		}
		fopen_s(&m_pw, this->m_logFileName.c_str(), "a+");

	}

public:
	static InfoLog& GetSigleInfoLog()   //单例模式最简单代码,需要C++11, GCC > 4.3, VS2015支持该特性
	{
		static InfoLog m_Instance;
		return m_Instance;
	}
	~InfoLog() 
	{ 
		if (m_pw)
		{
			fclose(m_pw);
			m_pw = nullptr;
		}
		DeleteCriticalSection(&m_CriticalFileWrite);

		
	};
	void WriteLog(int infoText)
	{
		char buff[100] = { 0 };
		sprintf_s(buff, 100, "%d", infoText);
		WriteLog(buff);
	}

	void WriteLog(std::string infoText)
	{
		const int timeInfoBufferSize = 1024;
		struct _timeb timebuffer;
		char timeline[26] = { 0 };
		char logtimeInfo[timeInfoBufferSize] = { 0 };
		errno_t err = 0;


		err = _ftime64_s(&timebuffer);
		if (err != 0)
		{
			sprintf_s(logtimeInfo, timeInfoBufferSize, "%s", "log failed!");
		}
		err = ctime_s(timeline, 26, &(timebuffer.time));
		if (err)
		{
			sprintf_s(logtimeInfo, timeInfoBufferSize, "%s", "log failed!");//printf("Invalid argument to ctime_s. ");
		}
		
		sprintf_s(logtimeInfo, timeInfoBufferSize, "time : %.19s.%hu %s", timeline, timebuffer.millitm, &timeline[20]);
		//std::cout<<logtimeInfo<<endl;

		std::string strWrite = logtimeInfo;
		strWrite[strWrite.length() - 1] = ' ';
		strWrite += "\t\tLogInfo:\t" + infoText + "\n";

		OutputDebugStringA(strWrite.c_str());
		
		EnterCriticalSection(&this->m_CriticalFileWrite);
		{
			if (m_pw)
			{
				
				fwrite(strWrite.c_str(), sizeof(char), strWrite.length(), m_pw);
				fflush(m_pw);
			}
			else
			{
				std::cout << "LOG Failed";
				/*MessageBoxA(nullptr, "LOG Failed", "Error", MB_ICONERROR);*/
			}
			
			
		}
		LeaveCriticalSection(&this->m_CriticalFileWrite);
	}
	void WriteDetail(std::string infoText, std::string file, int line, std::string function)
	{
		char buff[100] = { 0 };
		sprintf_s(buff, 100, "%d", line);
		string strLine = buff;
		infoText += "\t\t" + file + "\t\t" + strLine + "\t\t" + function + "\t\t";
		WriteLog(infoText);

	}
protected:
private:
	FILE *m_pw = nullptr;
	std::string m_logFileName; //Log文件名	
	CRITICAL_SECTION m_CriticalFileWrite; //保护代码的临界变量
};static InfoLog*   pDoLog = &InfoLog::GetSigleInfoLog() ;  //全局变量,执行Log过程

extern InfoLog*   pDoLog;
#define  LOG(x) pDoLog->WriteLog(x)
#define	 LOGDETAIL(x,file,line,function) pDoLog->WriteDetail(x,__FILE__,__LINE__,__FUNCTION__)

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值