blob: d8282266a4bb923c1857b6d7336818eb6885f6b1 [file] [log] [blame]
b.liu68a94c92025-05-24 12:53:41 +08001#include <stdarg.h>
2#include <sys/un.h>
3#include <sys/socket.h>
4#include "json-c/json.h"
5#include "lynq_deflog.h"
6#include "mbtk_type.h"
7#include <signal.h>
8#include "mbtk_utils.h"
9
10#define LOG_CONFIG_PATH "/etc/mbtk/mbtk_log.json"
11#define SYSLOG_PATCH "syslog"
12#define SYSLOG_NAME "syslog"
13#define SYSLOG_INDEX 0
14
15#define RADIOLOG_NAME "radio"
16#define RADIO_INDEX 1
17
18void lynq_log_configuration_init(const char *log_name)
19{
20 //UNUSED(log_name);
21 mbtk_log_init(SYSLOG_PATCH, (char*)log_name);
22}
23
24void lynq_log_global_output(log_level_enum Level,const char *format,...)
25{
26 va_list args;
27 va_start(args,format);
28 mbtk_log(Level, format, args);
29 va_end(args);
30}
31
32const char* lynq_read_log_version()
33{
34 return "LOG-V1.0";
35}
36
37int lynq_syslog_set_file_size(int value)
38{
39// UNUSED(value);
40 json_object* jsonobj = NULL;
41 json_object* tmpjson = NULL;
42 json_object* datajson = NULL;
43 json_object* listjson = NULL;
44
45 int tmp_int;
46 const char* tmp_string = NULL;
47
48 if(value < 1)
49 {
50 value = 1;
51 }
52 else if(value > 100)
53 {
54 value = 100;
55 }
56 value = value*1024;
57
58 jsonobj = json_object_from_file(LOG_CONFIG_PATH);
59 if (NULL == jsonobj) {
60 printf("Can't open config file: %s\n", LOG_CONFIG_PATH);
61 mbtk_system("echo Can't open config file > /dev/console");
62 return -1;
63 }
64 /***获取data***/
65 json_object_object_get_ex(jsonobj, "buffer_list", &tmpjson);
66 datajson = json_object_array_get_idx(tmpjson, SYSLOG_INDEX);
67 if (NULL == datajson) {
68 json_object_to_file(LOG_CONFIG_PATH, jsonobj);
69 json_object_put(jsonobj);
70 printf("NULL == datajson\n");
71 mbtk_system("echo NULL == datajson > /dev/console");
72 return -1;
73 }
74
75 json_object_object_get_ex(datajson, "name", &listjson);
76 tmp_string = json_object_get_string(listjson);
77
78 json_object_object_get_ex(datajson, "enable", &listjson);
79 tmp_int = json_object_get_int(listjson);
80
81 if(strcmp(tmp_string, SYSLOG_NAME) != 0 || tmp_int != 1)
82 {
83 json_object_to_file(LOG_CONFIG_PATH, jsonobj);
84 json_object_put(jsonobj);
85 printf("SYSLOG_NAME error, tmp_int != 1\n");
86 mbtk_system("echo SYSLOG_NAME error, tmp_int != 1 > /dev/console");
87 return -1;
88 }
89
90 json_object_object_add(datajson, "rotate_file_size", json_object_new_int(value));
91
92 /***释放json对象***/
93 json_object_to_file(LOG_CONFIG_PATH, jsonobj);
94 json_object_put(jsonobj);
95 return 0;
96}
97
98int lynq_syslog_get_file_size(void)
99{
100 json_object* jsonobj = NULL;
101 json_object* tmpjson = NULL;
102 json_object* datajson = NULL;
103 json_object* listjson = NULL;
104
105 int tmp_int;
106 const char* tmp_string = NULL;
107
108 jsonobj = json_object_from_file(LOG_CONFIG_PATH);
109 if (NULL == jsonobj) {
110 printf("Can't open config file: %s\n", LOG_CONFIG_PATH);
111 return -1;
112 }
113 /***获取data***/
114 json_object_object_get_ex(jsonobj, "buffer_list", &tmpjson);
115 datajson = json_object_array_get_idx(tmpjson, SYSLOG_INDEX);
116 if (NULL == datajson) {
117 json_object_put(jsonobj);
118 return -1;
119 }
120
121 json_object_object_get_ex(datajson, "name", &listjson);
122 tmp_string = json_object_get_string(listjson);
123
124 json_object_object_get_ex(datajson, "enable", &listjson);
125 tmp_int = json_object_get_int(listjson);
126
127 if(strcmp(tmp_string, SYSLOG_NAME) != 0 || tmp_int != 1)
128 {
129 json_object_put(jsonobj);
130 return -1;
131 }
132
133 json_object_object_get_ex(datajson, "rotate_file_size", &listjson);
134 tmp_int = json_object_get_int(listjson);
135
136 /***释放json对象***/
137 json_object_put(jsonobj);
138
139 return tmp_int/1024;
140// return tmp_int;
141}
142
143int lynq_syslog_set_file_rotate(int value)
144{
145 //UNUSED(value);
146
147 json_object* jsonobj = NULL;
148 json_object* tmpjson = NULL;
149 json_object* datajson = NULL;
150 json_object* listjson = NULL;
151
152 int tmp_int;
153 const char* tmp_string = NULL;
154
155 if(value < 0)
156 {
157 value = 0;
158 }
159 else if(value > 99)
160 {
161 value = 99;
162 }
163
164 jsonobj = json_object_from_file(LOG_CONFIG_PATH);
165 if (NULL == jsonobj) {
166 printf("Can't open config file: %s\n", LOG_CONFIG_PATH);
167 return -1;
168 }
169 /***获取data***/
170 json_object_object_get_ex(jsonobj, "buffer_list", &tmpjson);
171 datajson = json_object_array_get_idx(tmpjson, SYSLOG_INDEX);
172 if (NULL == datajson) {
173 json_object_to_file(LOG_CONFIG_PATH, jsonobj);
174 json_object_put(jsonobj);
175 return -1;
176 }
177
178 json_object_object_get_ex(datajson, "name", &listjson);
179 tmp_string = json_object_get_string(listjson);
180
181 json_object_object_get_ex(datajson, "enable", &listjson);
182 tmp_int = json_object_get_int(listjson);
183
184 if(strcmp(tmp_string, SYSLOG_NAME) != 0 || tmp_int != 1)
185 {
186 json_object_to_file(LOG_CONFIG_PATH, jsonobj);
187 json_object_put(jsonobj);
188 return -1;
189 }
190
191 json_object_object_add(datajson, "rotate_file_count", json_object_new_int(value));
192
193 /***释放json对象***/
194 json_object_to_file(LOG_CONFIG_PATH, jsonobj);
195 json_object_put(jsonobj);
196 return 0;
197}
198
199int lynq_syslog_get_file_rotate(void)
200{
201 json_object* jsonobj = NULL;
202 json_object* tmpjson = NULL;
203 json_object* datajson = NULL;
204 json_object* listjson = NULL;
205
206 int tmp_int;
207 const char* tmp_string = NULL;
208
209 jsonobj = json_object_from_file(LOG_CONFIG_PATH);
210 if (NULL == jsonobj) {
211 printf("Can't open config file: %s\n", LOG_CONFIG_PATH);
212 return -1;
213 }
214 /***获取data***/
215 json_object_object_get_ex(jsonobj, "buffer_list", &tmpjson);
216 datajson = json_object_array_get_idx(tmpjson, SYSLOG_INDEX);
217 if (NULL == datajson) {
218 json_object_put(jsonobj);
219 return -1;
220 }
221
222 json_object_object_get_ex(datajson, "name", &listjson);
223 tmp_string = json_object_get_string(listjson);
224
225 json_object_object_get_ex(datajson, "enable", &listjson);
226 tmp_int = json_object_get_int(listjson);
227
228 if(strcmp(tmp_string, SYSLOG_NAME) != 0 || tmp_int != 1)
229 {
230 json_object_put(jsonobj);
231 return -1;
232 }
233
234 json_object_object_get_ex(datajson, "rotate_file_count", &listjson);
235 tmp_int = json_object_get_int(listjson);
236
237 /***释放json对象***/
238 json_object_put(jsonobj);
239
240 return tmp_int;
241}
242
243log_level_enum filter_char_to_pri(char c)
244{
245 switch (c) {
246 case 'v':
247 return LOG_VERBOSE;
248 case 'd':
249 return LOG_DEBUG;
250 case 'i':
251 return LOG_INFO;
252 case 'w':
253 return LOG_WARNING;
254 case 'e':
255 return LOG_ERROR;
256 case '*':
257 default:
258 return LOG_LEVEL_MAX;
259 }
260}
261
262char filter_pri_to_char(log_level_enum level)
263{
264 char char_level;
265 switch (level) {
266 case LOG_VERBOSE:
267 char_level = 'v';
268 break;
269 case LOG_DEBUG:
270 char_level = 'd';
271 break;
272 case LOG_UNSET:
273 case LOG_INFO:
274 char_level = 'i';
275 break;
276 case LOG_WARNING:
277 char_level = 'w';
278 break;
279 case LOG_ERROR:
280 char_level = 'e';
281 break;
282 case LOG_LEVEL_MAX:
283 default:
284 char_level = '*';
285 break;
286 }
287 return char_level;
288}
289
290int lynq_set_log_level(const char * module_name, log_level_enum level)
291{
292 json_object* jsonobj = NULL;
293 json_object* tmpjson = NULL;
294 json_object* datajson = NULL;
295 json_object* listjson = NULL;
296 json_object* fileterjson = NULL;
297 json_object* fileter_listjson = NULL;
298 json_object* new_fileter = NULL;
299
300 int n = 0, array_length = 0;
301 char* tmp_string = NULL;
302 char level_string[5] = {'\0'};
303
304 jsonobj = json_object_from_file(LOG_CONFIG_PATH);
305 if (NULL == jsonobj) {
306 printf("Can't open config file: %s\n", LOG_CONFIG_PATH);
307 return -1;
308 }
309 /***获取data***/
310 json_object_object_get_ex(jsonobj, "buffer_list", &tmpjson);
311 datajson = json_object_array_get_idx(tmpjson, SYSLOG_INDEX);
312 if (NULL == datajson) {
313 json_object_put(jsonobj);
314 return -1;
315 }
316
317 json_object_object_get_ex(datajson, "filter_list", &listjson);
318 if (NULL == listjson) {
319 printf("%s %d: object failure!\n", __FUNCTION__, __LINE__);
320 json_object_put(listjson);
321 return -1;
322 }
323 array_length = json_object_array_length(listjson);
324 for (n = 0; n <= array_length; n++) {
325 fileterjson = json_object_array_get_idx(listjson, n);
326 if (NULL == fileterjson) {
327 new_fileter = json_object_new_object();
328 sprintf(level_string, "%c", filter_pri_to_char(level));
329 json_object_object_add(new_fileter, "priority", json_object_new_string(level_string));
330 json_object_object_add(new_fileter, "tag", json_object_new_string(module_name));
331 json_object_array_add(listjson, new_fileter);
332 break;
333 }
334
335 json_object_object_get_ex(fileterjson, "tag", &fileter_listjson);
336 const char *str = json_object_get_string(fileter_listjson);
337 if (str) {
338 tmp_string = strdup(str);
339 if(strcmp(module_name, tmp_string) == 0)
340 {
341 sprintf(level_string, "%c", filter_pri_to_char(level));
342 json_object_object_add(fileterjson, "priority", json_object_new_string(level_string));
343 json_object_to_file(LOG_CONFIG_PATH, jsonobj);
344 json_object_put(jsonobj);
345 return 0;
346 }
347 }
348 else
349 {
350 continue;
351 }
352 }
353 /***释放json对象***/
354 json_object_to_file(LOG_CONFIG_PATH, jsonobj);
355 json_object_put(jsonobj);
356
357 return 0;
358}
359
360int lynq_get_log_level(const char * module_name, log_level_enum *level)
361{
362 json_object* jsonobj = NULL;
363 json_object* tmpjson = NULL;
364 json_object* datajson = NULL;
365 json_object* listjson = NULL;
366 json_object* fileterjson = NULL;
367 json_object* fileter_listjson = NULL;
368
369 int n;
370 char* tmp_string = NULL;
371
372 jsonobj = json_object_from_file(LOG_CONFIG_PATH);
373 if (NULL == jsonobj) {
374 printf("Can't open config file: %s\n", LOG_CONFIG_PATH);
375 return -1;
376 }
377 /***获取data***/
378 json_object_object_get_ex(jsonobj, "buffer_list", &tmpjson);
379 datajson = json_object_array_get_idx(tmpjson, SYSLOG_INDEX);
380 if (NULL == datajson) {
381 json_object_put(jsonobj);
382 return -1;
383 }
384
385 json_object_object_get_ex(datajson, "filter_list", &listjson);
386 if (NULL == listjson) {
387 printf("%s %d: object failure!\n", __FUNCTION__, __LINE__);
388 json_object_put(listjson);
389 return -1;
390 }
391
392 for (n = 0 ; n < 5; n++) {
393 fileterjson = json_object_array_get_idx(listjson, n);
394 if (NULL == fileterjson) {
395 printf("the fileterjson exit\n");
396 break;
397 }
398
399 json_object_object_get_ex(fileterjson, "tag", &fileter_listjson);
400 const char *str = json_object_get_string(fileter_listjson);
401 if (str) {
402 tmp_string = strdup(str);
403 printf("tag is %s\n", tmp_string);
404 if(strcmp(module_name, tmp_string) == 0)
405 {
406 json_object_object_get_ex(fileterjson, "priority", &fileter_listjson);
407 str = json_object_get_string(fileter_listjson);
408 if (str) {
409 *tmp_string = str[0];
410 printf("fileter_listjson: %c\n", *tmp_string);
411 *level = filter_char_to_pri(*tmp_string);
412 //get the log level
413 json_object_put(jsonobj);
414 return 0;
415 }
416 else
417 {
418 break;
419 }
420 }
421 }
422 else
423 {
424 continue;
425 }
426 }
427 *level = LOG_UNSET;
428 /***释放json对象***/
429 json_object_put(jsonobj);
430
431 return 0;
432}
433
434int lynq_set_special_log_level(const char * exe_name, const char * module_name, log_level_enum level)
435{
436 UNUSED(exe_name);
437 UNUSED(module_name);
438 UNUSED(level);
439
440
441 return 0;
442}
443
444int lynq_get_special_log_level(const char * exe_name, const char * module_name, log_level_enum *level)
445{
446 UNUSED(exe_name);
447 UNUSED(module_name);
448 UNUSED(level);
449
450
451 return 0;
452}
453
454int lynq_notify_recalc_log_level(pid_t pid)
455{
456 UNUSED(pid);
457 char sendBuff[100]={'\0'};
458 int clientFd,nwrite;
459 struct sockaddr_un serverAddr,clientAddr;
460
461 unlink("/var/log_client.socket"); /* in case it already exists */
462
463 memset(&clientAddr,0,sizeof(clientAddr));
464 memset(&serverAddr,0,sizeof(serverAddr));
465 clientAddr.sun_family = AF_UNIX;
466 sprintf(clientAddr.sun_path,"%s","/var/log_client.socket");
467
468 if ((clientFd = socket(AF_UNIX,SOCK_STREAM,0)) < 0)
469 {
470 printf("err -1\n");
471 return -1;
472 }
473
474 if (bind(clientFd, (struct sockaddr *)&clientAddr, sizeof(clientAddr)) < 0)
475 {
476 printf("err -2\n");
477 close(clientFd);
478 return -2;
479 }
480
481 serverAddr.sun_family = AF_UNIX;
482 sprintf(serverAddr.sun_path, "/var/log_server.socket");
483 if (connect(clientFd, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0)
484 {
485 printf("err -3\n");
486 close(clientFd);
487 return -3;
488 }
489
490 sprintf(sendBuff,"%s","update");
491 if ((nwrite = write(clientFd, sendBuff, 100)) < 0)
492 {
493 printf("err -4\n");
494 close(clientFd);
495 return -4;
496 }
497
498 close(clientFd);
499 return 0;
500}
501
502int lynq_write_log_to_file_now()
503{
504 FILE *fp;
505 char command[256];
506 char buffer[256];
507 int pid = -1;
508 const char *process_name = "mbtk_logd";
509
510
511 snprintf(command, sizeof(command), "pgrep %s", process_name);
512 fp = popen(command, "r");
513 if (fp == NULL)
514 {
515 perror("error comman");
516 return -1;
517 }
518
519 if (fgets(buffer, sizeof(buffer), fp) != NULL)
520 {
521 pid = atoi(buffer);
522 }
523 pclose(fp);
524
525 if (pid != -1)
526 {
527 printf("mbtk %s PID: %d\n", process_name, pid);
528 if (kill(pid, SIGTERM) == -1)
529 {
530 perror("send SIGTERM signal failed");
531 return -1;
532 }
533 printf("send %d SIGTERM signal\n", pid);
534 }
535 else
536 {
537 printf("no find %s\n", process_name);
538 }
539
540 //lynq_log_configuration_init("log_end");
541 //lynq_log_global_output(LOG_DEBUG,"mbtk_logd_out");
542 return 0;
543
544}
545
546int lynq_stop_record_log(int value)
547{
548
549 json_object* jsonobj = NULL;
550 json_object* tmpjson = NULL;
551 json_object* datajson = NULL;
552 json_object* listjson = NULL;
553
554// int tmp_int;
555 const char* tmp_string = NULL;
556
557 if(value != 0 && value != 1)
558 {
559 printf("invalid value ");
560 return -1;
561 }
562
563 jsonobj = json_object_from_file(LOG_CONFIG_PATH);
564 if (NULL == jsonobj)
565 {
566 printf("Can't open config file: %s\n", LOG_CONFIG_PATH);
567 mbtk_system("echo Can't open config file > /dev/console");
568 return -1;
569 }
570
571
572 json_object_object_get_ex(jsonobj, "buffer_list", &tmpjson);
573
574 /***获取syslog json data***/
575 datajson = json_object_array_get_idx(tmpjson, SYSLOG_INDEX);
576 if (NULL == datajson)
577 {
578 json_object_to_file(LOG_CONFIG_PATH, jsonobj);
579 json_object_put(jsonobj);
580 printf("NULL == datajson\n");
581 mbtk_system("echo NULL == datajson > /dev/console");
582 return -1;
583 }
584
585 json_object_object_get_ex(datajson, "name", &listjson);
586 tmp_string = json_object_get_string(listjson);
587
588 json_object_object_get_ex(datajson, "enable", &listjson);
589 json_object_get_int(listjson);
590
591 if(strcmp(tmp_string, SYSLOG_NAME) != 0)
592 {
593 json_object_to_file(LOG_CONFIG_PATH, jsonobj);
594 json_object_put(jsonobj);
595 printf("SYSLOG_NAME error, \n");
596 mbtk_system("echo SYSLOG_NAME error, > /dev/console");
597 return -1;
598 }
599 else
600 {
601 //set 0 to syslog
602 json_object_object_add(datajson, "enable", json_object_new_int(value));
603 }
604
605 /***获取radiolog json data***/
606 datajson = json_object_array_get_idx(tmpjson, RADIO_INDEX);
607 if (NULL == datajson)
608 {
609 json_object_to_file(LOG_CONFIG_PATH, jsonobj);
610 json_object_put(jsonobj);
611 printf("NULL == datajson\n");
612 mbtk_system("echo NULL == datajson > /dev/console");
613 return -1;
614 }
615
616 json_object_object_get_ex(datajson, "name", &listjson);
617 tmp_string = json_object_get_string(listjson);
618
619 json_object_object_get_ex(datajson, "enable", &listjson);
620 json_object_get_int(listjson);
621
622 if(strcmp(tmp_string, RADIOLOG_NAME) != 0)
623 {
624 json_object_to_file(LOG_CONFIG_PATH, jsonobj);
625 json_object_put(jsonobj);
626 printf("RADIO_NAME error or \n");
627 mbtk_system("echo RADIO_NAME error, > /dev/console");
628 return -1;
629 }
630 else
631 {
632 //set 0 to radio_log
633 json_object_object_add(datajson, "enable", json_object_new_int(value));
634 }
635
636 /***释放json对象***/
637 json_object_to_file(LOG_CONFIG_PATH, jsonobj);
638 json_object_put(jsonobj);
639 return 0;
640}
641
642
643
644int lynq_syslog_control(CONTROL_TYPE value)
645{
646 FILE *fp = NULL;
647 const char *filePath = "/etc/syslog_close";
648 if(value !=SYSLOG_OPEN && value !=SYSLOG_CLOSE)
649 {
650 return -1;
651 }
652
653 if(value == SYSLOG_CLOSE)
654 {
655 fp = fopen(filePath, "w");
656 if (fp == NULL)
657 {
658 perror("Error creating file");
659 return -1;
660 }
661 fclose(fp);
662
663 }
664
665 if(value == SYSLOG_OPEN)
666 {
667 if(access(filePath,F_OK) == 0)
668 {
669 if(remove(filePath) == 0)
670 {
671 return 0;
672 }
673 else
674 {
675 perror("Error deleting file");
676 return -1;
677 }
678 }
679 else
680 {
681 return 0;
682 }
683
684 }
685
686 return 0;
687
688}
689
690int lynq_kernel_log_control(CONTROL_TYPE value)
691{
692 FILE *fp = NULL;
693 const char *filePath = "/etc/kernel_log_close";
694
695 if(value != KERNEL_LOG_OPEN && value != KERNEL_LOG_CLOSE)
696 {
697 return -1;
698 }
699
700 if(value == KERNEL_LOG_CLOSE)
701 {
702 fp = fopen(filePath, "w");
703 if (fp == NULL)
704 {
705 perror("Error creating file");
706 return -1;
707 }
708 fclose(fp);
709
710 }
711
712 if(value == KERNEL_LOG_OPEN)
713 {
714 if(access(filePath,F_OK) == 0)
715 {
716 if(remove(filePath) == 0)
717 {
718 return 0;
719 }
720 else
721 {
722 perror("Error deleting kernel_log_close file");
723 return -1;
724 }
725 }
726 else
727 {
728 return 0;
729 }
730
731 }
732
733 return 0;
734
735}
736