blob: 65ffc311f0b5e2eb72b6e45afcaa6511fa399245 [file] [log] [blame]
b.liuf37bd332024-03-18 13:51:24 +08001#include <stdarg.h>
xf.li44e08692024-01-30 01:54:44 -08002#include "json/json.h"
b.liuf37bd332024-03-18 13:51:24 +08003#include "lynq_deflog.h"
b.liu4e243dc2023-11-27 11:20:00 +08004#include "mbtk_type.h"
b.liu5fa9e772023-11-23 18:00:55 +08005
xf.li44e08692024-01-30 01:54:44 -08006#define LOG_CONFIG_PATH "/etc/mbtk/mbtk_log.json"
7#define SYSLOG_PATCH "syslog"
8#define SYSLOG_NAME "syslog"
9#define SYSLOG_INDEX 0
b.liu5fa9e772023-11-23 18:00:55 +080010
11void lynq_log_configuration_init(const char *log_name)
12{
xf.li44e08692024-01-30 01:54:44 -080013 //UNUSED(log_name);
14 mbtk_log_init(SYSLOG_PATCH, log_name);
b.liu5fa9e772023-11-23 18:00:55 +080015}
16
17void lynq_log_global_output(log_level_enum Level,const char *format,...)
18{
b.liuf37bd332024-03-18 13:51:24 +080019 va_list args;
20 va_start(args,format);
21 mbtk_log(Level, format, args);
22 va_end(args);
23}
b.liu5fa9e772023-11-23 18:00:55 +080024
b.liuf37bd332024-03-18 13:51:24 +080025const char* lynq_read_log_version()
26{
27 return "LOG-V1.0";
b.liu5fa9e772023-11-23 18:00:55 +080028}
29
30int lynq_syslog_set_file_size(int value)
31{
xf.li44e08692024-01-30 01:54:44 -080032// UNUSED(value);
33 json_object* jsonobj = NULL;
34 json_object* tmpjson = NULL;
35 json_object* datajson = NULL;
36 json_object* listjson = NULL;
b.liu5fa9e772023-11-23 18:00:55 +080037
xf.li44e08692024-01-30 01:54:44 -080038 int tmp_int;
39 char* tmp_string = NULL;
40
41 if(value < 1)
42 {
43 value = 1;
44 }
45 else if(value > 100)
46 {
47 value = 100;
48 }
xf.li43643772024-03-04 19:39:53 -080049 value = value*1024;
xf.li44e08692024-01-30 01:54:44 -080050
51 jsonobj = json_object_from_file(LOG_CONFIG_PATH);
52 if (NULL == jsonobj) {
53 printf("Can't open config file: %s\n", LOG_CONFIG_PATH);
54 system("echo Can't open config file > /dev/console");
55 return -1;
56 }
57 /***获取data***/
58 json_object_object_get_ex(jsonobj, "buffer_list", &tmpjson);
59 datajson = json_object_array_get_idx(tmpjson, SYSLOG_INDEX);
60 if (NULL == datajson) {
61 json_object_to_file(LOG_CONFIG_PATH, jsonobj);
62 json_object_put(jsonobj);
63 printf("NULL == datajson\n");
64 system("echo NULL == datajson > /dev/console");
65 return -1;
66 }
67
68 json_object_object_get_ex(datajson, "name", &listjson);
69 tmp_string = json_object_get_string(listjson);
70
71 json_object_object_get_ex(datajson, "enable", &listjson);
72 tmp_int = json_object_get_int(listjson);
73
74 if(strcmp(tmp_string, SYSLOG_NAME) != 0 || tmp_int != 1)
75 {
76 json_object_to_file(LOG_CONFIG_PATH, jsonobj);
77 json_object_put(jsonobj);
78 printf("SYSLOG_NAME error, tmp_int != 1\n");
79 system("echo SYSLOG_NAME error, tmp_int != 1 > /dev/console");
80 return -1;
81 }
82
83 json_object_object_add(datajson, "rotate_file_size", json_object_new_int(value));
84
85 /***释放json对象***/
86 json_object_to_file(LOG_CONFIG_PATH, jsonobj);
87 json_object_put(jsonobj);
b.liu5fa9e772023-11-23 18:00:55 +080088 return 0;
89}
90
91int lynq_syslog_get_file_size(void)
92{
xf.li44e08692024-01-30 01:54:44 -080093 json_object* jsonobj = NULL;
94 json_object* tmpjson = NULL;
95 json_object* datajson = NULL;
96 json_object* listjson = NULL;
b.liu5fa9e772023-11-23 18:00:55 +080097
xf.li44e08692024-01-30 01:54:44 -080098 int tmp_int;
99 char* tmp_string = NULL;
100
101 jsonobj = json_object_from_file(LOG_CONFIG_PATH);
102 if (NULL == jsonobj) {
103 printf("Can't open config file: %s\n", LOG_CONFIG_PATH);
104 return -1;
105 }
106 /***获取data***/
107 json_object_object_get_ex(jsonobj, "buffer_list", &tmpjson);
108 datajson = json_object_array_get_idx(tmpjson, SYSLOG_INDEX);
109 if (NULL == datajson) {
110 json_object_put(jsonobj);
111 return -1;
112 }
113
114 json_object_object_get_ex(datajson, "name", &listjson);
115 tmp_string = json_object_get_string(listjson);
116
117 json_object_object_get_ex(datajson, "enable", &listjson);
118 tmp_int = json_object_get_int(listjson);
119
120 if(strcmp(tmp_string, SYSLOG_NAME) != 0 || tmp_int != 1)
121 {
122 json_object_put(jsonobj);
123 return -1;
124 }
125
126 json_object_object_get_ex(datajson, "rotate_file_size", &listjson);
127 tmp_int = json_object_get_int(listjson);
128
129 /***释放json对象***/
130 json_object_put(jsonobj);
131
xf.li43643772024-03-04 19:39:53 -0800132 return tmp_int/1024;
133// return tmp_int;
b.liu5fa9e772023-11-23 18:00:55 +0800134}
135
136int lynq_syslog_set_file_rotate(int value)
137{
xf.li44e08692024-01-30 01:54:44 -0800138 //UNUSED(value);
b.liu5fa9e772023-11-23 18:00:55 +0800139
xf.li44e08692024-01-30 01:54:44 -0800140 json_object* jsonobj = NULL;
141 json_object* tmpjson = NULL;
142 json_object* datajson = NULL;
143 json_object* listjson = NULL;
144
145 int tmp_int;
146 char* tmp_string = NULL;
147
148 if(value < 0)
149 {
150 value = 0;
151 }
152 else if(value > 99)
153 {
154 value = 99;
155 }
156
157 jsonobj = json_object_from_file(LOG_CONFIG_PATH);
158 if (NULL == jsonobj) {
159 printf("Can't open config file: %s\n", LOG_CONFIG_PATH);
160 return -1;
161 }
162 /***获取data***/
163 json_object_object_get_ex(jsonobj, "buffer_list", &tmpjson);
164 datajson = json_object_array_get_idx(tmpjson, SYSLOG_INDEX);
165 if (NULL == datajson) {
166 json_object_to_file(LOG_CONFIG_PATH, jsonobj);
167 json_object_put(jsonobj);
168 return -1;
169 }
170
171 json_object_object_get_ex(datajson, "name", &listjson);
172 tmp_string = json_object_get_string(listjson);
173
174 json_object_object_get_ex(datajson, "enable", &listjson);
175 tmp_int = json_object_get_int(listjson);
176
177 if(strcmp(tmp_string, SYSLOG_NAME) != 0 || tmp_int != 1)
178 {
179 json_object_to_file(LOG_CONFIG_PATH, jsonobj);
180 json_object_put(jsonobj);
181 return -1;
182 }
183
184 json_object_object_add(datajson, "rotate_file_count", json_object_new_int(value));
185
186 /***释放json对象***/
187 json_object_to_file(LOG_CONFIG_PATH, jsonobj);
188 json_object_put(jsonobj);
b.liu5fa9e772023-11-23 18:00:55 +0800189 return 0;
190}
191
192int lynq_syslog_get_file_rotate(void)
193{
xf.li44e08692024-01-30 01:54:44 -0800194 json_object* jsonobj = NULL;
195 json_object* tmpjson = NULL;
196 json_object* datajson = NULL;
197 json_object* listjson = NULL;
b.liu5fa9e772023-11-23 18:00:55 +0800198
xf.li44e08692024-01-30 01:54:44 -0800199 int tmp_int;
200 char* tmp_string = NULL;
201
202 jsonobj = json_object_from_file(LOG_CONFIG_PATH);
203 if (NULL == jsonobj) {
204 printf("Can't open config file: %s\n", LOG_CONFIG_PATH);
205 return -1;
206 }
207 /***获取data***/
208 json_object_object_get_ex(jsonobj, "buffer_list", &tmpjson);
209 datajson = json_object_array_get_idx(tmpjson, SYSLOG_INDEX);
210 if (NULL == datajson) {
211 json_object_put(jsonobj);
212 return -1;
213 }
214
215 json_object_object_get_ex(datajson, "name", &listjson);
216 tmp_string = json_object_get_string(listjson);
217
218 json_object_object_get_ex(datajson, "enable", &listjson);
219 tmp_int = json_object_get_int(listjson);
220
221 if(strcmp(tmp_string, SYSLOG_NAME) != 0 || tmp_int != 1)
222 {
223 json_object_put(jsonobj);
224 return -1;
225 }
226
227 json_object_object_get_ex(datajson, "rotate_file_count", &listjson);
228 tmp_int = json_object_get_int(listjson);
229
230 /***释放json对象***/
231 json_object_put(jsonobj);
232
233 return tmp_int;
b.liu5fa9e772023-11-23 18:00:55 +0800234}
235
xf.li43643772024-03-04 19:39:53 -0800236log_level_enum filter_char_to_pri(char c)
237{
238 switch (c) {
239 case 'v':
240 return LOG_VERBOSE;
241 case 'd':
242 return LOG_DEBUG;
243 case 'i':
244 return LOG_INFO;
245 case 'w':
246 return LOG_WARNING;
247 case 'e':
248 return LOG_ERROR;
249 case '*':
250 default:
251 return LOG_LEVEL_MAX;
252 }
253}
254
255char filter_pri_to_char(log_level_enum level)
256{
257 char char_level;
258 switch (level) {
259 case LOG_VERBOSE:
260 char_level = 'v';
261 break;
262 case LOG_DEBUG:
263 char_level = 'd';
264 break;
265 case LOG_INFO:
266 char_level = 'i';
267 break;
268 case LOG_WARNING:
269 char_level = 'w';
270 break;
271 case LOG_ERROR:
272 char_level = 'e';
273 break;
274 case LOG_LEVEL_MAX:
275 default:
276 char_level = '*';
277 break;
278 }
279 return char_level;
280}
281
b.liu5fa9e772023-11-23 18:00:55 +0800282int lynq_set_log_level(const char * module_name, log_level_enum level)
283{
xf.li43643772024-03-04 19:39:53 -0800284 json_object* jsonobj = NULL;
285 json_object* tmpjson = NULL;
286 json_object* datajson = NULL;
287 json_object* listjson = NULL;
288 json_object* fileterjson = NULL;
289 json_object* fileter_listjson = NULL;
290 json_object* new_fileter = NULL;
b.liu5fa9e772023-11-23 18:00:55 +0800291
xf.li43643772024-03-04 19:39:53 -0800292 int n = 0, array_length = 0;
293 char* tmp_string = NULL;
294 char level_string[5] = {'\0'};
295
296 jsonobj = json_object_from_file(LOG_CONFIG_PATH);
297 if (NULL == jsonobj) {
298 printf("Can't open config file: %s\n", LOG_CONFIG_PATH);
299 return -1;
300 }
301 /***获取data***/
302 json_object_object_get_ex(jsonobj, "buffer_list", &tmpjson);
303 datajson = json_object_array_get_idx(tmpjson, SYSLOG_INDEX);
304 if (NULL == datajson) {
305 json_object_put(jsonobj);
306 return -1;
307 }
308
309 json_object_object_get_ex(datajson, "filter_list", &listjson);
310 if (NULL == listjson) {
311 printf("%s %d: object failure!\n", __FUNCTION__, __LINE__);
312 json_object_put(listjson);
313 return -1;
314 }
315 array_length = json_object_array_length(listjson);
316 for (n = 0; n <= array_length; n++) {
317 fileterjson = json_object_array_get_idx(listjson, n);
318 if (NULL == fileterjson) {
319 new_fileter = json_object_new_object();
320 sprintf(level_string, "%c", filter_pri_to_char(level));
321 json_object_object_add(new_fileter, "priority", json_object_new_string(level_string));
322 json_object_object_add(new_fileter, "tag", json_object_new_string(module_name));
323 json_object_array_add(listjson, new_fileter);
324 break;
325 }
326
327 json_object_object_get_ex(fileterjson, "tag", &fileter_listjson);
328 char *str = json_object_get_string(fileter_listjson);
329 if (str) {
330 tmp_string = strdup(str);
331 if(strcmp(module_name, tmp_string) == 0)
332 {
333 sprintf(level_string, "%c", filter_pri_to_char(level));
334 json_object_object_add(fileterjson, "priority", json_object_new_string(level_string));
335 json_object_to_file(LOG_CONFIG_PATH, jsonobj);
336 json_object_put(jsonobj);
337 return 0;
338 }
339 }
340 else
341 {
342 continue;
343 }
344 }
345 /***释放json对象***/
346 json_object_to_file(LOG_CONFIG_PATH, jsonobj);
347 json_object_put(jsonobj);
b.liu5fa9e772023-11-23 18:00:55 +0800348
349 return 0;
350}
351
352int lynq_get_log_level(const char * module_name, log_level_enum *level)
353{
xf.li43643772024-03-04 19:39:53 -0800354 json_object* jsonobj = NULL;
355 json_object* tmpjson = NULL;
356 json_object* datajson = NULL;
357 json_object* listjson = NULL;
358 json_object* fileterjson = NULL;
359 json_object* fileter_listjson = NULL;
b.liu5fa9e772023-11-23 18:00:55 +0800360
xf.li43643772024-03-04 19:39:53 -0800361 int n;
362 char* tmp_string = NULL;
363
364 jsonobj = json_object_from_file(LOG_CONFIG_PATH);
365 if (NULL == jsonobj) {
366 printf("Can't open config file: %s\n", LOG_CONFIG_PATH);
367 return -1;
368 }
369 /***获取data***/
370 json_object_object_get_ex(jsonobj, "buffer_list", &tmpjson);
371 datajson = json_object_array_get_idx(tmpjson, SYSLOG_INDEX);
372 if (NULL == datajson) {
373 json_object_put(jsonobj);
374 return -1;
375 }
376
377 json_object_object_get_ex(datajson, "filter_list", &listjson);
378 if (NULL == listjson) {
379 printf("%s %d: object failure!\n", __FUNCTION__, __LINE__);
380 json_object_put(listjson);
381 return -1;
382 }
383
384 for (n = 0 ; n < 5; n++) {
385 fileterjson = json_object_array_get_idx(listjson, n);
386 if (NULL == fileterjson) {
387 printf("the fileterjson exit\n");
388 break;
389 }
390
391 json_object_object_get_ex(fileterjson, "tag", &fileter_listjson);
392 char *str = json_object_get_string(fileter_listjson);
393 if (str) {
394 tmp_string = strdup(str);
395 printf("tag is %s\n", tmp_string);
396 if(strcmp(module_name, tmp_string) == 0)
397 {
398 json_object_object_get_ex(fileterjson, "priority", &fileter_listjson);
399 str = json_object_get_string(fileter_listjson);
400 if (str) {
401 tmp_string = str[0];
402 printf("fileter_listjson: %c\n", tmp_string);
403 *level = filter_char_to_pri(tmp_string);
404 //get the log level
405 json_object_put(jsonobj);
406 return 0;
407 }
408 else
409 {
410 break;
411 }
412 }
413 }
414 else
415 {
416 continue;
417 }
418 }
419 *level = LOG_UNSET;
420 /***释放json对象***/
421 json_object_put(jsonobj);
b.liu5fa9e772023-11-23 18:00:55 +0800422
423 return 0;
424}
425
426int lynq_set_special_log_level(const char * exe_name, const char * module_name, log_level_enum level)
427{
428 UNUSED(exe_name);
429 UNUSED(module_name);
430 UNUSED(level);
431
432
433 return 0;
434}
435
436int lynq_get_special_log_level(const char * exe_name, const char * module_name, log_level_enum *level)
437{
438 UNUSED(exe_name);
439 UNUSED(module_name);
440 UNUSED(level);
441
442
443 return 0;
444}
445
446int lynq_notify_recalc_log_level(pid_t pid)
447{
448 UNUSED(pid);
449
450 return 0;
xf.li44e08692024-01-30 01:54:44 -0800451}