QNX slog2日志系统上手指南
概要
slog2 是QNX系统提供的一套log机制;可通过slog2info命令获取历史log;
使用slog2
使用流程
1 配置buffer集名称和大小,以及log等级
2 创建slog2 buffer集 实例
3 打印log
1 配置
buffer set 配置
1
2
3
4
5
6
7
8typedef struct
{
int num_buffers;
const char *buffer_set_name;
uint8_t verbosity_level;
slog2_buffer_config_t buffer_config[ SLOG2_MAX_BUFFERS ];
uint32_t max_retries;
} slog2_buffer_set_config_t;- num_buffers : log buffer集 中有多少个buffer
- buffer_set_name : log buffer集的名称一般是进程名,slog2info -b 参数使用
- verbosity_level : 整个log buffer集中的最小verbosity_level,只有高于verbosity_level的log才会写入log buffer中;
- buffer_config :log buffer
buffer 配置
1
2
3
4
5typedef struct
{
const char *buffer_name;
int num_pages;
} slog2_buffer_config_t;- buffer name : slog2 buffer 名称
- num_pages : 用于定于log buffer的大小,一个page是4kB
2 创建slog2 buffer集实例
1
2
3
4
5
#include <sys/slog2.h>
int slog2_register( slog2_buffer_set_config_t *config,
slog2_buffer_t *handles,
uint32_t flags );
* config : buffer集的配置结构体
* handles : 返回操作buffer的handle,后面打印log函数会用到
* flags :用于指定是否存放到flash中,方便热启时恢复;[参考链接](http://www.qnx.com/developers/docs/7.1/#com.qnx.doc.neutrino.lib_ref/topic/s/slog2_register.html)
* 返回值:0 成功,-1 失败
3 打印log
- 打印一段固定字符串
int slog2c( slog2_buffer_t buffer, uint16_t code, uint8_t severity, const char* data );
slog2c( handle, 0, SLOG2_INFO, “Writing a constant string into the buffer” );
code参数在slog2info -e 中用到,slog2系统不做任何处理;
- 打印一段格式化字符串
`int slog2f( slog2_buffer_t buffer,uint16_t code, uint8_t severity, const char* format, ... );`
slog2f( handle, 0, SLOG2_INFO, “Writing a formatted string into the buffer: %s”, argv[0] );
- 打印一段格式化字符串,不会触发中断,可用于中断函数中,格式化是在读取log的时候发生
`int slog2fa( slog2_buffer_t buffer,uint16_t code, uint8_t severity, const char* format, ... );`
SLOG2_FA_ 开头的宏必须的;
slog2fa( buffer_handle[0], 0, SLOG2_WARNING, “string:%s, some_number:%d”,SLOG2_FA_STRING( “Hello world” ),SLOG2_FA_SIGNED( some_number ),SLOG2_FA_END);
编译选项
- 头文件 #include <sys/slog2.h>
- 需要链接库 libslog2
查看slog2
使用slog2info 命令来查看具体的log内容slog2info [-acfinSstvWw] [-b bset1,bset2,...] [-d path] [-e code1,code2,...] [-l filename] [-r [resetfilename]]
- 查看某一个bufferset的log
slog2info -b buffer_set_name
- 查看从最后一次reset开始的log
slog2info -r
- 一直打印log到终端
slog2info -w
结语
参考
verbosity_level:
- SLOG2_SHUTDOWN — shut down the system now (e.g., for OEM use).
- SLOG2_CRITICAL — unexpected unrecoverable error (e.g., hard disk error).
- SLOG2_ERROR — unexpected recoverable error (e.g., you need to reset a hardware controller).
- SLOG2_WARNING — expected error (e.g., parity error on a serial port).
- SLOG2_NOTICE — warning (e.g., out of paper).
- SLOG2_INFO — information (e.g., printing page 3).
- SLOG2_DEBUG1 — debug messages (e.g., normal detail).
- SLOG2_DEBUG2 — debug messages (e.g., fine detail).