zlog日志存储库的使用

个人在嵌入式linux开发中, 接坑第一步: 给这个坑加上zlog日志系统.

zlog

zlog使用手册

github

编译

make CC=xxxxx-gcc 
make PREFIX=/home/chry/codes/github/tar/new-root  install 


剥掉一些符号信息和调试信息
xxxxx-strip  /home/chry/codes/github/tar/new-root/lib/*

使用

创建conf文件

zlog.conf配置日志输出格式:

[formats]
my = "[%d] %f>: FUNCTION: %U, LINE: %L, %m"
[rules]
my_cat.DEBUG >stdout;my
*.*     "/tmp/device.log", 512KB * 2 ~ "/tmp/device.log.#r";my

解析

  • my = “[%d] %f>: FUNCTION: %U, LINE: %L, %m”

    指定输出日志的打印样式

    printf("ojbk\n");换成zlog("ojbk\n");之后,打印输出如下

    [2020-05-08 18:46:44] main.cpp>: FUNCTION: main, LINE: 305, ojbk
    
  • . “/tmp/device.log”, 512KB * 2 ~ “/tmp/device.log.#r”;my

    指定输出日志文件位置与名称/tmp/device.log,最多保存2个,每个文件512KB,旧的文件以/tmp/device.log.1/tmp/device.log.2方式命名,使用my的打印样式

zlog.conf的位置不能乱放,需要在代码中指定地址。

源码

xxx.h声明全局

#include 
#define ZLOG_CFG   "zlog.conf"		//指定conf文件地址
extern zlog_category_t * log_category;

#define COLOR_STR_NONE          "\033[0m"
#define COLOR_STR_BLACK         "\033[0;30m"
#define COLOR_STR_LIGHT_GRAY    "\033[0;37m"
#define COLOR_STR_DARK_GRAY     "\033[1;30m"
#define COLOR_STR_BLUE          "\033[0;34m"
#define COLOR_STR_LIGHT_BLUE    "\033[1;34m"
#define COLOR_STR_GREEN         "\033[0;32m"
#define COLOR_STR_LIGHT_GREEN   "\033[1;32m"
#define COLOR_STR_CYAN          "\033[0;36m"
#define COLOR_STR_LIGHT_CYAN    "\033[1;36m"
#define COLOR_STR_RED           "\033[0;31m"
#define COLOR_STR_LIGHT_RED     "\033[1;31m"
#define COLOR_STR_PURPLE        "\033[0;35m"
#define COLOR_STR_LIGHT_PURPLE  "\033[1;35m"
#define COLOR_STR_BROWN         "\033[0;33m"
#define COLOR_STR_YELLOW        "\033[1;33m"
#define COLOR_STR_WHITE         "\033[1;37m"
#define TIME_STR        "[%04d-%02d-%02d %02d:%02d:%02d]"


#define ryDbg(str, args...)     \
    if (log_category) \
        zlog(log_category, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, ZLOG_LEVEL_DEBUG, COLOR_STR_GREEN str COLOR_STR_NONE, ##args); 

#define ryErr(str, args...)     \
    if (log_category) \
        zlog(log_category, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, ZLOG_LEVEL_DEBUG, COLOR_STR_RED str COLOR_STR_NONE, ##args);
        

main.c初始化

#include "xxx.h"
zlog_category_t * log_category = nullptr;

int main()
{
    if (zlog_init(ZLOG_CFG))
    {
        printf("Error: zlog_init\n");
        zlog_fini();
    }
    else
    {
        //找到分类,在配置文件中的category
        log_category = zlog_get_category("my_cat");
        if (!log_category)
        {
            printf("Error: get cat fail\n");
            zlog_fini();
        }
    }
	// ======================================================
	// todo
	// ======================================================
    zlog_fini();
}

测试

chry@pc ~/c/g/U/b/UpgraderClient> cat /tmp/device.log 
[2020-05-08 18:46:44] main.cpp>: FUNCTION: main, LINE: 305, ojbk

你可能感兴趣的:(Linux)