【C语言开源库】C语言开源库zlog的使用

2023-05-16

前面的cJSON库和环形缓冲库源文件数目都只有几个,所以我们使用的时候都是直接把这几个源文件放到我们的项目中使用,这次的zlog的源文件有四十多个,如果也和前面一样把源文件加入到项目中,会使得项目源文件管理起来复杂,因此我们需要将这些源文件编译成库文件。在我们的项目中使用库文件和相应的头文件即可。

但是因为这个库中的源文件会依赖很多linux上的库函数,因此,想要直接把文件加入到Clion项目中直接编译是不可能的,因此我们需要在linux上将该开源库编译成库文件,我们在CLion上直接使用库文件和头文件即可。

从这里我们也可以看出,在Windows上编译开源库,开发项目还是没有在linux上方便,linux上的编译环境配置起来还是很容易的,在Windows上就会经常遇到头文件找不到的错误,这也是很正常的,因为很多开源库的实现是要依赖操作系统的,因此有时候我们必须要在linux环境下进行构建。

zlog的GitHub地址:zlog

zlog的中文帮助文档:zlog使用手册

zlog博客:高效c/c++日志工具zlog使用介绍、zlog的安装与使用

如果没有linux编译环境,可以看这个GitHub:WinZlog,已经编译好了lib和dll文件。


我的这篇博文说明了怎么使用vscode链接linux服务器进行开发Vscode远程连接linux服务器(阿里云服务器)

下面我们就演示一下在linux服务器下编译zlog开源库,生成我们需要的库文件。

首先使用vscode远程连接上我的阿里云服务器。

https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210731213514973.png

下面我们文件的更改和命令行都可以在vscode中进行,比较方便。


先进入zlog目录,执行make

root@iZ2zeaqy08amcq9fk440kyZ:/code/zlog# make
cd src && make all
make[1]: Entering directory '/code/zlog/src'
cc -std=c99 -pedantic -c -O2 -fPIC -pthread  -Wall -Wstrict-prototypes -fwrapv -g -ggdb buf.c
cc -std=c99 -pedantic -c -O2 -fPIC -pthread  -Wall -Wstrict-prototypes -fwrapv -g -ggdb category.c
cc -std=c99 -pedantic -c -O2 -fPIC -pthread  -Wall -Wstrict-prototypes -fwrapv -g -ggdb category_table.c
cc -std=c99 -pedantic -c -O2 -fPIC -pthread  -Wall -Wstrict-prototypes -fwrapv -g -ggdb conf.c
cc -std=c99 -pedantic -c -O2 -fPIC -pthread  -Wall -Wstrict-prototypes -fwrapv -g -ggdb event.c
cc -std=c99 -pedantic -c -O2 -fPIC -pthread  -Wall -Wstrict-prototypes -fwrapv -g -ggdb format.c
cc -std=c99 -pedantic -c -O2 -fPIC -pthread  -Wall -Wstrict-prototypes -fwrapv -g -ggdb level.c
cc -std=c99 -pedantic -c -O2 -fPIC -pthread  -Wall -Wstrict-prototypes -fwrapv -g -ggdb level_list.c
cc -std=c99 -pedantic -c -O2 -fPIC -pthread  -Wall -Wstrict-prototypes -fwrapv -g -ggdb mdc.c
cc -std=c99 -pedantic -c -O2 -fPIC -pthread  -Wall -Wstrict-prototypes -fwrapv -g -ggdb record.c
cc -std=c99 -pedantic -c -O2 -fPIC -pthread  -Wall -Wstrict-prototypes -fwrapv -g -ggdb record_table.c
cc -std=c99 -pedantic -c -O2 -fPIC -pthread  -Wall -Wstrict-prototypes -fwrapv -g -ggdb rotater.c
cc -std=c99 -pedantic -c -O2 -fPIC -pthread  -Wall -Wstrict-prototypes -fwrapv -g -ggdb rule.c
cc -std=c99 -pedantic -c -O2 -fPIC -pthread  -Wall -Wstrict-prototypes -fwrapv -g -ggdb spec.c
cc -std=c99 -pedantic -c -O2 -fPIC -pthread  -Wall -Wstrict-prototypes -fwrapv -g -ggdb thread.c
cc -std=c99 -pedantic -c -O2 -fPIC -pthread  -Wall -Wstrict-prototypes -fwrapv -g -ggdb zc_arraylist.c
cc -std=c99 -pedantic -c -O2 -fPIC -pthread  -Wall -Wstrict-prototypes -fwrapv -g -ggdb zc_hashtable.c
cc -std=c99 -pedantic -c -O2 -fPIC -pthread  -Wall -Wstrict-prototypes -fwrapv -g -ggdb zc_profile.c
cc -std=c99 -pedantic -c -O2 -fPIC -pthread  -Wall -Wstrict-prototypes -fwrapv -g -ggdb zc_util.c
cc -std=c99 -pedantic -c -O2 -fPIC -pthread  -Wall -Wstrict-prototypes -fwrapv -g -ggdb zlog.c
cc -shared -Wl,-soname,libzlog.so.1.2 -o libzlog.so  buf.o category.o category_table.o conf.o event.o format.o level.o level_list.o mdc.o record.o record_table.o rotater.o rule.o spec.o thread.o zc_arraylist.o zc_hashtable.o zc_profile.o zc_util.o zlog.o  -pthread
# for use in test folder - linux and requirement for aix runtime
# resolving
cp -f libzlog.so libzlog.so.1
cp -f libzlog.so libzlog.so.1.2
cc -std=c99 -pedantic -c -O2 -fPIC -pthread  -Wall -Wstrict-prototypes -fwrapv -g -ggdb zlog-chk-conf.c
ar rcs libzlog.a buf.o category.o category_table.o conf.o event.o format.o level.o level_list.o mdc.o record.o record_table.o rotater.o rule.o spec.o thread.o zc_arraylist.o zc_hashtable.o zc_profile.o zc_util.o zlog.o
cc -o zlog-chk-conf zlog-chk-conf.o -L. -lzlog  -pthread
make[1]: Leaving directory '/code/zlog/src'
root@iZ2zeaqy08amcq9fk440kyZ:/code/zlog# 

看上面执行的命令,主要是生成了libzlog.so和libzlog.a。

继续执行make install

root@iZ2zeaqy08amcq9fk440kyZ:/code/zlog# make install
cd src && make install
make[1]: Entering directory '/code/zlog/src'
mkdir -p /usr/local/include /usr/local/lib /usr/local/bin
cp -a zlog.h /usr/local/include
cp -a zlog-chk-conf /usr/local/bin
cp -a libzlog.so /usr/local/lib/libzlog.so.1.2
cd /usr/local/lib && ln -sf libzlog.so.1.2 libzlog.so.1
cd /usr/local/lib && ln -sf libzlog.so.1 libzlog.so
cp -a libzlog.a /usr/local/lib
make[1]: Leaving directory '/code/zlog/src'
root@iZ2zeaqy08amcq9fk440kyZ:/code/zlog# 

可以看到上面的命令主要作用是下面几条

  • cp -a zlog.h /usr/local/include:把zlog.h头文件复制到gcc编译器默认搜寻目录
  • cp -a libzlog.so /usr/local/lib/libzlog.so.1.2:把动态库文件libzlog.so复制到gcc编译器默认搜寻目录,并重命名为libzlog.so.1.2
  • cp -a libzlog.a /usr/local/lib:把静态库文件libzlog.a复制到gcc编译器默认搜寻目录

我们继续可以执行一下make test来看看

root@iZ2zeaqy08amcq9fk440kyZ:/code/zlog# make test
cd test && make
make[1]: Entering directory '/code/zlog/test'
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_tmp.o -c test_tmp.c -I. -I../src
gcc -O2 -g -o test_tmp test_tmp.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_longlog.o -c test_longlog.c -I. -I../src
gcc -O2 -g -o test_longlog test_longlog.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_buf.o -c test_buf.c -I. -I../src
gcc -O2 -g -o test_buf test_buf.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_bitmap.o -c test_bitmap.c -I. -I../src
gcc -O2 -g -o test_bitmap test_bitmap.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_conf.o -c test_conf.c -I. -I../src
gcc -O2 -g -o test_conf test_conf.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_conf2.o -c test_conf2.c -I. -I../src
gcc -O2 -g -o test_conf2 test_conf2.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_hashtable.o -c test_hashtable.c -I. -I../src
In file included from ../src/zc_profile.c:9,
                 from test_hashtable.c:15:
../src/fmacros.h:4: warning: "_DEFAULT_SOURCE" redefined
    4 | #define _DEFAULT_SOURCE
      | 
In file included from /usr/include/x86_64-linux-gnu/bits/libc-header-start.h:33,
                 from /usr/include/stdio.h:27,
                 from test_hashtable.c:9:
/usr/include/features.h:227: note: this is the location of the previous definition
  227 | # define _DEFAULT_SOURCE 1
      | 
gcc -O2 -g -o test_hashtable test_hashtable.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_hello.o -c test_hello.c -I. -I../src
gcc -O2 -g -o test_hello test_hello.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_hex.o -c test_hex.c -I. -I../src
gcc -O2 -g -o test_hex test_hex.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_init.o -c test_init.c -I. -I../src
gcc -O2 -g -o test_init test_init.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_level.o -c test_level.c -I. -I../src
gcc -O2 -g -o test_level test_level.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_leak.o -c test_leak.c -I. -I../src
gcc -O2 -g -o test_leak test_leak.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_mdc.o -c test_mdc.c -I. -I../src
gcc -O2 -g -o test_mdc test_mdc.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_multithread.o -c test_multithread.c -I. -I../src
gcc -O2 -g -o test_multithread test_multithread.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_record.o -c test_record.c -I. -I../src
gcc -O2 -g -o test_record test_record.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_pipe.o -c test_pipe.c -I. -I../src
gcc -O2 -g -o test_pipe test_pipe.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_press_zlog.o -c test_press_zlog.c -I. -I../src
gcc -O2 -g -o test_press_zlog test_press_zlog.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_press_zlog2.o -c test_press_zlog2.c -I. -I../src
gcc -O2 -g -o test_press_zlog2 test_press_zlog2.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_press_write.o -c test_press_write.c -I. -I../src
test_press_write.c: In function ‘work’:
test_press_write.c:33:3: warning: ignoring return value of ‘write’, declared with attribute warn_unused_result [-Wunused-result]
   33 |   write(fd, aa, sizeof(aa)-1);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~
gcc -O2 -g -o test_press_write test_press_write.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_press_write2.o -c test_press_write2.c -I. -I../src
test_press_write2.c: In function ‘work’:
test_press_write2.c:36:3: warning: ignoring return value of ‘write’, declared with attribute warn_unused_result [-Wunused-result]
   36 |   write(fd, log, sizeof(log)-1);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
gcc -O2 -g -o test_press_write2 test_press_write2.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_press_syslog.o -c test_press_syslog.c -I. -I../src
gcc -O2 -g -o test_press_syslog test_press_syslog.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_syslog.o -c test_syslog.c -I. -I../src
gcc -O2 -g -o test_syslog test_syslog.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_default.o -c test_default.c -I. -I../src
gcc -O2 -g -o test_default test_default.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_profile.o -c test_profile.c -I. -I../src
gcc -O2 -g -o test_profile test_profile.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_category.o -c test_category.c -I. -I../src
gcc -O2 -g -o test_category test_category.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_prompt.o -c test_prompt.c -I. -I../src
gcc -O2 -g -o test_prompt test_prompt.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_enabled.o -c test_enabled.c -I. -I../src
gcc -O2 -g -o test_enabled test_enabled.o -L../src -lzlog -lpthread -Wl,-rpath ../src
make[1]: Leaving directory '/code/zlog/test'
root@iZ2zeaqy08amcq9fk440kyZ:/code/zlog# 

可以看到在test文件夹下生成了很多的测试程序,我们切到test目录

root@iZ2zeaqy08amcq9fk440kyZ:/code/zlog# cd test/
root@iZ2zeaqy08amcq9fk440kyZ:/code/zlog/test# ./test_hello 
2021-07-31 22:09:37 INFO [58918:test_hello.c:30] hello, zlog
2021-07-31 22:09:37.911 hello, zlog
2021-07-31 22:09:37.911232 hello, zlog

root@iZ2zeaqy08amcq9fk440kyZ:/code/zlog/test# ./test_init 
2021-07-31 22:10:25 INFO [58919:test_init.c:34] before update
TEST after update
2021-07-31 22:10:27 INFO [58919:test_init.c:52] init again

root@iZ2zeaqy08amcq9fk440kyZ:/code/zlog/test# ./test_level 
TRACE trace hello, zlog - trace
INFO info hello, zlog - info

root@iZ2zeaqy08amcq9fk440kyZ:/code/zlog/test# ./test_longlog 
useage: test_longlog [count]

可以看到我们可以去执行这些测试程序


通过上面的步骤,我们生成了静态库文件和动态库文件,并将库文件和头文件都复制到了gcc编译器默认的搜寻路径了。

前面我们在test下测试了一些zlog的功能,下面我们开始自己写一点简单的demo来测试一下zlog。

zlog的使用很简单,只需要头文件包含zlog.h就可以,此外还需要一个配置文件.conf

//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;
} 
//test_hello.conf
[formats]

simple = "%m%n"

[rules]

my_cat.DEBUG    >stdout; simple

执行编译命令

root@iZ2zeaqy08amcq9fk440kyZ:/code# cc -c -o test_hello.o test_hello.c -I/usr/local/include
root@iZ2zeaqy08amcq9fk440kyZ:/code# cc -o test_hello test_hello.o -L/usr/local/lib -lzlog
root@iZ2zeaqy08amcq9fk440kyZ:/code# ./test_hello 
2021-07-31 22:38:04.10 INFO   (my_cat:test_hello.c:24) - hello, zlog
sh: 1: /usr/bin/cronolog: not found

在当前目录下,我们会生成aa.log日志文件,里面的内容如下

2021-07-31 22:38:04.10 INFO   (my_cat:test_hello.c:24) - hello, zlog
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

【C语言开源库】C语言开源库zlog的使用 的相关文章

随机推荐