blob: 25dbdb10dd6ae379bc9c3be8145e443faeb9fe44 [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>
10#include <sys/types.h>
11#include <sys/stat.h>
12#include "json/json.h"
13#include "json/printbuf.h"
14#include "log_config.h"
15
16// #define DEBUG 1
17
18#ifdef DEBUG
19#define mbtk_log(...) printf(__VA_ARGS__)
20#else
21#define mbtk_log(...)
22#endif
23
24static void handler_free(log_config_entry* listdata)
25{
26 int i, n;
27 struct filter_list_t* _filter_list = NULL;
28 log_config_entry* entry;
29
30 for (i = 0; i < 5; i++) {
31 entry = listdata + i;
32 if (entry->name) {
33 free(entry->name);
34 if (entry->out_path) {
35 free(entry->out_path);
36 }
37 if (entry->ip) {
38 free(entry->ip);
39 }
40 _filter_list = entry->filter_list;
41 for (n = 0; n < 10; n++) {
42 printf("%s %d: malloc %x!\n", __FUNCTION__, __LINE__, _filter_list);
43 if (_filter_list) {
44 printf("%s %d: malloc %x!\n", __FUNCTION__, __LINE__, _filter_list->next);
45 if (_filter_list->tag) {
46 free(_filter_list->tag);
47 }
48 free(_filter_list);
49 _filter_list = _filter_list->next;
50 } else {
51 break;
52 }
53 }
54 } else {
55 break;
56 }
57 }
58}
59
60int parse_config(log_config_entry* listdata)
61{
62 json_object* jsonobj = NULL;
63 json_object* tmpjson = NULL;
64 json_object* datajson = NULL;
65 json_object* listjson = NULL;
66 json_object* fileterjson = NULL;
67 json_object* fileter_listjson = NULL;
68 log_config_entry* entry;
69 int i, n, ret;
70 char* cmdval = NULL;
71
72 jsonobj = json_object_from_file(LOG_CONFIG_PATH);
73 if (NULL == jsonobj) {
74 printf("Can't open config file: %s\n", LOG_CONFIG_PATH);
75 return -1;
76 }
77 ret = json_object_object_get_ex(jsonobj, "logd", &tmpjson);
78 if (!ret) {
79 printf("get jsondata error ...\n");
80 }
81
82 if (NULL == tmpjson) {
83 printf("the tmpjson : [%s]\n", json_object_to_json_string(tmpjson));
84 }
85 /**获取total***/
86 cmdval = json_object_get_string(tmpjson);
87 printf("logd enable : %s\n", cmdval);
88 json_object_put(tmpjson);
89 /***获取data***/
90 json_object_object_get_ex(jsonobj, "buffer_list", &tmpjson);
91 for (i = 0 ; i < 5; i++) {
92 struct filter_list_t* _filter_list = NULL;
93 struct filter_list_t* tmp_filter_list = NULL;
94 datajson = json_object_array_get_idx(tmpjson, i);
95 if (NULL == datajson) {
96 mbtk_log("the datajson exit\n");
97 break;
98 }
99 entry = listdata + i;
100 json_object_object_get_ex(datajson, "enable", &listjson);
101 entry->enable = json_object_get_int(listjson);
102 mbtk_log("enable: %d\n", entry->enable);
103
104 json_object_object_get_ex(datajson, "name", &listjson);
105 entry->name = strdup(json_object_get_string(listjson));
106 mbtk_log("cmdval: %x, %s\n", entry->name, entry->name);
107
108 json_object_object_get_ex(datajson, "log_file", &listjson);
109 entry->out_path = strdup(json_object_get_string(listjson));
110 mbtk_log("cmdval: %s\n", entry->out_path);
111
112 json_object_object_get_ex(datajson, "log_stream", &listjson);
113 if (listjson) {
114 entry->ip = strdup(json_object_get_string(listjson));
115 cmdval = strstr(entry->ip, ":");
116 if (cmdval) {
117 entry->port = &cmdval[1];
118 cmdval[0] = 0;
119 mbtk_log("cmdval: %s [%s]\n", entry->ip, entry->port);
120 } else {
121 printf("Can't find port!!\n");
122 free(entry->ip);
123 entry->ip = NULL;
124 }
125 }
126 json_object_object_get_ex(datajson, "rotate_file_size", &listjson);
127 entry->rotate_file_size = json_object_get_int(listjson) * 1024;
128 mbtk_log("rotate_file_size: %d\n", entry->rotate_file_size);
129
130 json_object_object_get_ex(datajson, "rotate_file_count", &listjson);
131 entry->rotate_file_count = json_object_get_int(listjson);
132 mbtk_log("rotate_file_count: %d\n", entry->rotate_file_count);
133
134 json_object_object_get_ex(datajson, "filter_list", &listjson);
135 if (NULL == listjson) {
136 printf("%s %d: object failure!\n", __FUNCTION__, __LINE__);
137 json_object_put(listjson);
138 continue;
139 }
140 entry->filter_list = (struct filter_list_t*)malloc(sizeof(struct filter_list_t));
141 _filter_list = entry->filter_list;
142
143 for (n = 0 ; n < 5; n++) {
144 fileterjson = json_object_array_get_idx(listjson, n);
145 if (NULL == fileterjson) {
146 mbtk_log("the fileterjson exit\n");
147 free(tmp_filter_list->next);
148 tmp_filter_list->next = NULL;
149 break;
150 }
151 memset(_filter_list, 0, sizeof(struct filter_list_t));
152 json_object_object_get_ex(fileterjson, "priority", &fileter_listjson);
153 char* str = json_object_get_string(fileter_listjson);
154 if (str) {
155 _filter_list->priority = str[0];
156 mbtk_log("fileter_listjson: %c\n", _filter_list->priority);
157 }
158 json_object_object_get_ex(fileterjson, "tag", &fileter_listjson);
159 // if (NULL == fileter_listjson) {
160 // printf("%s %d: object failure!\n", __FUNCTION__, __LINE__);
161 // }
162 str = json_object_get_string(fileter_listjson);
163 if (str) {
164 _filter_list->tag = strdup(str);
165 mbtk_log("fileter_listjson: %s\n", _filter_list->tag);
166 }
167 json_object_put(fileter_listjson);
168 _filter_list->next = (struct filter_list_t*)malloc(sizeof(struct filter_list_t));
169 if (NULL == _filter_list->next) {
170 printf("%s %d: malloc failure!\n", __FUNCTION__, __LINE__);
171 break;
172 }
173 tmp_filter_list = _filter_list;
174 _filter_list = _filter_list->next;
175 }
176 json_object_put(listjson);
177 }
178 json_object_put(tmpjson);
179
180 /***释放json对象***/
181 json_object_put(jsonobj);
182
183 return 0;
184}
185
186#define LOGD_PID "/var/run/mbtk_logd.pid"
187
188static int save_pid(void)
189{
190 pid_t process_id;
191 int fd, ret;
192 char buf[12] = {0};
193
194 process_id = getpid();
195 if(access(LOGD_PID, F_OK) == 0)
196 {
197 printf("mbtk_logd 进程已经存在\n");
198 return -1;
199 }
200 fd = open(LOGD_PID, O_CREAT | O_WRONLY, 0600);
201 if (fd < 0) {
202 fprintf(stderr, "failed to open %s: %s\n", LOGD_PID, strerror(errno));
203 return -2;
204 }
205 snprintf(buf, sizeof(buf), "%d\n", process_id);
206 ret = write(fd, buf, strlen(buf));
207 close(fd);
208
209 if(ret > 0)
210 return 0;
211 else
212 return -2;
213}
214
215int main(int argc, char* argv[])
216{
217 log_config_entry listdata[5];
218 pthread_t pid[5] = {0};
219 int i, ret;
220 void* tret;
221
222 memset(listdata, 0, sizeof(log_config_entry) * 5);
223
224 ret = parse_config(listdata);
225 if (ret) {
226 return -1;
227 }
228 if(0 != save_pid())
229 {
230 printf("%s %d: logd exit!\n", __FUNCTION__, __LINE__);
231 return -1;
232 }
233
234 printf("logd %s start !\n", __FUNCTION__);
235
236 for (i = 0; i < 5; ++i) {
237 if (NULL == listdata[i].name) {
238 break;
239 }
240
241 if (0 == listdata[i].enable) {
242 printf("%s log disabled !\n", listdata[i].name);
243 continue;
244 }
245
246 if (0 == memcmp(listdata[i].name, "radio", 5)) {
247 ret = pthread_create(&pid[i], NULL, alog_thread, &listdata[i]);
248 if (ret != 0) {
249 fprintf(stderr, "\n%s: Failed to create pthread\n", __FUNCTION__);
250 }
251 } else if (0 == memcmp(listdata[i].name, "syslog", 6)) {
252 ret = pthread_create(&pid[i], NULL, syslog_main, &listdata[i]);
253 if (ret != 0) {
254 fprintf(stderr, "\n%s %d: Failed to create pthread\n", __FUNCTION__, __LINE__);
255 }
256 } else if (0 == memcmp(listdata[i].name, "local_socket", 12)) {
257 ret = pthread_create(&pid[i], NULL, socket_log_thread, &listdata[i]);
258 if (ret != 0) {
259 fprintf(stderr, "\n%s %d: Failed to create pthread\n", __FUNCTION__, __LINE__);
260 }
261 } else if (0 == memcmp(listdata[i].name, "/dev/tty", 8)) {
262 ret = pthread_create(&pid[i], NULL, common_log_thread, &listdata[i]);
263 if (ret != 0) {
264 fprintf(stderr, "\n%s %d: Failed to create pthread\n", __FUNCTION__, __LINE__);
265 }
266 }
267 }
268
269 for (i = 0; i < 5; ++i) {
270 if (NULL == listdata[i].name) {
271 break;
272 }
273 if (pid[i]) {
274 if (pthread_join(pid[i], &tret) != 0) {
275 printf("Join thread %d : %d error!\n", i, pid[i]);
276 exit(1);
277 }
278 }
279 }
280 handler_free(listdata);
281
282 return 0;
283}