blob: 5dde3b8380ee20a443e05b9541567c74a897b861 [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.liu0f7ffad2024-11-06 19:57:27 +080013#include <string.h>
14#include <pthread.h>
15
b.liub3b923a2024-06-06 15:15:49 +080016#include "json-c/json.h"
17#include "json-c/printbuf.h"
liubin281ac462023-07-19 14:22:54 +080018#include "log_config.h"
19
b.liu5fa2fb52024-06-20 18:26:34 +080020#include "mbtk_type.h"
xf.li43643772024-03-04 19:39:53 -080021//#define DEBUG 1
liubin281ac462023-07-19 14:22:54 +080022
23#ifdef DEBUG
24#define mbtk_log(...) printf(__VA_ARGS__)
25#else
26#define mbtk_log(...)
27#endif
28
29static void handler_free(log_config_entry* listdata)
30{
31 int i, n;
32 struct filter_list_t* _filter_list = NULL;
33 log_config_entry* entry;
34
35 for (i = 0; i < 5; i++) {
36 entry = listdata + i;
37 if (entry->name) {
38 free(entry->name);
39 if (entry->out_path) {
40 free(entry->out_path);
41 }
42 if (entry->ip) {
43 free(entry->ip);
44 }
45 _filter_list = entry->filter_list;
46 for (n = 0; n < 10; n++) {
b.liu9e8584b2024-11-06 19:21:28 +080047 printf("%s %d: malloc %p!\n", __FUNCTION__, __LINE__, _filter_list);
liubin281ac462023-07-19 14:22:54 +080048 if (_filter_list) {
b.liu9e8584b2024-11-06 19:21:28 +080049 printf("%s %d: malloc %p!\n", __FUNCTION__, __LINE__, _filter_list->next);
liubin281ac462023-07-19 14:22:54 +080050 if (_filter_list->tag) {
51 free(_filter_list->tag);
52 }
53 free(_filter_list);
54 _filter_list = _filter_list->next;
55 } else {
56 break;
57 }
58 }
59 } else {
60 break;
61 }
62 }
63}
64
65int parse_config(log_config_entry* listdata)
66{
67 json_object* jsonobj = NULL;
68 json_object* tmpjson = NULL;
69 json_object* datajson = NULL;
70 json_object* listjson = NULL;
71 json_object* fileterjson = NULL;
72 json_object* fileter_listjson = NULL;
73 log_config_entry* entry;
xf.li43643772024-03-04 19:39:53 -080074 int i, n, ret, array_length;
b.liu0f7ffad2024-11-06 19:57:27 +080075 char* cmdval = NULL;
liubin281ac462023-07-19 14:22:54 +080076
xf.li44e08692024-01-30 01:54:44 -080077 printf("MBTK_LOGD: in parse_config\n");
liubin281ac462023-07-19 14:22:54 +080078 jsonobj = json_object_from_file(LOG_CONFIG_PATH);
79 if (NULL == jsonobj) {
80 printf("Can't open config file: %s\n", LOG_CONFIG_PATH);
81 return -1;
82 }
83 ret = json_object_object_get_ex(jsonobj, "logd", &tmpjson);
84 if (!ret) {
85 printf("get jsondata error ...\n");
86 }
87
88 if (NULL == tmpjson) {
89 printf("the tmpjson : [%s]\n", json_object_to_json_string(tmpjson));
90 }
91 /**获取total***/
b.liu0f7ffad2024-11-06 19:57:27 +080092 cmdval = (char*)json_object_get_string(tmpjson);
liubin281ac462023-07-19 14:22:54 +080093 printf("logd enable : %s\n", cmdval);
94 json_object_put(tmpjson);
xf.li44e08692024-01-30 01:54:44 -080095
liubin281ac462023-07-19 14:22:54 +080096 /***获取data***/
97 json_object_object_get_ex(jsonobj, "buffer_list", &tmpjson);
xf.li44e08692024-01-30 01:54:44 -080098
liubin281ac462023-07-19 14:22:54 +080099 for (i = 0 ; i < 5; i++) {
100 struct filter_list_t* _filter_list = NULL;
101 struct filter_list_t* tmp_filter_list = NULL;
102 datajson = json_object_array_get_idx(tmpjson, i);
103 if (NULL == datajson) {
104 mbtk_log("the datajson exit\n");
105 break;
106 }
107 entry = listdata + i;
108 json_object_object_get_ex(datajson, "enable", &listjson);
109 entry->enable = json_object_get_int(listjson);
110 mbtk_log("enable: %d\n", entry->enable);
111
112 json_object_object_get_ex(datajson, "name", &listjson);
b.liu0f7ffad2024-11-06 19:57:27 +0800113 entry->name = (char*)json_object_get_string(listjson);
b.liu9e8584b2024-11-06 19:21:28 +0800114 printf("cmdval: %s\n", entry->name);
liubin281ac462023-07-19 14:22:54 +0800115
116 json_object_object_get_ex(datajson, "log_file", &listjson);
b.liu0f7ffad2024-11-06 19:57:27 +0800117 entry->out_path = (char*)json_object_get_string(listjson);
liubin281ac462023-07-19 14:22:54 +0800118 mbtk_log("cmdval: %s\n", entry->out_path);
119
120 json_object_object_get_ex(datajson, "log_stream", &listjson);
121 if (listjson) {
b.liu0f7ffad2024-11-06 19:57:27 +0800122 entry->ip = (char*)json_object_get_string(listjson);
xf.li44e08692024-01-30 01:54:44 -0800123
liubin281ac462023-07-19 14:22:54 +0800124 cmdval = strstr(entry->ip, ":");
125 if (cmdval) {
126 entry->port = &cmdval[1];
127 cmdval[0] = 0;
128 mbtk_log("cmdval: %s [%s]\n", entry->ip, entry->port);
129 } else {
130 printf("Can't find port!!\n");
131 free(entry->ip);
132 entry->ip = NULL;
133 }
134 }
135 json_object_object_get_ex(datajson, "rotate_file_size", &listjson);
136 entry->rotate_file_size = json_object_get_int(listjson) * 1024;
137 mbtk_log("rotate_file_size: %d\n", entry->rotate_file_size);
138
139 json_object_object_get_ex(datajson, "rotate_file_count", &listjson);
140 entry->rotate_file_count = json_object_get_int(listjson);
141 mbtk_log("rotate_file_count: %d\n", entry->rotate_file_count);
142
143 json_object_object_get_ex(datajson, "filter_list", &listjson);
144 if (NULL == listjson) {
145 printf("%s %d: object failure!\n", __FUNCTION__, __LINE__);
146 json_object_put(listjson);
147 continue;
148 }
149 entry->filter_list = (struct filter_list_t*)malloc(sizeof(struct filter_list_t));
150 _filter_list = entry->filter_list;
151
xf.li43643772024-03-04 19:39:53 -0800152 array_length = json_object_array_length(listjson);
153 for (n = 0 ; n <= array_length; n++) {
liubin281ac462023-07-19 14:22:54 +0800154 fileterjson = json_object_array_get_idx(listjson, n);
155 if (NULL == fileterjson) {
156 mbtk_log("the fileterjson exit\n");
157 free(tmp_filter_list->next);
158 tmp_filter_list->next = NULL;
159 break;
160 }
161 memset(_filter_list, 0, sizeof(struct filter_list_t));
162 json_object_object_get_ex(fileterjson, "priority", &fileter_listjson);
b.liu0f7ffad2024-11-06 19:57:27 +0800163 const char* str = json_object_get_string(fileter_listjson);
liubin281ac462023-07-19 14:22:54 +0800164 if (str) {
165 _filter_list->priority = str[0];
166 mbtk_log("fileter_listjson: %c\n", _filter_list->priority);
167 }
168 json_object_object_get_ex(fileterjson, "tag", &fileter_listjson);
169 // if (NULL == fileter_listjson) {
170 // printf("%s %d: object failure!\n", __FUNCTION__, __LINE__);
171 // }
172 str = json_object_get_string(fileter_listjson);
173 if (str) {
174 _filter_list->tag = strdup(str);
175 mbtk_log("fileter_listjson: %s\n", _filter_list->tag);
176 }
xf.li43643772024-03-04 19:39:53 -0800177 else
178 {
179 _filter_list->tag = "\0";
180 }
xf.li44e08692024-01-30 01:54:44 -0800181
182 //json_object_put(fileter_listjson);
liubin281ac462023-07-19 14:22:54 +0800183 _filter_list->next = (struct filter_list_t*)malloc(sizeof(struct filter_list_t));
184 if (NULL == _filter_list->next) {
185 printf("%s %d: malloc failure!\n", __FUNCTION__, __LINE__);
186 break;
187 }
188 tmp_filter_list = _filter_list;
189 _filter_list = _filter_list->next;
190 }
liubin281ac462023-07-19 14:22:54 +0800191 }
liubin281ac462023-07-19 14:22:54 +0800192
193 /***释放json对象***/
194 json_object_put(jsonobj);
xf.li44e08692024-01-30 01:54:44 -0800195 printf("MBTK_LOGD: parse_config end\n");
liubin281ac462023-07-19 14:22:54 +0800196 return 0;
197}
198
199#define LOGD_PID "/var/run/mbtk_logd.pid"
b.liu0f7ffad2024-11-06 19:57:27 +0800200#if 0
liubin281ac462023-07-19 14:22:54 +0800201static int save_pid(void)
202{
203 pid_t process_id;
204 int fd, ret;
205 char buf[12] = {0};
206
207 process_id = getpid();
208 if(access(LOGD_PID, F_OK) == 0)
209 {
210 printf("mbtk_logd 进程已经存在\n");
211 return -1;
212 }
213 fd = open(LOGD_PID, O_CREAT | O_WRONLY, 0600);
214 if (fd < 0) {
215 fprintf(stderr, "failed to open %s: %s\n", LOGD_PID, strerror(errno));
216 return -2;
217 }
218 snprintf(buf, sizeof(buf), "%d\n", process_id);
219 ret = write(fd, buf, strlen(buf));
220 close(fd);
221
222 if(ret > 0)
223 return 0;
224 else
225 return -2;
226}
b.liu0f7ffad2024-11-06 19:57:27 +0800227#endif
xf.li43643772024-03-04 19:39:53 -0800228
liubin281ac462023-07-19 14:22:54 +0800229int main(int argc, char* argv[])
230{
231 log_config_entry listdata[5];
232 pthread_t pid[5] = {0};
233 int i, ret;
234 void* tret;
b.liu0f7ffad2024-11-06 19:57:27 +0800235// struct filter_list_t* _filter_list = NULL;
xf.li44e08692024-01-30 01:54:44 -0800236
b.liubb590492024-06-13 16:42:08 +0800237#ifdef MBTK_DUMP_SUPPORT
238 mbtk_debug_open(NULL, TRUE);
239#endif
240
xf.li44e08692024-01-30 01:54:44 -0800241 int lock_file = open("/tmp/mbtk_logd.lock", O_CREAT|O_RDWR, 0666);
242 int rc = flock(lock_file,LOCK_EX|LOCK_NB);
243 if(rc) {
244 if(EWOULDBLOCK == errno) {
245 printf("Error: cannot restart the mbtk_logd repeatedly\n");
246 exit(0);
247 }
248 }
liubin281ac462023-07-19 14:22:54 +0800249
250 memset(listdata, 0, sizeof(log_config_entry) * 5);
liubin281ac462023-07-19 14:22:54 +0800251 ret = parse_config(listdata);
252 if (ret) {
253 return -1;
254 }
liubin281ac462023-07-19 14:22:54 +0800255
256 printf("logd %s start !\n", __FUNCTION__);
257
258 for (i = 0; i < 5; ++i) {
259 if (NULL == listdata[i].name) {
260 break;
261 }
262
263 if (0 == listdata[i].enable) {
264 printf("%s log disabled !\n", listdata[i].name);
265 continue;
266 }
267
268 if (0 == memcmp(listdata[i].name, "radio", 5)) {
269 ret = pthread_create(&pid[i], NULL, alog_thread, &listdata[i]);
270 if (ret != 0) {
271 fprintf(stderr, "\n%s: Failed to create pthread\n", __FUNCTION__);
272 }
273 } else if (0 == memcmp(listdata[i].name, "syslog", 6)) {
274 ret = pthread_create(&pid[i], NULL, syslog_main, &listdata[i]);
275 if (ret != 0) {
276 fprintf(stderr, "\n%s %d: Failed to create pthread\n", __FUNCTION__, __LINE__);
277 }
278 } else if (0 == memcmp(listdata[i].name, "local_socket", 12)) {
279 ret = pthread_create(&pid[i], NULL, socket_log_thread, &listdata[i]);
280 if (ret != 0) {
281 fprintf(stderr, "\n%s %d: Failed to create pthread\n", __FUNCTION__, __LINE__);
282 }
xf.li44e08692024-01-30 01:54:44 -0800283 } else if (0 == memcmp(listdata[i].name, "/dev/ttyS0", 8)) {
liubin281ac462023-07-19 14:22:54 +0800284 ret = pthread_create(&pid[i], NULL, common_log_thread, &listdata[i]);
285 if (ret != 0) {
286 fprintf(stderr, "\n%s %d: Failed to create pthread\n", __FUNCTION__, __LINE__);
287 }
288 }
289 }
290
b.liu5fa2fb52024-06-20 18:26:34 +0800291
liubin281ac462023-07-19 14:22:54 +0800292 for (i = 0; i < 5; ++i) {
293 if (NULL == listdata[i].name) {
294 break;
295 }
296 if (pid[i]) {
297 if (pthread_join(pid[i], &tret) != 0) {
b.liu0f7ffad2024-11-06 19:57:27 +0800298 printf("Join thread %d : %ld error!\n", i, pid[i]);
liubin281ac462023-07-19 14:22:54 +0800299 exit(1);
300 }
301 }
302 }
303 handler_free(listdata);
304
305 return 0;
306}