技术宅,fat-man

增加语言的了解程度可以避免写出愚蠢的代码

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

zlog小试(C语言日志工具)

test.c

复制代码
#include <stdio.h> 
#include "zlog.h"

int main(int argc, char** argv)

{
    int rc;
    zlog_category_t *c;

    rc = zlog_init("test_hello.conf");
    if (rc) {
        printf("init failed\n");
        return -1; 
    }

    c = zlog_get_category("my_cat");
    if (!c) {
        printf("get cat fail\n");
        zlog_fini();
        return -2; 
    }

    zlog_info(c, "hello, zlog");
    zlog_debug(c, "hello, zlog");
    zlog_fini();

    return 0; 
}
复制代码

test_hello.conf

[formats]
simple = "%d(%F %T %ms) %m%n"

[rules]
# my_cat.INFO    >stdout; simple 
my_cat.INFO    "test.log"; simple 

编译命令

gcc test.c -lzlog -I /usr/local/include/ -L /usr/local/lib/

说明

test_hello.conf 需要和可执行文件在同一目录,如果找不到或者配置文件里的配置不正确zlog_init都会报告初始化失败 %m是用户需要记录的message从info,debug函数传入,%n是换行符 %d(...) 表示配置的是日期时间选项,日志的时间是非常非常重要的,只有对程序各个环节在执行时消耗的时间有大致的了解,才能知道程序性能的瓶颈在哪里,以及找对应的解决办法%(%F %T %ms)分别是(年-月-日)日期,时间,毫秒, #号是注释 >stdout 表示日志输出到终端,"test.log"则表示日志输出到文件,直接写文件名日志就会被输出到可执行文件所在目录

posted on   codestyle  阅读(1135)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示