C++ Log日志類輕量級(jí)支持格式化輸出變量實(shí)現(xiàn)代碼
更新時(shí)間:2019年04月07日 21:28:13 投稿:mdxy-dxy
這篇文章主要介紹了C++ Log日志類輕量級(jí)支持格式化輸出變量實(shí)現(xiàn)代碼,需要的朋友可以參考下
CLog 頭 代碼很簡(jiǎn)單 如果需要的直接Ctrl+C ----Ctrl+V 即可
#ifndef __CLOG__
#define __CLOG__
#include <windows.h>
#include <string>
#include <fstream>
#include <tchar.h>
#include <ctime>
class CLog
{
public:
CLog();
CLog(const std::string LogFile);
~CLog();
template <class T>
static void WriteLog(T x);
//支持格式化輸出多參數(shù)輸出
static void WriteLogFormat(const char* format, ...);
private:
static std::string GetFilePath();
std::string m_LogFilePath;
static std::string GetSystemTimes();
static bool IsPathExist(const std::string FilePath);
};
//支持輸出int double 文本
template <class T> void CLog::WriteLog(T x)
{
std::fstream of(GetFilePath(), std::ios::app);
if (!of.is_open())return;
of.seekp(std::ios::end); //設(shè)置文件指針到文件尾部
of << GetSystemTimes() <<_T("line: ")<<__LINE__<<_T(" value: ")<< x << std::endl;
of.close(); //關(guān)閉文件;
}
#endif
CLog.cpp
#include "Log.h"
CLog::CLog()
:m_LogFilePath("")
{
m_LogFilePath = GetFilePath();
if (IsPathExist(m_LogFilePath))
DeleteFile(m_LogFilePath.c_str());
}
CLog::CLog(const std::string LogFile)
:m_LogFilePath(LogFile)
{
if (IsPathExist(m_LogFilePath))
DeleteFile(m_LogFilePath.c_str());
}
CLog::~CLog()
{
}
void CLog::WriteLogFormat(const char* format, ...)
{
va_list arglist;
std::string strArgData;
char szBuffer[0x1024];
ZeroMemory(szBuffer, 0x1024);
va_start(arglist, format);
vsprintf_s(szBuffer, format, arglist);
va_end(arglist);
strArgData = szBuffer;
std::fstream of(GetFilePath(), std::ios::app);
if (!of.is_open())return;
of << GetSystemTimes() << " Line: " << __LINE__ << " Value: " << strArgData << std::endl;
of.close();
}
std::string CLog::GetFilePath()
{
std::string FlieTmp;
TCHAR szPath[MAX_PATH];
::ZeroMemory(szPath, MAX_PATH);
if (!::GetCurrentDirectory(MAX_PATH, szPath))return FlieTmp;
FlieTmp = szPath;
FlieTmp += _T("\\log.txt");
return FlieTmp;
}
std::string CLog::GetSystemTimes()
{
time_t Time;
CHAR strTime[MAX_PATH];
ZeroMemory(strTime, MAX_PATH);
time(&Time);
tm t;
localtime_s(&t, &Time);
strftime(strTime, 100, _T("%Y-%m-%d %H:%M:%S "), &t);
std::string strTimes = strTime;
return strTimes;
}
bool CLog::IsPathExist(const std::string FilePath)
{
DWORD dwAttribute = ::GetFileAttributes(FilePath.c_str());
return dwAttribute != INVALID_FILE_ATTRIBUTES;
}
好了這篇文章就介紹到這了,需要的朋友可以的參考一下。
相關(guān)文章
C語(yǔ)言中for循環(huán)問題(一個(gè)小坑需注意)
這篇文章主要給大家介紹了關(guān)于C語(yǔ)言中for循環(huán)問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
C語(yǔ)言實(shí)現(xiàn)手寫JSON解析的方法詳解
JSON(JavaScript?Object?Notation)是一種輕量級(jí)的數(shù)據(jù)交換格式,用來傳輸屬性值或者序列性的值組成的數(shù)據(jù)對(duì)象。本文將利用C語(yǔ)言實(shí)現(xiàn)手寫JSON解析,感興趣的可以了解一下2022-09-09
opencv3/C++圖像濾波實(shí)現(xiàn)方式
今天小編就為大家分享一篇opencv3/C++圖像濾波實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-12-12
16種C語(yǔ)言編譯警告(Warning)類型的解決方法
由于編譯的警告各種各樣,根本不可以一一羅列出來,下面只是列舉出比較典型的十六種警告,還有一些警告,大家只要根據(jù)字面意思,就可以很快的查找出來,并解決之。希望對(duì)大家有所幫助。2014-08-08

