freecplus framework - log file operation

Keywords: C++ C network xml Linux

1, Source code description

Freecplus is a C/C + + open source framework under Linux system. Please go to the C language technology network (www.freecplus.net) to download the source code.

This paper introduces the method of log file operation in freecplus framework.

The declaration file for functions and classes is freecplus / 65124; freecplus. H.

The definition file for functions and classes is freecplus / 65124; freecplus.cpp.

The sample program is located in the freecplus/demo directory.

The compile rule file is freecplus/demo/makefile.

2, Significance of log files

For the C/C + + service program, the program runs in the background, without an operation interface, unattended. The status of the program, the log of data processing, and the exception of the program must be recorded in the log file. The operation and maintenance personnel can check the program operation and data processing according to the contents of the log file.

3, CLogFile class

The CLogFile class is used by the service program to record the running log of the program.

1. Class declaration

// Log file operation class
class CLogFile
{
public:
  FILE   *m_tracefp;           // Log file pointer.
  char    m_filename[301];     // Log file name, absolute path is recommended.
  char    m_openmode[11];      // Generally, "a +" is used to open log files.
  bool    m_bEnBuffer;         // Whether to enable the buffering mechanism of the operating system when writing logs is not enabled by default.
  long    m_MaxLogSize;        // Maximum log file size, unit M, default 100M.
  bool    m_bBackup;           // Whether to switch automatically. If the log file size exceeds m ﹣ maxlogsize, it will switch automatically. It is enabled by default.

  // MaxLogSize: the maximum log file size, in M. the default is 100M, and the minimum is 10M.
  CLogFile(const long MaxLogSize=100); 

  // Open the log file.
  // Filename: the name of the log file. Absolute path is recommended. If the directory in the filename does not exist, create the directory first.
  // openmode: the opening mode of log file is the same as that of fopen library function. The default value is "a +".
  // bBackup: whether to switch automatically, true switch, false do not switch. In a multi process service program, if multiple users share a log file, bBackup must be false.
  // bEnBuffer: whether to enable the file buffer mechanism, true - enable, false - not enable. If the buffer is enabled, the content written into the log file will not be written to the file immediately, which is not enabled by default.
  bool Open(const char *filename,const char *openmode=0,bool bBackup=true,bool bEnBuffer=false);

  // If the log file is greater than 100M, the current log file will be backed up as a history log file. After the switch is successful, the contents of the current log file will be cleared.
  // After backup, the log file name will be appended with date and time.
  // Note that in multi process programs, log files cannot be switched, and in multi line programs, log files can be switched.
  bool BackupLogFile();

  // Write the content to the log file. fmt is a variable parameter, which is used in the same way as the printf library function.
  // The Write method writes the current time. The WriteEx method does not Write the time.
  bool Write(const char *fmt,...);
  bool WriteEx(const char *fmt,...);

  // Close log file
  void Close();

  ~CLogFile();  // The destructor calls the Close method.
};

2. Sample program

Example (demo42.cpp)

/*
 *  Program name: demo42.cpp. This program demonstrates the use of the CLogFile class of freecplus framework to record the running log of the program.
 *  This program modifies demo40.cpp to change the output printf statement to a log file.
 *  Author: C language technology network (www.freecplus.net) date: 20190525
*/
#include "../_freecplus.h"

int main()
{
  CLogFile logfile;

  // Open the log file. If "/ tmp/log" does not exist, create it, but make sure that the current user has the permission to create the directory.
  if (logfile.Open("/tmp/log/demo42.log")==false)
  { printf("logfile.Open(/tmp/log/demo42.log) failed.\n"); return -1; }

  logfile.Write("demo42 The program starts running.\n");

  CDir Dir;

  // Scan the files whose names match "surfdata" *. XML "in the / tmp/data directory.
  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("process the file%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; }

    // Here you can insert the code to read the contents of the data file, parse the xml string and write the data to the database.
    // Fgets and FFGETS are used to read text data, and Fread is used to read binary data.
    // I will not write the specific code.

    // After processing the data in the file, close the file pointer and delete the file.
    File.CloseAndRemove();

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

  logfile.Write("demo42 End of program operation.\n");
}

Run the demo39 program first, generate several data files in the / tmp/data directory, and then run demo42 to generate the log file / tmp/log/demo42.log, as follows:

3. Log file switching

We use a sample program to demonstrate the log file switching function, write 10 million data to the log file, and make it switch.

Example (demo43.cpp)

/*
 *  Program name: demo43.cpp, this program demonstrates the switching of log files of the CLogFile class of freecplus framework.
 *  Author: C language technology network (www.freecplus.net) date: 20190525
*/
#include "../_freecplus.h"

int main()
{
  CLogFile logfile;

  // Open the log file. If "/ tmp/log" does not exist, create it, but make sure that the current user has the permission to create the directory.
  if (logfile.Open("/tmp/log/demo43.log")==false)
  { printf("logfile.Open(/tmp/log/demo43.log) failed.\n"); return -1; }

  logfile.Write("demo43 The program starts running.\n");

  // Let the program loop 10000000 to generate a log large enough.
  for (int ii=0;ii<10000000;ii++)
  {
    logfile.Write("This program demonstrates the switching of log files. This is the%010%d Records.\n",ii);
  }

  logfile.Write("demo43 End of program operation.\n");
}

Run demo43, and a batch of log files will be generated in the / tmp/log directory. Use ls-l / tmp/log to see the following:

4, Copyright notice

C language technology net original article, reprint please explain the article source, the author and the original link.
Source: C language technology network (www.freecplus.net)
Author: Manon Youdao

If this article is helpful to you, please like support or forward my article in your blog, thank you!!!
If there is a mistake in the article, or there is a mistake in the content, or other suggestions and opinions, please leave a message for correction, thank you very much!!!

Posted by Kardall on Mon, 20 Apr 2020 10:56:26 -0700