自学内容网 自学内容网

C语言日志类库 zlog 使用指南(第三章 “Hello World“)

第三章 “Hello World”

3.1 构建和安装 zlog

1. 下载 zlog:

链接: [https://github.com/HardySimpson/zlog/archive/latest-stable.tar.gz]

2. 解压并进入目录:

$ tar -zxvf zlog-latest-stable.tar.gz
$ cd zlog-latest-stable/

3. 编译并安装:

$ make
$ sudo make install

或者安装到指定目录:

$ sudo make PREFIX=/usr/local/ install

4. 配置系统设置:

$ sudo vi /etc/ld.so.conf
/usr/local/lib
$ sudo ldconfig

5. 其他可用的 make 命令:

  • make 32bit:在64位机器上编译32位版本,需要 libc6-dev-i386
  • make noopt:无 GCC 优化
  • make doc:需要 lyxhevea
  • make test:测试代码,也是 zlog 的好例子

3.2 在用户应用程序中调用和链接 zlog

1. 包含头文件:

#include "zlog.h"

2. 编译和链接:

$ cc -c -o app.o app.c -I/usr/local/include
$ cc -o app app.o -L/usr/local/lib -lzlog -lpthread

3.3 Hello World 示例

1. 创建源文件 test_hello.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_fini();

return 0;
}

2. 创建配置文件 test_hello.conf

[formats]
simple = "%m%n"

[rules]
my_cat.DEBUG >stdout; simple

3. 编译和运行:

$ cc -c -o test_hello.o test_hello.c -I/usr/local/include
$ cc -o test_hello test_hello.o -L/usr/local/lib -lzlog
$ ./test_hello

hello, zlog

3.4 更简单的 Hello World 示例

1. 创建源文件 test_default.c

#include <stdio.h>
#include "zlog.h"

int main(int argc, char** argv)
{
int rc;
rc = dzlog_init("test_default.conf", "my_cat");

if (rc) {
printf("init failed\n");

return -1;
}

dzlog_info("hello, zlog");
zlog_fini();

return 0;
}

2. 配置文件 test_default.conftest_hello.conf 相同

3. 区别:

这个例子使用 dzlog API,它包含一个默认的 zlog_category_t,使用更简单。

总结:这一章演示了如何下载、构建、安装 zlog,并在一个简单的 C 语言项目中使用它进行日志记录。通过两个示例代码和配置文件,展示了 zlog 的基本用法和配置方法。


原文地址:https://blog.csdn.net/fjw12998/article/details/142626966

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!