這篇文章主要為大家展示了“C++中Log日志類輕量級(jí)支持格式化輸出變量的示例分析”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“C++中Log日志類輕量級(jí)支持格式化輸出變量的示例分析”這篇文章吧。
創(chuàng)新互聯(lián)公司長(zhǎng)期為1000多家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開(kāi)放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為二七企業(yè)提供專業(yè)的成都做網(wǎng)站、網(wǎng)站建設(shè),二七網(wǎng)站改版等技術(shù)服務(wù)。擁有十年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開(kāi)發(fā)。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; }
以上是“C++中Log日志類輕量級(jí)支持格式化輸出變量的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司行業(yè)資訊頻道!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站www.rwnh.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
本文標(biāo)題:C++中Log日志類輕量級(jí)支持格式化輸出變量的示例分析-創(chuàng)新互聯(lián)
網(wǎng)址分享:http://www.rwnh.cn/article14/cedcge.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、全網(wǎng)營(yíng)銷推廣、ChatGPT、軟件開(kāi)發(fā)、動(dòng)態(tài)網(wǎng)站、定制開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容