日志打印分析工具 mylog 使用wiki

来源:互联网 发布:淘宝商标需要注册吗 编辑:程序博客网 时间:2024/05/17 23:49

1 说明
1) 本文件夹包含源码src以及示例文件sample
2) 编译src生成output(包括iånclude和lib)
3) 使用output的lib和include,具体可以查看sample的Makefile和code
4) 日志级别为FATAL, WARNING, NOTICE, TRACE, DEBUG,以此级别变低
5) 使用MY_LOG_FATAL等打印日志,和printf使用方式类似,非常简单。

2 使用API(查看mylog.h)

1) 初始化日志目录

my_log_init(const char* log_path, const char* normal_path, const char* warn_fatal_path, const int log_level) 

log_path : log路径 normal_path : 正常日志目录 warn_fatal_path : 异常日志目录 log_level : 日志级别

2) 初始化线程日志数据

  my_log_thread_init() 

多线程使用

3) 设置一个线程的logid

my_log_set_logid(logid) 

必须在my_log_thread_init() 之后使用。

4) 设置一个线程的reqip 

my_log_set_reqip(reqip) 

必须在my_log_thread_init() 之后使用.

5) 设置一个线程的reqip

my_log_set_mod(mod) 

设置一个线程的reqip, 必须在单线程中使用或者my_log_thread_init() 之后使用.

6)设置计算执行时间的类型(打印时间是ms还是us)

 my_log_set_time_type(time_type) 

必须在单线程中使用或者my_log_thread_init() 之后使用.

7) 打印FATAL日志

MY_LOG_FATAL(logfmt, arg...) 

日记级别 >=1会打印 FATAL日志。

8)打印WARNNING日志

MY_LOG_WARNING(logfmt, arg...) 

日记级别 >=2会打印 WARNING日志。

9) 打印NOTICE日志

MY_LOG_NOTICE(logfmt, arg...) 

日记级别 >=4会打印 NOTICE日志。

10)打印TRACE日志

MY_LOG_TRACE(logfmt, arg...) 

日记级别 >=8会打印TRACE日志。

11) 打印DEBUG日志

MY_LOG_DEBUG(logfmt, arg...) 

日记级别 >=16会打印DEBUG日志。

3 范例

1) code

 

01#include "mylog.h"
02 
03 
04void* test_thread1(void*)
05{
06    my_log_thread_init();
07    my_log_set_reqip("10.10.10.31");
08    my_log_set_time_type(TIME_TYPE_MSEC);
09    for(int i=0; i<100; i++)
10    {
11        my_log_set_logid(i);
12        MY_LOG_FATAL("thread 1 fatal is at %d, it's %s", i , "OK");
13        MY_LOG_WARNNING("thread 1 warning is at %d, it's %s", i , "OK");
14        MY_LOG_NOTICE("thread 1 notice is at %d, it's %s", i , "OK");
15        MY_LOG_TRACE("thread 1 trace is at %d, it's %s", i , "OK");
16        MY_LOG_DEBUG("thread 1 debug is at %d, it's %s", i , "OK");
17        sleep(1);
18     
19    }
20}
21 
22 
23void* test_thread2(void*)
24{
25    my_log_thread_init();
26    for(int i=0; i<3; i++)
27    {
28        my_log_set_mod("test2");
29        MY_LOG_FATAL("thread 2 fatal is at %d, it's %s", i , "OK");
30        MY_LOG_WARNNING("thread 2 warning is at %d, it's %s", i , "OK");
31        MY_LOG_NOTICE("thread 2 notice is at %d, it's %s", i , "OK");
32        MY_LOG_TRACE("thread 2 trace is at %d, it's %s", i , "OK");
33        MY_LOG_DEBUG("thread 2 debug is at %d, it's %s", i , "OK");
34        sleep(1);
35    }
36}
37 
38 
39int main()
40{
41    my_log_init(".""test.log""test.log.wf", 16);
42    MY_LOG_DEBUG("main begin");
43 
44 
45    pthread_t t1, t2;
46    pthread_create(&t1, NULL, test_thread1, NULL);
47    pthread_create(&t2, NULL, test_thread2, NULL);
48 
49 
50    pthread_join(t1, NULL);
51    pthread_join(t2, NULL);
52    MY_LOG_DEBUG("main end");
53}

 

 

2)运行结果

0 0
原创粉丝点击