blob: e096f8acf3cd96cdeb623e0cba92629ee8a21995 [file] [log] [blame]
b.liuf37bd332024-03-18 13:51:24 +08001#include <stdarg.h>
xf.li4bd9ee42024-04-13 01:31:44 -07002#include <sys/un.h>
3#include <sys/socket.h>
b.liub3b923a2024-06-06 15:15:49 +08004#include "json-c/json.h"
b.liuf37bd332024-03-18 13:51:24 +08005#include "lynq_deflog.h"
b.liu4e243dc2023-11-27 11:20:00 +08006#include "mbtk_type.h"
b.liu5fa9e772023-11-23 18:00:55 +08007
xf.li44e08692024-01-30 01:54:44 -08008#define LOG_CONFIG_PATH "/etc/mbtk/mbtk_log.json"
9#define SYSLOG_PATCH "syslog"
10#define SYSLOG_NAME "syslog"
11#define SYSLOG_INDEX 0
b.liu5fa9e772023-11-23 18:00:55 +080012
13void lynq_log_configuration_init(const char *log_name)
14{
xf.li44e08692024-01-30 01:54:44 -080015 //UNUSED(log_name);
16 mbtk_log_init(SYSLOG_PATCH, log_name);
b.liu5fa9e772023-11-23 18:00:55 +080017}
18
19void lynq_log_global_output(log_level_enum Level,const char *format,...)
20{
b.liuf37bd332024-03-18 13:51:24 +080021 va_list args;
22 va_start(args,format);
23 mbtk_log(Level, format, args);
24 va_end(args);
25}
b.liu5fa9e772023-11-23 18:00:55 +080026
b.liuf37bd332024-03-18 13:51:24 +080027const char* lynq_read_log_version()
28{
29 return "LOG-V1.0";
b.liu5fa9e772023-11-23 18:00:55 +080030}
31
32int lynq_syslog_set_file_size(int value)
33{
xf.li44e08692024-01-30 01:54:44 -080034// UNUSED(value);
35 json_object* jsonobj = NULL;
36 json_object* tmpjson = NULL;
37 json_object* datajson = NULL;
38 json_object* listjson = NULL;
b.liu5fa9e772023-11-23 18:00:55 +080039
xf.li44e08692024-01-30 01:54:44 -080040 int tmp_int;
41 char* tmp_string = NULL;
42
43 if(value < 1)
44 {
45 value = 1;
46 }
47 else if(value > 100)
48 {
49 value = 100;
50 }
xf.li43643772024-03-04 19:39:53 -080051 value = value*1024;
xf.li44e08692024-01-30 01:54:44 -080052
53 jsonobj = json_object_from_file(LOG_CONFIG_PATH);
54 if (NULL == jsonobj) {
55 printf("Can't open config file: %s\n", LOG_CONFIG_PATH);
56 system("echo Can't open config file > /dev/console");
57 return -1;
58 }
59 /***获取data***/
60 json_object_object_get_ex(jsonobj, "buffer_list", &tmpjson);
61 datajson = json_object_array_get_idx(tmpjson, SYSLOG_INDEX);
62 if (NULL == datajson) {
63 json_object_to_file(LOG_CONFIG_PATH, jsonobj);
64 json_object_put(jsonobj);
65 printf("NULL == datajson\n");
66 system("echo NULL == datajson > /dev/console");
67 return -1;
68 }
69
70 json_object_object_get_ex(datajson, "name", &listjson);
71 tmp_string = json_object_get_string(listjson);
72
73 json_object_object_get_ex(datajson, "enable", &listjson);
74 tmp_int = json_object_get_int(listjson);
75
76 if(strcmp(tmp_string, SYSLOG_NAME) != 0 || tmp_int != 1)
77 {
78 json_object_to_file(LOG_CONFIG_PATH, jsonobj);
79 json_object_put(jsonobj);
80 printf("SYSLOG_NAME error, tmp_int != 1\n");
81 system("echo SYSLOG_NAME error, tmp_int != 1 > /dev/console");
82 return -1;
83 }
84
85 json_object_object_add(datajson, "rotate_file_size", json_object_new_int(value));
86
87 /***释放json对象***/
88 json_object_to_file(LOG_CONFIG_PATH, jsonobj);
89 json_object_put(jsonobj);
b.liu5fa9e772023-11-23 18:00:55 +080090 return 0;
91}
92
93int lynq_syslog_get_file_size(void)
94{
xf.li44e08692024-01-30 01:54:44 -080095 json_object* jsonobj = NULL;
96 json_object* tmpjson = NULL;
97 json_object* datajson = NULL;
98 json_object* listjson = NULL;
b.liu5fa9e772023-11-23 18:00:55 +080099
xf.li44e08692024-01-30 01:54:44 -0800100 int tmp_int;
101 char* tmp_string = NULL;
102
103 jsonobj = json_object_from_file(LOG_CONFIG_PATH);
104 if (NULL == jsonobj) {
105 printf("Can't open config file: %s\n", LOG_CONFIG_PATH);
106 return -1;
107 }
108 /***获取data***/
109 json_object_object_get_ex(jsonobj, "buffer_list", &tmpjson);
110 datajson = json_object_array_get_idx(tmpjson, SYSLOG_INDEX);
111 if (NULL == datajson) {
112 json_object_put(jsonobj);
113 return -1;
114 }
115
116 json_object_object_get_ex(datajson, "name", &listjson);
117 tmp_string = json_object_get_string(listjson);
118
119 json_object_object_get_ex(datajson, "enable", &listjson);
120 tmp_int = json_object_get_int(listjson);
121
122 if(strcmp(tmp_string, SYSLOG_NAME) != 0 || tmp_int != 1)
123 {
124 json_object_put(jsonobj);
125 return -1;
126 }
127
128 json_object_object_get_ex(datajson, "rotate_file_size", &listjson);
129 tmp_int = json_object_get_int(listjson);
130
131 /***释放json对象***/
132 json_object_put(jsonobj);
133
xf.li43643772024-03-04 19:39:53 -0800134 return tmp_int/1024;
135// return tmp_int;
b.liu5fa9e772023-11-23 18:00:55 +0800136}
137
138int lynq_syslog_set_file_rotate(int value)
139{
xf.li44e08692024-01-30 01:54:44 -0800140 //UNUSED(value);
b.liu5fa9e772023-11-23 18:00:55 +0800141
xf.li44e08692024-01-30 01:54:44 -0800142 json_object* jsonobj = NULL;
143 json_object* tmpjson = NULL;
144 json_object* datajson = NULL;
145 json_object* listjson = NULL;
146
147 int tmp_int;
148 char* tmp_string = NULL;
149
150 if(value < 0)
151 {
152 value = 0;
153 }
154 else if(value > 99)
155 {
156 value = 99;
157 }
158
159 jsonobj = json_object_from_file(LOG_CONFIG_PATH);
160 if (NULL == jsonobj) {
161 printf("Can't open config file: %s\n", LOG_CONFIG_PATH);
162 return -1;
163 }
164 /***获取data***/
165 json_object_object_get_ex(jsonobj, "buffer_list", &tmpjson);
166 datajson = json_object_array_get_idx(tmpjson, SYSLOG_INDEX);
167 if (NULL == datajson) {
168 json_object_to_file(LOG_CONFIG_PATH, jsonobj);
169 json_object_put(jsonobj);
170 return -1;
171 }
172
173 json_object_object_get_ex(datajson, "name", &listjson);
174 tmp_string = json_object_get_string(listjson);
175
176 json_object_object_get_ex(datajson, "enable", &listjson);
177 tmp_int = json_object_get_int(listjson);
178
179 if(strcmp(tmp_string, SYSLOG_NAME) != 0 || tmp_int != 1)
180 {
181 json_object_to_file(LOG_CONFIG_PATH, jsonobj);
182 json_object_put(jsonobj);
183 return -1;
184 }
185
186 json_object_object_add(datajson, "rotate_file_count", json_object_new_int(value));
187
188 /***释放json对象***/
189 json_object_to_file(LOG_CONFIG_PATH, jsonobj);
190 json_object_put(jsonobj);
b.liu5fa9e772023-11-23 18:00:55 +0800191 return 0;
192}
193
194int lynq_syslog_get_file_rotate(void)
195{
xf.li44e08692024-01-30 01:54:44 -0800196 json_object* jsonobj = NULL;
197 json_object* tmpjson = NULL;
198 json_object* datajson = NULL;
199 json_object* listjson = NULL;
b.liu5fa9e772023-11-23 18:00:55 +0800200
xf.li44e08692024-01-30 01:54:44 -0800201 int tmp_int;
202 char* tmp_string = NULL;
203
204 jsonobj = json_object_from_file(LOG_CONFIG_PATH);
205 if (NULL == jsonobj) {
206 printf("Can't open config file: %s\n", LOG_CONFIG_PATH);
207 return -1;
208 }
209 /***获取data***/
210 json_object_object_get_ex(jsonobj, "buffer_list", &tmpjson);
211 datajson = json_object_array_get_idx(tmpjson, SYSLOG_INDEX);
212 if (NULL == datajson) {
213 json_object_put(jsonobj);
214 return -1;
215 }
216
217 json_object_object_get_ex(datajson, "name", &listjson);
218 tmp_string = json_object_get_string(listjson);
219
220 json_object_object_get_ex(datajson, "enable", &listjson);
221 tmp_int = json_object_get_int(listjson);
222
223 if(strcmp(tmp_string, SYSLOG_NAME) != 0 || tmp_int != 1)
224 {
225 json_object_put(jsonobj);
226 return -1;
227 }
228
229 json_object_object_get_ex(datajson, "rotate_file_count", &listjson);
230 tmp_int = json_object_get_int(listjson);
231
232 /***释放json对象***/
233 json_object_put(jsonobj);
234
235 return tmp_int;
b.liu5fa9e772023-11-23 18:00:55 +0800236}
237
xf.li43643772024-03-04 19:39:53 -0800238log_level_enum filter_char_to_pri(char c)
239{
240 switch (c) {
241 case 'v':
242 return LOG_VERBOSE;
243 case 'd':
244 return LOG_DEBUG;
245 case 'i':
246 return LOG_INFO;
247 case 'w':
248 return LOG_WARNING;
249 case 'e':
250 return LOG_ERROR;
251 case '*':
252 default:
253 return LOG_LEVEL_MAX;
254 }
255}
256
257char filter_pri_to_char(log_level_enum level)
258{
259 char char_level;
260 switch (level) {
261 case LOG_VERBOSE:
262 char_level = 'v';
263 break;
264 case LOG_DEBUG:
265 char_level = 'd';
266 break;
267 case LOG_INFO:
268 char_level = 'i';
269 break;
270 case LOG_WARNING:
271 char_level = 'w';
272 break;
273 case LOG_ERROR:
274 char_level = 'e';
275 break;
276 case LOG_LEVEL_MAX:
277 default:
278 char_level = '*';
279 break;
280 }
281 return char_level;
282}
283
b.liu5fa9e772023-11-23 18:00:55 +0800284int lynq_set_log_level(const char * module_name, log_level_enum level)
285{
xf.li43643772024-03-04 19:39:53 -0800286 json_object* jsonobj = NULL;
287 json_object* tmpjson = NULL;
288 json_object* datajson = NULL;
289 json_object* listjson = NULL;
290 json_object* fileterjson = NULL;
291 json_object* fileter_listjson = NULL;
292 json_object* new_fileter = NULL;
b.liu5fa9e772023-11-23 18:00:55 +0800293
xf.li43643772024-03-04 19:39:53 -0800294 int n = 0, array_length = 0;
295 char* tmp_string = NULL;
296 char level_string[5] = {'\0'};
297
298 jsonobj = json_object_from_file(LOG_CONFIG_PATH);
299 if (NULL == jsonobj) {
300 printf("Can't open config file: %s\n", LOG_CONFIG_PATH);
301 return -1;
302 }
303 /***获取data***/
304 json_object_object_get_ex(jsonobj, "buffer_list", &tmpjson);
305 datajson = json_object_array_get_idx(tmpjson, SYSLOG_INDEX);
306 if (NULL == datajson) {
307 json_object_put(jsonobj);
308 return -1;
309 }
310
311 json_object_object_get_ex(datajson, "filter_list", &listjson);
312 if (NULL == listjson) {
313 printf("%s %d: object failure!\n", __FUNCTION__, __LINE__);
314 json_object_put(listjson);
315 return -1;
316 }
317 array_length = json_object_array_length(listjson);
318 for (n = 0; n <= array_length; n++) {
319 fileterjson = json_object_array_get_idx(listjson, n);
320 if (NULL == fileterjson) {
321 new_fileter = json_object_new_object();
322 sprintf(level_string, "%c", filter_pri_to_char(level));
323 json_object_object_add(new_fileter, "priority", json_object_new_string(level_string));
324 json_object_object_add(new_fileter, "tag", json_object_new_string(module_name));
325 json_object_array_add(listjson, new_fileter);
326 break;
327 }
328
329 json_object_object_get_ex(fileterjson, "tag", &fileter_listjson);
330 char *str = json_object_get_string(fileter_listjson);
331 if (str) {
332 tmp_string = strdup(str);
333 if(strcmp(module_name, tmp_string) == 0)
334 {
335 sprintf(level_string, "%c", filter_pri_to_char(level));
336 json_object_object_add(fileterjson, "priority", json_object_new_string(level_string));
337 json_object_to_file(LOG_CONFIG_PATH, jsonobj);
338 json_object_put(jsonobj);
339 return 0;
340 }
341 }
342 else
343 {
344 continue;
345 }
346 }
347 /***释放json对象***/
348 json_object_to_file(LOG_CONFIG_PATH, jsonobj);
349 json_object_put(jsonobj);
b.liu5fa9e772023-11-23 18:00:55 +0800350
351 return 0;
352}
353
354int lynq_get_log_level(const char * module_name, log_level_enum *level)
355{
xf.li43643772024-03-04 19:39:53 -0800356 json_object* jsonobj = NULL;
357 json_object* tmpjson = NULL;
358 json_object* datajson = NULL;
359 json_object* listjson = NULL;
360 json_object* fileterjson = NULL;
361 json_object* fileter_listjson = NULL;
b.liu5fa9e772023-11-23 18:00:55 +0800362
xf.li43643772024-03-04 19:39:53 -0800363 int n;
364 char* tmp_string = NULL;
365
366 jsonobj = json_object_from_file(LOG_CONFIG_PATH);
367 if (NULL == jsonobj) {
368 printf("Can't open config file: %s\n", LOG_CONFIG_PATH);
369 return -1;
370 }
371 /***获取data***/
372 json_object_object_get_ex(jsonobj, "buffer_list", &tmpjson);
373 datajson = json_object_array_get_idx(tmpjson, SYSLOG_INDEX);
374 if (NULL == datajson) {
375 json_object_put(jsonobj);
376 return -1;
377 }
378
379 json_object_object_get_ex(datajson, "filter_list", &listjson);
380 if (NULL == listjson) {
381 printf("%s %d: object failure!\n", __FUNCTION__, __LINE__);
382 json_object_put(listjson);
383 return -1;
384 }
385
386 for (n = 0 ; n < 5; n++) {
387 fileterjson = json_object_array_get_idx(listjson, n);
388 if (NULL == fileterjson) {
389 printf("the fileterjson exit\n");
390 break;
391 }
392
393 json_object_object_get_ex(fileterjson, "tag", &fileter_listjson);
394 char *str = json_object_get_string(fileter_listjson);
395 if (str) {
396 tmp_string = strdup(str);
397 printf("tag is %s\n", tmp_string);
398 if(strcmp(module_name, tmp_string) == 0)
399 {
400 json_object_object_get_ex(fileterjson, "priority", &fileter_listjson);
401 str = json_object_get_string(fileter_listjson);
402 if (str) {
403 tmp_string = str[0];
404 printf("fileter_listjson: %c\n", tmp_string);
405 *level = filter_char_to_pri(tmp_string);
406 //get the log level
407 json_object_put(jsonobj);
408 return 0;
409 }
410 else
411 {
412 break;
413 }
414 }
415 }
416 else
417 {
418 continue;
419 }
420 }
421 *level = LOG_UNSET;
422 /***释放json对象***/
423 json_object_put(jsonobj);
b.liu5fa9e772023-11-23 18:00:55 +0800424
425 return 0;
426}
427
428int lynq_set_special_log_level(const char * exe_name, const char * module_name, log_level_enum level)
429{
430 UNUSED(exe_name);
431 UNUSED(module_name);
432 UNUSED(level);
433
434
435 return 0;
436}
437
438int lynq_get_special_log_level(const char * exe_name, const char * module_name, log_level_enum *level)
439{
440 UNUSED(exe_name);
441 UNUSED(module_name);
442 UNUSED(level);
443
444
445 return 0;
446}
447
448int lynq_notify_recalc_log_level(pid_t pid)
449{
450 UNUSED(pid);
xf.li4bd9ee42024-04-13 01:31:44 -0700451 char sendBuff[100]={'\0'};
452 int serverFd,clientFd,addrLen,nwrite,nread;
453 struct sockaddr_un serverAddr,clientAddr;
b.liu5fa9e772023-11-23 18:00:55 +0800454
xf.li4bd9ee42024-04-13 01:31:44 -0700455 unlink("/var/log_client.socket"); /* in case it already exists */
456
457 memset(&clientAddr,0,sizeof(clientAddr));
458 memset(&serverAddr,0,sizeof(serverAddr));
459 clientAddr.sun_family = AF_UNIX;
460 sprintf(clientAddr.sun_path,"%s","/var/log_client.socket");
461
462 if ((clientFd = socket(AF_UNIX,SOCK_STREAM,0)) < 0)
463 {
464 printf("err -1\n");
465 return -1;
466 }
467
468 if (bind(clientFd, (struct sockaddr *)&clientAddr, sizeof(clientAddr)) < 0)
469 {
470 printf("err -2\n");
471 close(clientFd);
472 return -2;
473 }
474
475 serverAddr.sun_family = AF_UNIX;
476 sprintf(serverAddr.sun_path, "/var/log_server.socket");
477 if (connect(clientFd, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0)
478 {
479 printf("err -3\n");
480 close(clientFd);
481 return -3;
482 }
483
484 sprintf(sendBuff,"%s","update");
485 if ((nwrite = write(clientFd, sendBuff, 100)) < 0)
486 {
487 printf("err -4\n");
488 close(clientFd);
489 return -4;
490 }
491
492 close(clientFd);
b.liu5fa9e772023-11-23 18:00:55 +0800493 return 0;
xf.li44e08692024-01-30 01:54:44 -0800494}