blob: 7925381b075890a690bc315b5ee3694d1707ac6a [file] [log] [blame]
liubin281ac462023-07-19 14:22:54 +08001#include <stdio.h>
2#include <fcntl.h>
3#include <time.h>
4#include <stdio.h>
5#include <unistd.h>
6#include <sys/socket.h>
7#include <time.h>
8#include <errno.h>
9#include <stdlib.h>
xf.li44e08692024-01-30 01:54:44 -080010#include <sys/file.h>
liubin281ac462023-07-19 14:22:54 +080011#include <sys/types.h>
12#include <sys/stat.h>
b.liub3b923a2024-06-06 15:15:49 +080013#include "json-c/json.h"
14#include "json-c/printbuf.h"
liubin281ac462023-07-19 14:22:54 +080015#include "log_config.h"
16
b.liu5fa2fb52024-06-20 18:26:34 +080017#include "mbtk_type.h"
xf.li43643772024-03-04 19:39:53 -080018//#define DEBUG 1
liubin281ac462023-07-19 14:22:54 +080019
20#ifdef DEBUG
21#define mbtk_log(...) printf(__VA_ARGS__)
22#else
23#define mbtk_log(...)
24#endif
25
26static void handler_free(log_config_entry* listdata)
27{
28 int i, n;
29 struct filter_list_t* _filter_list = NULL;
30 log_config_entry* entry;
31
32 for (i = 0; i < 5; i++) {
33 entry = listdata + i;
34 if (entry->name) {
35 free(entry->name);
36 if (entry->out_path) {
37 free(entry->out_path);
38 }
39 if (entry->ip) {
40 free(entry->ip);
41 }
42 _filter_list = entry->filter_list;
43 for (n = 0; n < 10; n++) {
44 printf("%s %d: malloc %x!\n", __FUNCTION__, __LINE__, _filter_list);
45 if (_filter_list) {
46 printf("%s %d: malloc %x!\n", __FUNCTION__, __LINE__, _filter_list->next);
47 if (_filter_list->tag) {
48 free(_filter_list->tag);
49 }
50 free(_filter_list);
51 _filter_list = _filter_list->next;
52 } else {
53 break;
54 }
55 }
56 } else {
57 break;
58 }
59 }
60}
61
62int parse_config(log_config_entry* listdata)
63{
64 json_object* jsonobj = NULL;
65 json_object* tmpjson = NULL;
66 json_object* datajson = NULL;
67 json_object* listjson = NULL;
68 json_object* fileterjson = NULL;
69 json_object* fileter_listjson = NULL;
70 log_config_entry* entry;
xf.li43643772024-03-04 19:39:53 -080071 int i, n, ret, array_length;
liubin281ac462023-07-19 14:22:54 +080072 char* cmdval = NULL;
73
xf.li44e08692024-01-30 01:54:44 -080074 printf("MBTK_LOGD: in parse_config\n");
liubin281ac462023-07-19 14:22:54 +080075 jsonobj = json_object_from_file(LOG_CONFIG_PATH);
76 if (NULL == jsonobj) {
77 printf("Can't open config file: %s\n", LOG_CONFIG_PATH);
78 return -1;
79 }
80 ret = json_object_object_get_ex(jsonobj, "logd", &tmpjson);
81 if (!ret) {
82 printf("get jsondata error ...\n");
83 }
84
85 if (NULL == tmpjson) {
86 printf("the tmpjson : [%s]\n", json_object_to_json_string(tmpjson));
87 }
88 /**获取total***/
89 cmdval = json_object_get_string(tmpjson);
90 printf("logd enable : %s\n", cmdval);
91 json_object_put(tmpjson);
xf.li44e08692024-01-30 01:54:44 -080092
liubin281ac462023-07-19 14:22:54 +080093 /***获取data***/
94 json_object_object_get_ex(jsonobj, "buffer_list", &tmpjson);
xf.li44e08692024-01-30 01:54:44 -080095
liubin281ac462023-07-19 14:22:54 +080096 for (i = 0 ; i < 5; i++) {
97 struct filter_list_t* _filter_list = NULL;
98 struct filter_list_t* tmp_filter_list = NULL;
99 datajson = json_object_array_get_idx(tmpjson, i);
100 if (NULL == datajson) {
101 mbtk_log("the datajson exit\n");
102 break;
103 }
104 entry = listdata + i;
105 json_object_object_get_ex(datajson, "enable", &listjson);
106 entry->enable = json_object_get_int(listjson);
107 mbtk_log("enable: %d\n", entry->enable);
108
109 json_object_object_get_ex(datajson, "name", &listjson);
xf.li44e08692024-01-30 01:54:44 -0800110 entry->name = json_object_get_string(listjson);
111 printf("cmdval: %x, %s\n", entry->name, entry->name);
liubin281ac462023-07-19 14:22:54 +0800112
113 json_object_object_get_ex(datajson, "log_file", &listjson);
xf.li44e08692024-01-30 01:54:44 -0800114 entry->out_path = json_object_get_string(listjson);
liubin281ac462023-07-19 14:22:54 +0800115 mbtk_log("cmdval: %s\n", entry->out_path);
116
117 json_object_object_get_ex(datajson, "log_stream", &listjson);
118 if (listjson) {
xf.li44e08692024-01-30 01:54:44 -0800119 entry->ip = json_object_get_string(listjson);
120
liubin281ac462023-07-19 14:22:54 +0800121 cmdval = strstr(entry->ip, ":");
122 if (cmdval) {
123 entry->port = &cmdval[1];
124 cmdval[0] = 0;
125 mbtk_log("cmdval: %s [%s]\n", entry->ip, entry->port);
126 } else {
127 printf("Can't find port!!\n");
128 free(entry->ip);
129 entry->ip = NULL;
130 }
131 }
132 json_object_object_get_ex(datajson, "rotate_file_size", &listjson);
133 entry->rotate_file_size = json_object_get_int(listjson) * 1024;
134 mbtk_log("rotate_file_size: %d\n", entry->rotate_file_size);
135
136 json_object_object_get_ex(datajson, "rotate_file_count", &listjson);
137 entry->rotate_file_count = json_object_get_int(listjson);
138 mbtk_log("rotate_file_count: %d\n", entry->rotate_file_count);
139
140 json_object_object_get_ex(datajson, "filter_list", &listjson);
141 if (NULL == listjson) {
142 printf("%s %d: object failure!\n", __FUNCTION__, __LINE__);
143 json_object_put(listjson);
144 continue;
145 }
146 entry->filter_list = (struct filter_list_t*)malloc(sizeof(struct filter_list_t));
147 _filter_list = entry->filter_list;
148
xf.li43643772024-03-04 19:39:53 -0800149 array_length = json_object_array_length(listjson);
150 for (n = 0 ; n <= array_length; n++) {
liubin281ac462023-07-19 14:22:54 +0800151 fileterjson = json_object_array_get_idx(listjson, n);
152 if (NULL == fileterjson) {
153 mbtk_log("the fileterjson exit\n");
154 free(tmp_filter_list->next);
155 tmp_filter_list->next = NULL;
156 break;
157 }
158 memset(_filter_list, 0, sizeof(struct filter_list_t));
159 json_object_object_get_ex(fileterjson, "priority", &fileter_listjson);
160 char* str = json_object_get_string(fileter_listjson);
161 if (str) {
162 _filter_list->priority = str[0];
163 mbtk_log("fileter_listjson: %c\n", _filter_list->priority);
164 }
165 json_object_object_get_ex(fileterjson, "tag", &fileter_listjson);
166 // if (NULL == fileter_listjson) {
167 // printf("%s %d: object failure!\n", __FUNCTION__, __LINE__);
168 // }
169 str = json_object_get_string(fileter_listjson);
170 if (str) {
171 _filter_list->tag = strdup(str);
172 mbtk_log("fileter_listjson: %s\n", _filter_list->tag);
173 }
xf.li43643772024-03-04 19:39:53 -0800174 else
175 {
176 _filter_list->tag = "\0";
177 }
xf.li44e08692024-01-30 01:54:44 -0800178
179 //json_object_put(fileter_listjson);
liubin281ac462023-07-19 14:22:54 +0800180 _filter_list->next = (struct filter_list_t*)malloc(sizeof(struct filter_list_t));
181 if (NULL == _filter_list->next) {
182 printf("%s %d: malloc failure!\n", __FUNCTION__, __LINE__);
183 break;
184 }
185 tmp_filter_list = _filter_list;
186 _filter_list = _filter_list->next;
187 }
liubin281ac462023-07-19 14:22:54 +0800188 }
liubin281ac462023-07-19 14:22:54 +0800189
190 /***释放json对象***/
191 json_object_put(jsonobj);
xf.li44e08692024-01-30 01:54:44 -0800192 printf("MBTK_LOGD: parse_config end\n");
liubin281ac462023-07-19 14:22:54 +0800193 return 0;
194}
195
196#define LOGD_PID "/var/run/mbtk_logd.pid"
197
198static int save_pid(void)
199{
200 pid_t process_id;
201 int fd, ret;
202 char buf[12] = {0};
203
204 process_id = getpid();
205 if(access(LOGD_PID, F_OK) == 0)
206 {
207 printf("mbtk_logd 进程已经存在\n");
208 return -1;
209 }
210 fd = open(LOGD_PID, O_CREAT | O_WRONLY, 0600);
211 if (fd < 0) {
212 fprintf(stderr, "failed to open %s: %s\n", LOGD_PID, strerror(errno));
213 return -2;
214 }
215 snprintf(buf, sizeof(buf), "%d\n", process_id);
216 ret = write(fd, buf, strlen(buf));
217 close(fd);
218
219 if(ret > 0)
220 return 0;
221 else
222 return -2;
223}
224
xf.li43643772024-03-04 19:39:53 -0800225
liubin281ac462023-07-19 14:22:54 +0800226int main(int argc, char* argv[])
227{
228 log_config_entry listdata[5];
229 pthread_t pid[5] = {0};
230 int i, ret;
231 void* tret;
xf.li44e08692024-01-30 01:54:44 -0800232 struct filter_list_t* _filter_list = NULL;
233
b.liubb590492024-06-13 16:42:08 +0800234#ifdef MBTK_DUMP_SUPPORT
235 mbtk_debug_open(NULL, TRUE);
236#endif
237
xf.li44e08692024-01-30 01:54:44 -0800238 int lock_file = open("/tmp/mbtk_logd.lock", O_CREAT|O_RDWR, 0666);
239 int rc = flock(lock_file,LOCK_EX|LOCK_NB);
240 if(rc) {
241 if(EWOULDBLOCK == errno) {
242 printf("Error: cannot restart the mbtk_logd repeatedly\n");
243 exit(0);
244 }
245 }
liubin281ac462023-07-19 14:22:54 +0800246
247 memset(listdata, 0, sizeof(log_config_entry) * 5);
liubin281ac462023-07-19 14:22:54 +0800248 ret = parse_config(listdata);
249 if (ret) {
250 return -1;
251 }
liubin281ac462023-07-19 14:22:54 +0800252
253 printf("logd %s start !\n", __FUNCTION__);
254
255 for (i = 0; i < 5; ++i) {
256 if (NULL == listdata[i].name) {
257 break;
258 }
259
260 if (0 == listdata[i].enable) {
261 printf("%s log disabled !\n", listdata[i].name);
262 continue;
263 }
264
265 if (0 == memcmp(listdata[i].name, "radio", 5)) {
266 ret = pthread_create(&pid[i], NULL, alog_thread, &listdata[i]);
267 if (ret != 0) {
268 fprintf(stderr, "\n%s: Failed to create pthread\n", __FUNCTION__);
269 }
270 } else if (0 == memcmp(listdata[i].name, "syslog", 6)) {
271 ret = pthread_create(&pid[i], NULL, syslog_main, &listdata[i]);
272 if (ret != 0) {
273 fprintf(stderr, "\n%s %d: Failed to create pthread\n", __FUNCTION__, __LINE__);
274 }
275 } else if (0 == memcmp(listdata[i].name, "local_socket", 12)) {
276 ret = pthread_create(&pid[i], NULL, socket_log_thread, &listdata[i]);
277 if (ret != 0) {
278 fprintf(stderr, "\n%s %d: Failed to create pthread\n", __FUNCTION__, __LINE__);
279 }
xf.li44e08692024-01-30 01:54:44 -0800280 } else if (0 == memcmp(listdata[i].name, "/dev/ttyS0", 8)) {
liubin281ac462023-07-19 14:22:54 +0800281 ret = pthread_create(&pid[i], NULL, common_log_thread, &listdata[i]);
282 if (ret != 0) {
283 fprintf(stderr, "\n%s %d: Failed to create pthread\n", __FUNCTION__, __LINE__);
284 }
285 }
286 }
287
b.liu5fa2fb52024-06-20 18:26:34 +0800288
liubin281ac462023-07-19 14:22:54 +0800289 for (i = 0; i < 5; ++i) {
290 if (NULL == listdata[i].name) {
291 break;
292 }
293 if (pid[i]) {
294 if (pthread_join(pid[i], &tret) != 0) {
295 printf("Join thread %d : %d error!\n", i, pid[i]);
296 exit(1);
297 }
298 }
299 }
300 handler_free(listdata);
301
302 return 0;
303}