zsummer / fn-log

fn-log is a cpp-based logging utility. It's a gift.
MIT License
81 stars 30 forks source link

Introduction:

Build CodeQL Coverity Status Coverage Status
fn-log is an open source C++ lightweight & cross platform log library. It's an iteration from log4z.
It provides in a C++ application log and trace debug function for 7*24h service program.
Support 64/32 of windows/linux/mac/android/iOS.
fn-log是一款开源的轻量级高性能的跨平台日志库, 从log4z迭代而来, 主要针对7*24小时服务器程序的日志输出与跟踪调试,
支持64/32位的windows/linux/mac/android/iOS等操作系统.

Feature:

Example

Log Format

[20190514 16:47:20.536][ALARM] [15868]channel [0] start.

[20190514 16:47:20.548][DEBUG] [15868] fn-log\tests\simple_test.cpp:<40> main log init success
[20190514 16:47:20.548][DEBUG] [15868] fn-log\tests\simple_test.cpp:<42> main now time:1557823640;
[20190514 16:47:20.548][DEBUG] [15868] fn-log\tests\simple_test.cpp:<44> main hex text:
    [
    [0x7FF67B00B4B4:  f  n  l  o  g  . 
    [0x7FF67B00B4B4: 66 6E 6C 6F 67 00 
    ]

[20190514 16:47:20.548][ALARM] [15868] fn-log\tests\simple_test.cpp:<46> main finish

Fast Use Example With Out Yaml File

#include "fn_log.h"

int main(int argc, char* argv[])
{
    int ret = FNLog::FastStartDefaultLogger();
    if (ret != 0)
    {
        return ret;
    }

    LogAlarm() << "log init success";

    LogDebug() << "now time:" << time(nullptr) << ";";
    LOGD("now time:" << time(nullptr) << ";");
    LogDebugPack(0, 0, "now time:", time(nullptr), ";");

    LogAlarm() << "finish";

    return 0;
}

Fast Use Example With Yaml File

yaml file

# 配表文件  

 hot_update: true
# 0通道为多线程带文件和屏显输出
 - channel: 0  
    priority: trace
    category: 0
    category_extend: 0
    -device: 0
        disable: false
        out_type: file
        priority: trace
        category: 0
        category_extend: 0
        path: "./log/"
        file: "$PNAME_$YEAR$MON$DAY"
        rollback: 4
        limit_size: 1000 m #only support M byte
    -device: 1
        disable: false
        out_type: screen
        category: 0
        category_extend: 0
 # 1通道为多线程不挂任何输出端
 - channel: 1

 # 2通道为单线程同步写文件
 - channel: 2
    sync_write: 1 #only support single thread
    -device: 0
        disable: false
        out_type: file

 # 3通道为单线程无输出端
 - channel: 3
    sync_write: 1 #only support single thread

code

#include "fn_log.h"

int main(int argc, char* argv[])
{
    int ret = FNLog::LoadAndStartDefaultLogger("./log.yaml");
    if (ret != 0)
    {
        return ret;
    }

    int limit_second = 50;
    while (limit_second > 0)
    {
        LogDebug() << "default channel.";
        LogDebugStream(1, 0) << "channel:1, category:0.";
        LogDebugStream(2, 0) << "channel:2, category:0.";
        LogDebugStream(3, 0) << "channel:3, category:0.";
        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
        limit_second--;
    }

    LogAlarm() << "finish";
    return 0;
}

How To Use

multi header file

cp src/include/*.h to dst project.

single header file

cp fn_log.h.only to dst project and remove suffix ".only"

the file fn_log.h.only merge from src/include/*.h

Supported Compilers

How To Test

mkdir build
cd build
cmake ..
make
cd ../bin
./buffer_test
./buffer_correct_test
./load_config_test
./simple_test
./stress_udp_test
./stress_test
./multi-thread_test
./multi-thread_write_file_test

Yaml Manual

Global Option:

Channel Option: (channel.)

Device Option: (channel.device.)

基础语法

缩进 相同scope的field需要保持缩进一致

缩进 子层级field缩进大于父层级field

数组 以-为前缀 需要放在key之前

注释 任意行内读到#之后会停止该行的解析操作, 即使位于引号内.

kv分隔符 :

空白符 通常读到空白符会自动跳过, 但不包括尾随在值中间的空白符

key 必须为小写字母和下划线 不允许其他值

值 会自动裁切前缀空白

值 的行尾边界有["#\r\n\0]

值 的内容做了约束 范围为: 字符数字[a-zA-Z0-9] [_-:/.,$~%]

值 的内容需求上为 文件名和路径 数字 字符 因此通过约束可以减少不必要的配置问题

错误码 代码中搜索ParseErrorCode, ErrNo查找对应说明.

宏语义 当前实现方式 原地替换(inplace)随后的所有字符串内容

宏语义 要注意替换后的内容不能长于原有内容, 可以避免内存重分配开销.

宏语义 要注意不要替换掉正常的字符串, 包含值中的内容

变量 和宏替换类似, 不同点在于执行替换动作时候会增加前缀$进行替换, 并包裹{}重复执行一次. 例如var:{tag0=1} 会替换掉${tag0} 和 $tag0

宏,变量 均不能重复定义 (替换无法再替换)

宏,变量 通常为符号名+字面量 用来定义在随后重复使用的数字类编号 category, indentify, mask等

- shm_key: 33321231

- hot_update:[true][false]

- logger_name: string # log content prefix "LOG_PREFIX_NAME"

- logger_desc: string # log content prefix "LOG_PREFIX_DESC"

About The Author

Author: YaweiZhang
Mail: yawei.zhang@foxmail.com
QQGROUP: 524700770
GitHub: https://github.com/zsummer/fn-log