lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <unistd.h> |
| 4 | #include <string.h> |
| 5 | #include <errno.h> |
| 6 | #include <fcntl.h> |
| 7 | #include <stdarg.h> |
| 8 | |
| 9 | #include "pub_debug_info.h" |
| 10 | |
| 11 | |
| 12 | #define DEBUG_INFO_MAX_TOTAL_LEN (140) |
| 13 | #define DEBUG_INFO_MAX_DATA_LEN (128) |
| 14 | #define DEBUG_INFO_MEM_HEAD_LEN (8) |
| 15 | |
| 16 | typedef unsigned int UINT32; |
| 17 | typedef unsigned short UINT16; |
| 18 | typedef unsigned char UINT8; |
| 19 | |
| 20 | typedef struct |
| 21 | { |
| 22 | UINT16 module_id; // 模块id |
| 23 | UINT16 sub_len; // 用户数据长度 |
| 24 | UINT32 time; |
| 25 | char sub_data[]; // 用户数据 |
| 26 | } T_SHARED_MEM_DATA; |
| 27 | |
| 28 | int sc_debug_info_record(unsigned int id, const char *format, ...) |
| 29 | { |
| 30 | int fd = -1; |
| 31 | ssize_t writelen; |
| 32 | |
| 33 | int len; |
| 34 | va_list args; |
| 35 | char str_buf[DEBUG_INFO_MAX_TOTAL_LEN] __attribute__((aligned(4))); |
| 36 | T_SHARED_MEM_DATA *shareMemData; |
| 37 | shareMemData = (T_SHARED_MEM_DATA *)str_buf; |
| 38 | |
| 39 | /* args是一个char*类型指针,指向format之后的第一个参数*/ |
| 40 | va_start(args, format); |
| 41 | len = vsnprintf(shareMemData->sub_data, DEBUG_INFO_MAX_DATA_LEN, format, args); |
| 42 | va_end(args); |
| 43 | if (len < 0) |
| 44 | { |
| 45 | printf("[libdebug_info]: vsnprintf format error, %s.\n", strerror(errno)); |
| 46 | return -1; |
| 47 | } |
| 48 | |
| 49 | shareMemData->module_id = (UINT16)(id & 0xFFFF); |
| 50 | shareMemData->sub_len = len; |
| 51 | shareMemData->time = 0; |
| 52 | |
| 53 | fd = open(DEBUG_INFO_DEV_PATH, O_WRONLY); |
| 54 | if (fd < 0) |
| 55 | { |
| 56 | printf("[libdebug_info]: sc_debug_info_record, open debug_info error, %s\n", strerror(errno)); |
| 57 | return -1; |
| 58 | } |
| 59 | |
| 60 | writelen = write(fd, (char *)shareMemData, (len + DEBUG_INFO_MEM_HEAD_LEN)); |
| 61 | if (writelen < 0) |
| 62 | { |
| 63 | printf("[libdebug_info]: sc_debug_info_record, write debug_info error, %s\n", strerror(errno)); |
| 64 | return -1; |
| 65 | } |
| 66 | |
| 67 | if (fd >= 0) |
| 68 | { |
| 69 | close(fd); |
| 70 | } |
| 71 | |
| 72 | return writelen; |
| 73 | } |