交叉编译zlog日志工具:zlog+cronolog+crontab+logrotate

原创:

一、编译zlog日志工具

zlog的官网http://hardysimpson.github.io/zlog/

zlgo github源码下载网https://github.com/HardySimpson/zlog/releases

有网友提供了如下版本,方便其他平台上安装编译,非常感谢!

auto tools版本: https://github.com/bmanojlovic/zlog

我下载的auto tools版本

 

PC端(Ubuntu)

首先查看下是否已经具有交叉编译环境

echo $PATH

我这里是显示带有"/opt/poky/../arm-poky-linux-gnueabi"

然后可以去编译源码

cd 到源码目录

unzip zlog-master.zip
cd zlog-master/
mkdir install
chmod +x autogen.sh
./autogen.sh
./configure --host=arm-poky-linux-gnueabi --prefix=/.../zlog/zlog-master/install/
make install

此时已经编译成功至当前目录下的install目录里

进去zlog_install目录的lib里,查看是否已经是你的下位机适用库

file libzlog.so.1.1.0
显示:libzlog.so.1.1.0: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked, BuildID[sha1]=59cdf007aadd8a298b9f8a7f9df54b7f3919174a, not stripped

表示已经是下位机可执行的zlog库;该工具已经编译成功,将install里面的东西打包成.gz文件 tar czvf zlog_master.tar.gz install/,scp 给下位机直接解压到根目录下

 

接下来开始编写测试程序:

mkdir zlog_test

cd zlog_test

touch test_hello.c

vi test_hello.c

 1 #include  
 2 
 3 #include "zlog.h"
 4 
 5  
 6 
 7 int main(int argc, char** argv)
 8 
 9 {
10 
11     int rc;
12 
13     zlog_category_t *c;
14 
15     rc = zlog_init("test_hello.conf");
16 
17     if (rc) {
18 
19         printf("init failed\n");
20 
21         return -1; 
22 
23     }
24 
25     c = zlog_get_category("my_cat");
26 
27     if (!c) {
28 
29     printf("get cat fail\n");
30 
31         zlog_fini();
32 
33         return -2; 
34 
35     }
36 
37     zlog_info(c, "hello, zlog");
38 
39     zlog_fini();
40 
41     return 0; 
42 
43 }  

 

在同目录下新建test_hello.conf,编辑输入

[formats]
simple = "%m%n"

[rules]
my_cat.DEBUG >stdout; simple

在同目录下新建Makefile

all:clean test test_hello.o test_hello

test_hello.o:
    $(CC) -c -o test_hello.o test_hello.c -I/../zlog-master/install/include

test_hello:
    $(CC) -o test_hello test_hello.o -L/../zlog-master/install/lib -lzlog -lpthread
clean:
    rm -f test test_hello.o test_hello

 

执行make,生成 test_hello

将test_hello和test_hello.conf拷贝至下位机,直接运行./test_hello,打印成功

# ./test_hello
hello, zlog
 

二、交叉编译cronolog,缩短zlog打印时间

如果你按照zlog打印的日志处理,它功能很强大,能帮你自定义命名,快速转档,大小限制,都很智能,但是打印日志都会耗费cpu时间,你可以用它源码自带的test_press_zlog自己作测试;的确如zlog手册中所说,用cronolog速度更快;如下是我的zlog.conf配置文件:

# comments        
                              
[global]                        
                                              
strict init = true                              
                                                
buffer min = 1024                               
                                                
buffer max = 2MB                                
                                                
rotate lock file =/tmp/zlog.lock                
                                                
default format = "%d.%us %-6V (%c:%F:%L) - %m%n"
                                                                              
file perms = 600                                                              
                                                                              
[formats]                                                                     
                                                                              
simple = "%m%n"                                                                 
                                                                                
normal = "%d %m%n"                                                              
                                                                                
mdc_format="%d(%F %T).%us %-6V (%c:%F:%L) [%M(source)] [%M(direction)] - %m%n"
                                                                              
                                                                              
[rules]                                                                       
                                                                              
#smb_zlog.INFO    "/app/log/smb_zlog-%d(%Y%m%d).log", 2MB ~ "/app/log/smb_zlog-%
                                                                                
                                                                                
smb_zlog.INFO    | /usr/bin/cronolog /app/log/smb_cronolog%Y%m%d.log;mdc_format

交叉编译cronolog,下载源码,进行交叉编译,可能是源码不是很支持交叉编译,即使我配置了./configure --host=arm-poky-linux-gnueabi --prefix=$PWD/install 然后make && make install发现cronolog还是下位机无法使用,我是直接修改源码下的src/Makefile将CC编译环境进行手动修改,就成功了,这个源码如果编译不过就手动更改makefile;

编译成功后将cronolog拷贝至下位机 linux系统的 /usr/bin/位置下,在zlog.conf配置文件进行直接调用即可(如上图配置)

cronolog可以快速生成日志,但是无法切割转档日志,所以接下来需要用到logrotate日志切割工具

 

三、交叉编译logrotate,进行日志转档切割

logrotate交叉编译比较容易,下载源码,编译好执行bin文件logrotate至下位机/usb/bin位置,如果在/etc/目录下无logrotate.conf文件以及logrotate.d文件夹的话就手动建立,logrotate.conf如下图:

vi /etc/logrotate.conf

include /etc/logrotate.d/

在/etc/logrotate.d/目录下建立自己的应用程序的配置文件: touch mylogrotate,

/app/log/*.log{
rotate 5
#compress
nocompress
create 0666 root root
#delaycompress
nocopytruncate
missingok
notifempty
noolddir
sharedscripts
postrotate
if [ -f /var/run/myapp.pid ]; then
        kill -USR1 `cat /var/run/myapp.pid`
fi
endscript
}

配置中具体的含义自己百度logrotate用法,值得注意的是再转档时需要发个信号给你应用程序去重载 zlog_reload(const char *);因为切割后文件或许不在了,这就需要zlog重新加载配置文件;logrotate可以进行转档多少个文件,可以设定以文件大小是否满足转档,以及压缩文件;所以很强大,你可以手动输入命令进行logrotate进行使用: /usr/bin/logrotate -vf /etc/logrotate.conf

虽然可以切割日志并转档,但是logrotate一天也不可能永久运行去监视日志是否需要转档,所以需要配合crontab工具进行任务定时去调用logrotate;

四、交叉编译crontab,进行任务定时调用

crontab的源码百度真不好找,于是我用apt-get source cron进行下载源码,下载的是Vixie's cron 3.0的源码cron3.0-pl1,进去设置好你的编译环境,进行make,会生成两个bin文件:cron、crontab,可以看具体INSTALL说明怎么用,一开始我不知道用cron还是crontab,将cron拷贝至下位机/etc/中,crontab拷贝至下位机/usb/bin下,然后在你下位机中的/etc/rc.local最后一行进行加上 /etc/cron,记住不需要写成/etc/cron &,INSTALL说明中已解释;

/etc/cron

这样开机运行cron,cron会去看crontab有什么任务需要调用,crontab会在 /var/spool/crontab目录下查看用户的设定任务,我这里是root用户,在该目录下就会有个root文件,编译该文件或者使用命令crontab -e:

# Edit this file to introduce tasks to be run by cron.    
#  
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task                      
#                                                           
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system  
# daemon's notion of time and timezones.                        
#                                                             
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#                                                                   
# For example, you can run a backup of all your user accounts       
# at 5 a.m every week with:                                  
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/            
#                                                
# For more information see the manual pages of crontab(5) and cron(8)
#                                                                    
# m h  dom mon dow   command                                         
                            
*/1 * * * * date >> /tmp/cron_temp.txt  #每分钟打印当前时间至cron_temp.txt中
*/1 * * * * sh /app/etc/logrotate_zlog.sh #每分钟调用脚本去运行logrotate脚本

 logrotate_zlog.sh脚本是自己写来调用logrotate去切割日志的脚本:vi /app/etc/logrotate_zlog.sh

/usr/bin/logrotate -vf /etc/logrotate.conf

在配置中是每一分钟去调用该脚本,你可以设分钟/小时/天/星期,就像个闹钟一样,时间也很准,很强大,在这里设置每分钟我只是方便看效果而已;

至此总体就是这样,你用zlog打印日志有两个方案:

①zlog_api ->输出日志(包括转档日志,功能很智能强大);

②zlog_api->cronolog输出日志,crontab定时任务+logrotate对日志转档切割,虽然步骤多,组合技能,但速度比①方案快,大家自己取舍;

未交叉编译的源码我已上传至:

源码已上传至:https://download.csdn.net/download/u010505080/10883318

交叉编译每个人环境不一样,所以上面是源码

你可能感兴趣的:(交叉编译zlog日志工具:zlog+cronolog+crontab+logrotate)