1. Zlog profile

There are a lot of zlog resources on the web, not detailed here; Zlog is a logging tool written in C. It is very small and efficient, and can output to the console and file simultaneously. The logging interface is basically the same as printf, so it is easy to use.

The only downside to feeling is that the configuration is a bit more complex and you can’t change the output level dynamically;

 

2. Introduction to usage

2.1 zlog compilation

Zlog compiles well. If you’re running on x86, you don’t need to modify the makefile, just go to SRC and make it. If you are cross-compiling and porting to an embedded device, you only need to change the SRC /makefile cc to the cross-compiler GCC and the ar to the cross-compiler ar:

Libzlog. so and libzlog.a are generated in the SRC directory

2.2 Zlog Configuration File

When using zlog, you need to configure the output rules first and write them in a file. Here is a brief introduction to configuration files.

The default value is true. If the value is set to true, Zlog will strictly check the format and rules used. Otherwise, Zlog will ignore the format and rules used. Strict init = true Buffer min = 1024 buffer Max = 2048 # File transfer Specifies a lock file, which is used to ensure safe file transfer in multi-process scenarios. The default configuration file is the lock file. Rotate lock file = zlog.lock #rotate lock file = zlog.lock #rotate lock file = 600 perms = 600 [formats] # Use default log output format "%d %V [%p %F %L] %m%n" The output log format is as follows: %-5V Left-aligned by log level #2012-12-13 10:23:29 INFO [31668:test_hello.c:41] hello, Zlog simple = "% d % % 8.8 us - 5 v [% 8.8 p. % 8.8 F t % % L] % m % n" # simple = "% d. % % m % n" ms # simple2 = "% % d. The us % m % n" (rules) Debug info notice WARN FATAL debug is output to the DEBUG level. my_cat.* >stderr; My_cat. INFO "hello.txt", 10KB * 3 ~ "hello.txt.#r"; simple #my_cat.INFO "hello.txt",1MB ~ "hello-%d(%Y%m%d).#2s.txt"; simple #my_cat.INFO "hello.txt",1MB; simple #my_cat.INFO "hello.txt",1MB; simpleCopy the code

2.3 zlog use

When using zlog, you need to initialize it first, and then get a handle (pointer). Since this handle is global, this is normal in C, but we have used C ++ for a long time, we don’t like global variables, so here I use C ++ single instance idea, to wrap the zlog initialization part, so that it is easy to use. Just include these two files and change the log path and cat to suit your own needs:

Source file log_manager.cpp:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>


#include "log_manager.h"

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /


/ * constants * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
const char* ConfigText =
"[global]\n"
"strict init = true\n"
"buffer min = 1024\n"
"buffer max = 2048\n"
"# log access,600 only allow current user access \n"
"file perms = 600\n"
"[formats]\n"
"Simple = \" % d % % 8.8 us - 5 v [% 8.8 p. % 8.8 F t % % L] % m % n \ \ n ""
"simple1= \"%m%n\"\n"
"[rules]\n"
"db_cat.warn		>stderr;simple1\n"
"db_cat.ERROR \"./logs/test.txt\",1M * 3 ~ \"./logs/test.txt.#r\"; simple\n";


// Priority from low to high Debug info notice WARN error fatal DEBUG The debug priority is greater than or equal to.


/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
static zlog_category_t * logHandler_m = NULL;
static int DetectConfigFile(const char *File);

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * the function name: LogMgr, LogMgr function description: Log module initialization Input parameter: none Output parameter: none Rollback value: change history: 1. Date: Wednesday, December 19, 2012 Author: FensJoy The new generation function * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
zlog_category_t * Init_zlog(a)
{
	DetectConfigFile(LOG_CONFIG);

	zlog_init(LOG_CONFIG);
    logHandler_m = zlog_get_category(LOG_CAT);
	if(! logHandler_m) {printf("%s in Init_zlog, get cat fail! line = %d\n",PRO_NAME, __LINE__);
		zlog_fini(a);remove(LOG_CONFIG);
		exit(- 1);
	}
	return logHandler_m;
};

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * the function name: DetectConfigFile function description: Input parameter: const char *File Output parameter: no rollback value: static Change history: 1 Date: Thursday, December 20, 2012 Author: FensJoy The new generation function * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
static int DetectConfigFile(const char *File)
{
	FILE *fp  = NULL;
	int iRet = 0;
	DIR *log_p = NULL;
	char *cmd_buf = NULL;

	/* Check whether the directory for storing log files exists. If not, create */
	log_p = opendir(LOG_PATH);
	if ( log_p == NULL )
	{
		printf("%s in DetectConfigFile, the log dir is not exist, now creat: %s! \n", PRO_NAME,LOG_PATH);

		cmd_buf = new char[64];

		printf("%s in DetectConfigFile, the log dir is not exist, now creat! \n", PRO_NAME);
		snprintf(cmd_buf, 64."mkdir -p %s &", LOG_PATH);
		iRet = system(cmd_buf);

		delete [] cmd_buf;

	}
	else
	{
		closedir(log_p);
	}

	if ( access(File, F_OK) ! =0 )
	{
		printf("%s log config file not exist,now creat! \n", PRO_NAME);
		fp = fopen(File, "w+");
		if(fp == NULL)
		{
			perror("#[db]: in DetectConfigFile, fopen config file error!");
			return - 1;
		}
		iRet = fwrite(ConfigText, 1.strlen(ConfigText), fp);
		if ( iRet < 0 )
		{
			perror("#[para]: in DetectConfigFile, fwrite error!");
			fclose(fp);
			return - 1;
		}

		fclose(fp);
	}


	return 0;
}

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * the function name: getZlogHandler function description: Obtaining a Zlog handle Input: none Output: none Return value: zlog_category_t Change history: 1. Date: Friday, January 24, 2014 Author: FensJoy The new generation function * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
zlog_category_t *  getZlogHandler(a)
{
    if ( NULL == logHandler_m )
	{
		logHandler_m = Init_zlog(a); }return logHandler_m;
}
Copy the code

The header file log_manager. H

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * all copyright (C), 2001-2014, * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * name: log_manger. H version number: the first draft as a person: Fensjoy Built date: Wed, 05 Jun 2013 Last modified: Function description: log_manger. CPP header function list: Change history: 1. Date: Wednesday, June 5, 2013 Author: FensJoy Create a file * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
#ifndef __LOG_MANGER_H__
#define ___LOG_MANGER_H__
#include <iostream>
#include "zlog.h"


#define PRO_NAME "#[db]: "
#define LOG_CONFIG "./test_log.conf"
#define LOG_PATH   ". /"

#define LOG_CAT    "test_cat"  // Make sure that the output is consistent with that in the configuration file

//extern zlog_category_t * logHandler_m;


zlog_category_t * Init_zlog(a);
zlog_category_t *  getZlogHandler(a);




#endif /* __LOG_MANGER_H__ */
Copy the code

Test main function:

#include <iostream>
#include "log_manager.h"

using namespace std;


int main(int argc, char **argv)
{

	zlog_category_t *pLog =  Init_zlog(a);zlog_notice(pLog, "hello zlog!");
	
	zlog_error(pLog, "this msg both to console and log file!");
	
	zlog_debug(pLog, "this msg is debug level, just to console!");

	return 0;
}
Copy the code

Is it very simple ~;

Make run directly after compiling:

Run:Log file contents:

It is really very simple to use, I feel that Zlog is more suitable for the performance analysis of the program, that is, when the program runs for a long time, record the running state of the program, and then analyze the program performance for later optimization; In addition, zlog has a place to pay attention to, that is, when the log file reaches the set size, switch the log file, if the program output log too much interval is relatively short (100 milliseconds) or so, the program will hang, then a zlog bug. The general program output log is not so much, will not appear

Source code has been uploaded to CSDN:

Download.csdn.net/download/wu…

 

Since the lowest option of download credit is 1 when uploading, it cannot be set to 0. If there is no credit, you can send me a message on wechat official account.

I’ll email you