国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++ Log日志類輕量級支持格式化輸出變量實現代碼

2020-01-26 13:29:32
字體:
來源:轉載
供稿:網友

CLog 頭 代碼很簡單 如果需要的直接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);  //支持格式化輸出多參數輸出  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);  //設置文件指針到文件尾部  of << GetSystemTimes() <<_T("line: ")<<__LINE__<<_T(" value: ")<< x << std::endl;  of.close();  //關閉文件;}#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("http://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;}

好了這篇文章就介紹到這了,需要的朋友可以的參考一下。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 双桥区| 拉萨市| 余江县| 紫金县| 政和县| 姜堰市| 嘉兴市| 湟中县| 定南县| 金门县| 临潭县| 达尔| 中卫市| 定南县| 汾西县| 营山县| 买车| 武义县| 色达县| 长春市| 射阳县| 屏山县| 和平区| 山东| 湾仔区| 兴化市| 当雄县| 上饶县| 萍乡市| 沙田区| 上蔡县| 微山县| 高阳县| 兖州市| 崇信县| 钟祥市| 富蕴县| 和硕县| 临海市| 灯塔市| 西安市|