C++中Log日志类轻量级支持格式化输出变量的示例分析-创新互联

这篇文章主要为大家展示了“C++中Log日志类轻量级支持格式化输出变量的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“C++中Log日志类轻量级支持格式化输出变量的示例分析”这篇文章吧。

创新互联公司长期为1000多家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为二七企业提供专业的成都做网站、网站建设,二七网站改版等技术服务。拥有十年丰富建站经验和众多成功案例,为您定制开发。

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("\\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日志类轻量级支持格式化输出变量的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联网站建设公司行业资讯频道!

另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

本文标题:C++中Log日志类轻量级支持格式化输出变量的示例分析-创新互联
网址分享:https://www.cdcxhl.com/article14/cedcge.html

成都网站建设公司_创新互联,为您提供自适应网站全网营销推广ChatGPT软件开发动态网站定制开发

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都定制网站网页设计