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 的基本用法和配置方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值