C++打印日志输出文件

做后台服务程序很多情况下都需要打印日志输出,我这里有简单的C++使用的打印日志输出文件可以直接复制粘贴使用,很方便,直接贴代码了。

   
   
  1. #ifndef NETDATALOG_H
  2. #define NETDATALOG_H
  3. #include <iostream>
  4. #include <fstream>
  5. #include <stdio.h>
  6. #include <direct.h>
  7. #include <string>
  8. #include <io.h>
  9. #include <stdlib.h>
  10. #include <time.h>
  11. #include <fcntl.h>
  12. using namespace std;
  13. enum TIMEFORMAT
  14. {
  15. NETLOG = 0, // [yyyy\mm\dd hh.MM.ss]
  16. LOGINLOG=1, // mm-dd hh:MM:ss
  17. };
  18. class NetDataLog
  19. {
  20. public:
  21. NetDataLog(string strDir = “log”,string filename = “record”,int maxfilesize=0,int filecount=0,int timeformat=0);
  22. ~NetDataLog();
  23. void addLog(string log); //添加日志记录到日志文件
  24. void fileSizeLimit(); //判断文件大小是否达到限定值
  25. int getCurrentLogFileSize();//获取当前日志文件的大小
  26. string getLogFileName(); //获取日志文件名称
  27. void setMaxFileSize(int);//设置文件最大大小
  28. void setFileName(string); //设置日志文件名
  29. void setFileCount(int); //设置日志文件的个数
  30. void setLogDir(string strDir); //设置日志文件目录
  31. private:
  32. void fileOffset(); //文件名称进行偏移
  33. bool checkFolderExist(const string &strPath);
  34. string getCurrentTime();
  35. private:
  36. string m_LogFileName; //文件名
  37. int m_MaxFileSize; //文件大小
  38. int m_FileCount; //文件个数
  39. fstream *m_outputFile; //输出文件流
  40. string m_strDir; //目录
  41. int m_timeFormat;
  42. };
  43. #endif

cpp文件内容如下:


   
   
  1. #include "stdafx.h"
  2. #include "NetDataLog.h"
  3. NetDataLog::NetDataLog(string strDir,string filename,int maxsize,int filecount,int timeFormat)
  4. {
  5. m_strDir = strDir;
  6. m_LogFileName = m_strDir+string("\\")+filename;
  7. m_MaxFileSize = maxsize;
  8. m_FileCount = filecount;
  9. m_timeFormat = timeFormat;
  10. //判断日志目录是否存在
  11. if(!checkFolderExist(m_strDir.c_str()))
  12. {
  13. _mkdir(m_strDir.c_str());
  14. }
  15. m_outputFile = new fstream;
  16. string strname = m_LogFileName+".txt";
  17. m_outputFile->open(strname,ofstream::out|ofstream::app); //打开日志文件
  18. bool b=m_outputFile->is_open();
  19. }
  20. NetDataLog::~NetDataLog()
  21. {
  22. if(m_outputFile)
  23. delete m_outputFile;
  24. }
  25. //********************************
  26. //函数名:NetDataLog::checkFolderExist
  27. //描 述:测试目录是否存在
  28. //参 数:strPath 目录名
  29. //返回值:存在返回真
  30. //*************************************
  31. bool NetDataLog::checkFolderExist( const string & strPath)
  32. {
  33. if(_access(strPath.data(),0) == 0)
  34. return true;
  35. else
  36. return false;
  37. }
  38. //********************************
  39. //函数名:NetDataLog::addLog
  40. //描 述:向文件中添加日志信息
  41. //参 数 log 为信息内容
  42. //返回值:void
  43. //*************************************
  44. void NetDataLog::addLog(string log)
  45. {
  46. string currentTime = getCurrentTime(); //获取本地时间
  47. if(m_timeFormat == NETLOG)
  48. *m_outputFile <<"["<<currentTime<<"] "<<log<<endl;
  49. else
  50. *m_outputFile<<currentTime<<" "<<log<<endl;
  51. //判断文件大小
  52. fileSizeLimit();
  53. }
  54. //********************************
  55. //函数名:NetDataLog::fileSizeLimit
  56. //描 述:判断文件大小是否达到最大值
  57. //参 数:无
  58. //返回值:void
  59. //*************************************
  60. void NetDataLog::fileSizeLimit()
  61. {
  62. int filesize = getCurrentLogFileSize();
  63. if(filesize>=m_MaxFileSize*1024)
  64. fileOffset();
  65. }
  66. //********************************
  67. //函数名:NetDataLog::fileOffset
  68. //描 述:实现文件名的偏移
  69. //参 数:无
  70. //返回值:void
  71. //*************************************
  72. void NetDataLog::fileOffset()
  73. {
  74. m_outputFile->close(); //关闭当前文件
  75. char filename[100]={0};
  76. char newfilename[100] = {0};
  77. for(int i = m_FileCount-1;i > 0;i--)
  78. {
  79. memset(filename,0,100);
  80. sprintf(filename,"%s%d.txt",m_LogFileName.data(),i);
  81. if(checkFolderExist(filename)) //存在
  82. {
  83. if(i == m_FileCount-1)
  84. {
  85. remove(filename);//删除文件
  86. continue;
  87. }
  88. //文件名序号向后偏移
  89. memset(newfilename,0,100);
  90. sprintf(newfilename,"%s%d.txt",m_LogFileName.data(),i+1);
  91. rename(filename,newfilename);
  92. }
  93. }
  94. memset(filename,0,100);
  95. sprintf(filename,"%s.txt",m_LogFileName.data());
  96. sprintf(newfilename,"%s%d.txt",m_LogFileName.data(),1);
  97. rename(filename,newfilename);
  98. m_outputFile->open(filename,ofstream::out|ofstream::app); //打开日志文件
  99. }
  100. //********************************
  101. //函数名:NetDataLog::getCurrentLogFileSize
  102. //描 述:计算当前日记文件的大小
  103. //参 数:无
  104. //返回值:文件大小(KB)
  105. //*************************************
  106. int NetDataLog::getCurrentLogFileSize()
  107. {
  108. long long filepos = m_outputFile->tellp(); //保存当前文件位置
  109. m_outputFile->seekp(0,ios_base::end); //移动到文件尾
  110. long long filesize = m_outputFile->tellp();
  111. m_outputFile->seekp(filepos,ios_base::beg); //恢复文件位置
  112. return filesize/1024;
  113. }
  114. //获取文件名
  115. string NetDataLog::getLogFileName()
  116. {
  117. return m_LogFileName+".txt";
  118. }
  119. //设置文件个数
  120. void NetDataLog::setFileCount(int count)
  121. {
  122. m_FileCount = count;
  123. }
  124. //设置文件名
  125. void NetDataLog::setFileName(string filename)
  126. {
  127. m_LogFileName = m_strDir+string("\\")+filename;
  128. }
  129. //设置文件大小
  130. void NetDataLog::setMaxFileSize(int maxsize)
  131. {
  132. m_MaxFileSize = maxsize;
  133. }
  134. //********************************
  135. //函数名:NetDataLog::getCurrentTime
  136. //描 述:获取本地时间
  137. //返回值:时间字符串
  138. //*************************************
  139. string NetDataLog::getCurrentTime()
  140. {
  141. time_t seconds = time(NULL); //获取时间
  142. struct tm *p;
  143. p = localtime(&seconds);//获取本地时间
  144. char strTime[100] = {0};
  145. if(m_timeFormat == NETLOG)
  146. sprintf(strTime,"%d\\%d\\%d %d.%d.%d",1900+p->tm_year,1+p->tm_mon,p->tm_mday,p->tm_hour,p->tm_min,p->tm_sec);
  147. else
  148. sprintf(strTime,"%02d-%02d %02d:%02d:%02d",1+p->tm_mon,p->tm_mday,p->tm_hour,p->tm_min,p->tm_sec);
  149. return string(strTime);
  150. }


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值