1 /*
2  * Copyright (C) 2015-2019 Alibaba Group Holding Limited
3  */
4 
5 #ifndef ULOG_SESSION_FILE_H__
6 #define ULOG_SESSION_FILE_H__
7 #include <stdio.h>
8 #include <stdbool.h>
9 #include <stdint.h>
10 
11 typedef unsigned short ulog_idx_type;
12 
13 #define ULOG_FILE_IDX_INVALID (ulog_idx_type)0xFFFF
14 
15 #define ULOG_FILE_IDX_START   (ulog_idx_type)1
16 
17 #define ULOG_CFG_PARA_VAL_SIZE 24
18 
19 #define SESSION_FS_INFO printf
20 #define SESSION_FS_DEBUG(...)
21 
22 #define ULOG_FILE_FORMAT     "%sulog%03d.log"
23 
24 #define ULOG_FILE_CFG_IDX    0
25 
26 #define ULOG_CFG_LINE_MAX_SIZE 128
27 
28 #define ULOG_FILE_PATH_SIZE 32
29 
30 #define LOG_LINE_SEPARATOR '\n'
31 #define LOG_LINE_END_STR   "\n"
32 
33 #define LOG_LIST_PREFIX "{\"time\":\"%s\",list:["
34 #define LOG_LIST_SUFFIX "]}"
35 
36 #define LOG_LIST_NODE "{\"idx\":%d,\"start\":\"%s\",\"end\":\"%s\"},"
37 
38 typedef enum {
39     ulog_cfg_para_start = 0,
40     ulog_cfg_para_end,
41     ulog_cfg_para_cnt,
42     ulog_cfg_para_none
43 } ulog_cfg_para_t;
44 
45 typedef enum {
46     ulog_cfg_type_working = 0,
47     ulog_cfg_type_list,
48     ulog_cfg_type_cnt
49 } ulog_cfg_type_t;
50 
51 #ifndef CSP_LINUXHOST
52 
53 #include "aos/vfs.h"
54 #else /* !CSP_LINUXHOST */
55 
56 #include <sys/types.h>
57 #include <sys/stat.h>
58 #include <fcntl.h>
59 #include <dirent.h>
60 #include <unistd.h>
61 
62 typedef DIR aos_dir_t;
63 
64 typedef struct dirent aos_dirent_t;
65 
66 #define aos_open open
67 
68 #define aos_close close
69 
70 #define aos_write write
71 
72 #define aos_read read
73 
74 #define aos_opendir opendir
75 
76 #define aos_readdir readdir
77 
78 #define aos_closedir closedir
79 
80 #define aos_lseek lseek
81 
82 #define aos_sync fsync
83 
84 #define aos_unlink unlink
85 
86 #undef ULOG_FILE_PATH_SIZE
87 #define ULOG_FILE_PATH_SIZE 256
88 
89 #endif /*CSP_LINUXHOST */
90 
91 #ifdef __cplusplus
92 extern "C" {
93 #endif
94 
95 void cfg_init_mutex(void);
96 
97 bool cfg_get_mutex(void);
98 
99 void cfg_release_mutex(void);
100 
101 /*
102  * get one line from specify fd.
103  *
104  * @param[in]  fd file description.
105  * @param[out]  buf buffer to save the output character.
106  * @param[in]  buf_len buffer room size.
107  */
108 int get_log_line(const int fd, char* buf, const uint16_t buf_len);
109 
110 /* O_APPEND */
111 int open_log_file(const ulog_idx_type file_idx, int flag, const off_t off);
112 
113 ulog_idx_type get_working_from_cfg_mm(void);
114 
115 int update_mm_cfg(const ulog_cfg_type_t type, const unsigned short idx, const ulog_cfg_para_t para_type, char val[ULOG_CFG_PARA_VAL_SIZE]);
116 int cfg_mm_2_file(const int fd);
117 int write_log_line(const int file_instanse, const char* buf, const bool keep_open);
118 int load_cfg_2_mm(void);
119 
120 #ifdef __cplusplus
121 }
122 #endif
123 
124 #endif /* ULOG_SESSION_FILE_H__ */
125 
126