freecplus framework - log file operations

A source code description

freecplus is a C / C ++ open source framework under a Linux system, go to the C language source code Technology Network (www.freecplus.net) download.

This article describes a method freecplus frame operation log file.

Declaration file functions and classes are freecplus / _freecplus.h.

Definition file functions and classes are freecplus / _freecplus.cpp.

Sample program located freecplus / demo directory.

Compiled rules file is freecplus / demo / makefile.

Significance of the log file

For the C / C ++ service program, the program running in the background, without user interface, unattended, running state, the processing of log data, and the like abnormal program must be recorded in the log file, the log file according to the operation and maintenance personnel of content, case status of the program and data processing.

Three, CLogFile class

CLogFile class service program record for running log of the program.

1, class declaration

// 日志文件操作类
class CLogFile
{
public:
  FILE   *m_tracefp;           // 日志文件指针。
  char    m_filename[301];     // 日志文件名,建议采用绝对路径。
  char    m_openmode[11];      // 日志文件的打开方式,一般采用"a+"。
  bool    m_bEnBuffer;         // 写入日志时,是否启用操作系统的缓冲机制,缺省不启用。
  long    m_MaxLogSize;        // 最大日志文件的大小,单位M,缺省100M。
  bool    m_bBackup;           // 是否自动切换,日志文件大小超过m_MaxLogSize将自动切换,缺省启用。

  // MaxLogSize:最大日志文件的大小,单位M,缺省100M,最小为10M。
  CLogFile(const long MaxLogSize=100); 

  // 打开日志文件。
  // filename:日志文件名,建议采用绝对路径,如果文件名中的目录不存在,就先创建目录。
  // openmode:日志文件的打开方式,与fopen库函数打开文件的方式相同,缺省值是"a+"。
  // bBackup:是否自动切换,true-切换,false-不切换,在多进程的服务程序中,如果多个进行共用一个日志文件,bBackup必须为false。
  // bEnBuffer:是否启用文件缓冲机制,true-启用,false-不启用,如果启用缓冲区,那么写进日志文件中的内容不会立即写入文件,缺省是不启用。
  bool Open(const char *filename,const char *openmode=0,bool bBackup=true,bool bEnBuffer=false);

  // 如果日志文件大于100M,就把当前的日志文件备份成历史日志文件,切换成功后清空当前日志文件的内容。
  // 备份后的文件会在日志文件名后加上日期时间。
  // 注意,在多进程的程序中,日志文件不可切换,多线的程序中,日志文件可以切换。
  bool BackupLogFile();

  // 把内容写入日志文件,fmt是可变参数,使用方法与printf库函数相同。
  // Write方法会写入当前的时间,WriteEx方法不写时间。
  bool Write(const char *fmt,...);
  bool WriteEx(const char *fmt,...);

  // 关闭日志文件
  void Close();

  ~CLogFile();  // 析构函数会调用Close方法。
};

2, the sample program

Example (demo42.cpp)

/*
 *  程序名:demo42.cpp,此程序演示采用freecplus框架的CLogFile类记录程序的运行日志。
 *  本程序修改demo40.cpp把输出的printf语句改为写日志文件。
 *  作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include "../_freecplus.h"

int main()
{
  CLogFile logfile;

  // 打开日志文件,如果"/tmp/log"不存在,就创建它,但是要确保当前用户具备创建目录的权限。
  if (logfile.Open("/tmp/log/demo42.log")==false)
  { printf("logfile.Open(/tmp/log/demo42.log) failed.\n"); return -1; }

  logfile.Write("demo42程序开始运行。\n");

  CDir Dir;

  // 扫描/tmp/data目录下文件名匹配"surfdata_*.xml"的文件。
  if (Dir.OpenDir("/tmp/data","surfdata_*.xml")==false)
  { logfile.Write("Dir.OpenDir(/tmp/data) failed.\n"); return -1; }

  CFile File;

  while (Dir.ReadDir()==true)
  {
    logfile.Write("处理文件%s...",Dir.m_FullFileName);

    if (File.Open(Dir.m_FullFileName,"r")==false)
    { logfile.WriteEx("failed.File.Open(%s) failed.\n",Dir.m_FullFileName); return -1; }

    // 这里可以插入读取数据文件的内容、解析xml字符串并把数据写入数据库的代码。
    // 读取文本数据用Fgets和FFGETS方法,读取二进制数据用Fread方法。
    // 具体的代码我就不写了。

    // 处理完文件中的数据后,关闭文件指针,并删除文件。
    File.CloseAndRemove();

    logfile.WriteEx("ok\n");
  }

  logfile.Write("demo42程序运行结束。\n");
}

Demo39 run the program in / tmp / data several data files generated in the directory, then run demo42, to generate log files /tmp/log/demo42.log, as follows:

Here Insert Picture Description

3 switch, the log file

We, ten million write data to a log file log file by switching feature presentation of a sample program, it generates the switching.

Example (demo43.cpp)

/*
 *  程序名:demo43.cpp,此程序演示freecplus框架的CLogFile类的日志文件的切换。
 *  作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include "../_freecplus.h"

int main()
{
  CLogFile logfile;

  // 打开日志文件,如果"/tmp/log"不存在,就创建它,但是要确保当前用户具备创建目录的权限。
  if (logfile.Open("/tmp/log/demo43.log")==false)
  { printf("logfile.Open(/tmp/log/demo43.log) failed.\n"); return -1; }

  logfile.Write("demo43程序开始运行。\n");

  // 让程序循环10000000,生成足够大的日志。
  for (int ii=0;ii<10000000;ii++)
  {
    logfile.Write("本程序演示日志文件的切换,这是第%010%d条记录。\n",ii);
  }

  logfile.Write("demo43程序运行结束。\n");
}

Run demo43, will generate a number of log files in / tmp / log directory, use ls -l / tmp / log view as follows:

Here Insert Picture Description

Fourth, the copyright notice

C Language Technology Network original article, reproduced please indicate the source link to the article, the author and original.
Source: C Language Technology Network (www.freecplus.net)
Author: Ethics code Agriculture

If the article typos, or content errors, or other suggestions and comments, please correct me message, thank you very much! ! !

Published 159 original articles · won praise 458 · views 120 000 +

Guess you like

Origin blog.csdn.net/wucz122140729/article/details/105187475