blob: 6e3885aff462faa810b6f1022b912a4d0390136a [file] [log] [blame]
xf.li44e08692024-01-30 01:54:44 -08001#include "json/json.h"
b.liu4e243dc2023-11-27 11:20:00 +08002#include "liblog.h"
3#include "mbtk_type.h"
b.liu5fa9e772023-11-23 18:00:55 +08004
xf.li44e08692024-01-30 01:54:44 -08005#define LOG_CONFIG_PATH "/etc/mbtk/mbtk_log.json"
6#define SYSLOG_PATCH "syslog"
7#define SYSLOG_NAME "syslog"
8#define SYSLOG_INDEX 0
b.liu5fa9e772023-11-23 18:00:55 +08009
10void lynq_log_configuration_init(const char *log_name)
11{
xf.li44e08692024-01-30 01:54:44 -080012 //UNUSED(log_name);
13 mbtk_log_init(SYSLOG_PATCH, log_name);
b.liu5fa9e772023-11-23 18:00:55 +080014}
15
16void lynq_log_global_output(log_level_enum Level,const char *format,...)
17{
18 UNUSED(Level);
19 UNUSED(format);
20
b.liu5fa9e772023-11-23 18:00:55 +080021}
22
23int lynq_syslog_set_file_size(int value)
24{
xf.li44e08692024-01-30 01:54:44 -080025// UNUSED(value);
26 json_object* jsonobj = NULL;
27 json_object* tmpjson = NULL;
28 json_object* datajson = NULL;
29 json_object* listjson = NULL;
b.liu5fa9e772023-11-23 18:00:55 +080030
xf.li44e08692024-01-30 01:54:44 -080031 int tmp_int;
32 char* tmp_string = NULL;
33
34 if(value < 1)
35 {
36 value = 1;
37 }
38 else if(value > 100)
39 {
40 value = 100;
41 }
xf.li43643772024-03-04 19:39:53 -080042 value = value*1024;
xf.li44e08692024-01-30 01:54:44 -080043
44 jsonobj = json_object_from_file(LOG_CONFIG_PATH);
45 if (NULL == jsonobj) {
46 printf("Can't open config file: %s\n", LOG_CONFIG_PATH);
47 system("echo Can't open config file > /dev/console");
48 return -1;
49 }
50 /***获取data***/
51 json_object_object_get_ex(jsonobj, "buffer_list", &tmpjson);
52 datajson = json_object_array_get_idx(tmpjson, SYSLOG_INDEX);
53 if (NULL == datajson) {
54 json_object_to_file(LOG_CONFIG_PATH, jsonobj);
55 json_object_put(jsonobj);
56 printf("NULL == datajson\n");
57 system("echo NULL == datajson > /dev/console");
58 return -1;
59 }
60
61 json_object_object_get_ex(datajson, "name", &listjson);
62 tmp_string = json_object_get_string(listjson);
63
64 json_object_object_get_ex(datajson, "enable", &listjson);
65 tmp_int = json_object_get_int(listjson);
66
67 if(strcmp(tmp_string, SYSLOG_NAME) != 0 || tmp_int != 1)
68 {
69 json_object_to_file(LOG_CONFIG_PATH, jsonobj);
70 json_object_put(jsonobj);
71 printf("SYSLOG_NAME error, tmp_int != 1\n");
72 system("echo SYSLOG_NAME error, tmp_int != 1 > /dev/console");
73 return -1;
74 }
75
76 json_object_object_add(datajson, "rotate_file_size", json_object_new_int(value));
77
78 /***释放json对象***/
79 json_object_to_file(LOG_CONFIG_PATH, jsonobj);
80 json_object_put(jsonobj);
b.liu5fa9e772023-11-23 18:00:55 +080081 return 0;
82}
83
84int lynq_syslog_get_file_size(void)
85{
xf.li44e08692024-01-30 01:54:44 -080086 json_object* jsonobj = NULL;
87 json_object* tmpjson = NULL;
88 json_object* datajson = NULL;
89 json_object* listjson = NULL;
b.liu5fa9e772023-11-23 18:00:55 +080090
xf.li44e08692024-01-30 01:54:44 -080091 int tmp_int;
92 char* tmp_string = NULL;
93
94 jsonobj = json_object_from_file(LOG_CONFIG_PATH);
95 if (NULL == jsonobj) {
96 printf("Can't open config file: %s\n", LOG_CONFIG_PATH);
97 return -1;
98 }
99 /***获取data***/
100 json_object_object_get_ex(jsonobj, "buffer_list", &tmpjson);
101 datajson = json_object_array_get_idx(tmpjson, SYSLOG_INDEX);
102 if (NULL == datajson) {
103 json_object_put(jsonobj);
104 return -1;
105 }
106
107 json_object_object_get_ex(datajson, "name", &listjson);
108 tmp_string = json_object_get_string(listjson);
109
110 json_object_object_get_ex(datajson, "enable", &listjson);
111 tmp_int = json_object_get_int(listjson);
112
113 if(strcmp(tmp_string, SYSLOG_NAME) != 0 || tmp_int != 1)
114 {
115 json_object_put(jsonobj);
116 return -1;
117 }
118
119 json_object_object_get_ex(datajson, "rotate_file_size", &listjson);
120 tmp_int = json_object_get_int(listjson);
121
122 /***释放json对象***/
123 json_object_put(jsonobj);
124
xf.li43643772024-03-04 19:39:53 -0800125 return tmp_int/1024;
126// return tmp_int;
b.liu5fa9e772023-11-23 18:00:55 +0800127}
128
129int lynq_syslog_set_file_rotate(int value)
130{
xf.li44e08692024-01-30 01:54:44 -0800131 //UNUSED(value);
b.liu5fa9e772023-11-23 18:00:55 +0800132
xf.li44e08692024-01-30 01:54:44 -0800133 json_object* jsonobj = NULL;
134 json_object* tmpjson = NULL;
135 json_object* datajson = NULL;
136 json_object* listjson = NULL;
137
138 int tmp_int;
139 char* tmp_string = NULL;
140
141 if(value < 0)
142 {
143 value = 0;
144 }
145 else if(value > 99)
146 {
147 value = 99;
148 }
149
150 jsonobj = json_object_from_file(LOG_CONFIG_PATH);
151 if (NULL == jsonobj) {
152 printf("Can't open config file: %s\n", LOG_CONFIG_PATH);
153 return -1;
154 }
155 /***获取data***/
156 json_object_object_get_ex(jsonobj, "buffer_list", &tmpjson);
157 datajson = json_object_array_get_idx(tmpjson, SYSLOG_INDEX);
158 if (NULL == datajson) {
159 json_object_to_file(LOG_CONFIG_PATH, jsonobj);
160 json_object_put(jsonobj);
161 return -1;
162 }
163
164 json_object_object_get_ex(datajson, "name", &listjson);
165 tmp_string = json_object_get_string(listjson);
166
167 json_object_object_get_ex(datajson, "enable", &listjson);
168 tmp_int = json_object_get_int(listjson);
169
170 if(strcmp(tmp_string, SYSLOG_NAME) != 0 || tmp_int != 1)
171 {
172 json_object_to_file(LOG_CONFIG_PATH, jsonobj);
173 json_object_put(jsonobj);
174 return -1;
175 }
176
177 json_object_object_add(datajson, "rotate_file_count", json_object_new_int(value));
178
179 /***释放json对象***/
180 json_object_to_file(LOG_CONFIG_PATH, jsonobj);
181 json_object_put(jsonobj);
b.liu5fa9e772023-11-23 18:00:55 +0800182 return 0;
183}
184
185int lynq_syslog_get_file_rotate(void)
186{
xf.li44e08692024-01-30 01:54:44 -0800187 json_object* jsonobj = NULL;
188 json_object* tmpjson = NULL;
189 json_object* datajson = NULL;
190 json_object* listjson = NULL;
b.liu5fa9e772023-11-23 18:00:55 +0800191
xf.li44e08692024-01-30 01:54:44 -0800192 int tmp_int;
193 char* tmp_string = NULL;
194
195 jsonobj = json_object_from_file(LOG_CONFIG_PATH);
196 if (NULL == jsonobj) {
197 printf("Can't open config file: %s\n", LOG_CONFIG_PATH);
198 return -1;
199 }
200 /***获取data***/
201 json_object_object_get_ex(jsonobj, "buffer_list", &tmpjson);
202 datajson = json_object_array_get_idx(tmpjson, SYSLOG_INDEX);
203 if (NULL == datajson) {
204 json_object_put(jsonobj);
205 return -1;
206 }
207
208 json_object_object_get_ex(datajson, "name", &listjson);
209 tmp_string = json_object_get_string(listjson);
210
211 json_object_object_get_ex(datajson, "enable", &listjson);
212 tmp_int = json_object_get_int(listjson);
213
214 if(strcmp(tmp_string, SYSLOG_NAME) != 0 || tmp_int != 1)
215 {
216 json_object_put(jsonobj);
217 return -1;
218 }
219
220 json_object_object_get_ex(datajson, "rotate_file_count", &listjson);
221 tmp_int = json_object_get_int(listjson);
222
223 /***释放json对象***/
224 json_object_put(jsonobj);
225
226 return tmp_int;
b.liu5fa9e772023-11-23 18:00:55 +0800227}
228
xf.li43643772024-03-04 19:39:53 -0800229log_level_enum filter_char_to_pri(char c)
230{
231 switch (c) {
232 case 'v':
233 return LOG_VERBOSE;
234 case 'd':
235 return LOG_DEBUG;
236 case 'i':
237 return LOG_INFO;
238 case 'w':
239 return LOG_WARNING;
240 case 'e':
241 return LOG_ERROR;
242 case '*':
243 default:
244 return LOG_LEVEL_MAX;
245 }
246}
247
248char filter_pri_to_char(log_level_enum level)
249{
250 char char_level;
251 switch (level) {
252 case LOG_VERBOSE:
253 char_level = 'v';
254 break;
255 case LOG_DEBUG:
256 char_level = 'd';
257 break;
258 case LOG_INFO:
259 char_level = 'i';
260 break;
261 case LOG_WARNING:
262 char_level = 'w';
263 break;
264 case LOG_ERROR:
265 char_level = 'e';
266 break;
267 case LOG_LEVEL_MAX:
268 default:
269 char_level = '*';
270 break;
271 }
272 return char_level;
273}
274
b.liu5fa9e772023-11-23 18:00:55 +0800275int lynq_set_log_level(const char * module_name, log_level_enum level)
276{
xf.li43643772024-03-04 19:39:53 -0800277 json_object* jsonobj = NULL;
278 json_object* tmpjson = NULL;
279 json_object* datajson = NULL;
280 json_object* listjson = NULL;
281 json_object* fileterjson = NULL;
282 json_object* fileter_listjson = NULL;
283 json_object* new_fileter = NULL;
b.liu5fa9e772023-11-23 18:00:55 +0800284
xf.li43643772024-03-04 19:39:53 -0800285 int n = 0, array_length = 0;
286 char* tmp_string = NULL;
287 char level_string[5] = {'\0'};
288
289 jsonobj = json_object_from_file(LOG_CONFIG_PATH);
290 if (NULL == jsonobj) {
291 printf("Can't open config file: %s\n", LOG_CONFIG_PATH);
292 return -1;
293 }
294 /***获取data***/
295 json_object_object_get_ex(jsonobj, "buffer_list", &tmpjson);
296 datajson = json_object_array_get_idx(tmpjson, SYSLOG_INDEX);
297 if (NULL == datajson) {
298 json_object_put(jsonobj);
299 return -1;
300 }
301
302 json_object_object_get_ex(datajson, "filter_list", &listjson);
303 if (NULL == listjson) {
304 printf("%s %d: object failure!\n", __FUNCTION__, __LINE__);
305 json_object_put(listjson);
306 return -1;
307 }
308 array_length = json_object_array_length(listjson);
309 for (n = 0; n <= array_length; n++) {
310 fileterjson = json_object_array_get_idx(listjson, n);
311 if (NULL == fileterjson) {
312 new_fileter = json_object_new_object();
313 sprintf(level_string, "%c", filter_pri_to_char(level));
314 json_object_object_add(new_fileter, "priority", json_object_new_string(level_string));
315 json_object_object_add(new_fileter, "tag", json_object_new_string(module_name));
316 json_object_array_add(listjson, new_fileter);
317 break;
318 }
319
320 json_object_object_get_ex(fileterjson, "tag", &fileter_listjson);
321 char *str = json_object_get_string(fileter_listjson);
322 if (str) {
323 tmp_string = strdup(str);
324 if(strcmp(module_name, tmp_string) == 0)
325 {
326 sprintf(level_string, "%c", filter_pri_to_char(level));
327 json_object_object_add(fileterjson, "priority", json_object_new_string(level_string));
328 json_object_to_file(LOG_CONFIG_PATH, jsonobj);
329 json_object_put(jsonobj);
330 return 0;
331 }
332 }
333 else
334 {
335 continue;
336 }
337 }
338 /***释放json对象***/
339 json_object_to_file(LOG_CONFIG_PATH, jsonobj);
340 json_object_put(jsonobj);
b.liu5fa9e772023-11-23 18:00:55 +0800341
342 return 0;
343}
344
345int lynq_get_log_level(const char * module_name, log_level_enum *level)
346{
xf.li43643772024-03-04 19:39:53 -0800347 json_object* jsonobj = NULL;
348 json_object* tmpjson = NULL;
349 json_object* datajson = NULL;
350 json_object* listjson = NULL;
351 json_object* fileterjson = NULL;
352 json_object* fileter_listjson = NULL;
b.liu5fa9e772023-11-23 18:00:55 +0800353
xf.li43643772024-03-04 19:39:53 -0800354 int n;
355 char* tmp_string = NULL;
356
357 jsonobj = json_object_from_file(LOG_CONFIG_PATH);
358 if (NULL == jsonobj) {
359 printf("Can't open config file: %s\n", LOG_CONFIG_PATH);
360 return -1;
361 }
362 /***获取data***/
363 json_object_object_get_ex(jsonobj, "buffer_list", &tmpjson);
364 datajson = json_object_array_get_idx(tmpjson, SYSLOG_INDEX);
365 if (NULL == datajson) {
366 json_object_put(jsonobj);
367 return -1;
368 }
369
370 json_object_object_get_ex(datajson, "filter_list", &listjson);
371 if (NULL == listjson) {
372 printf("%s %d: object failure!\n", __FUNCTION__, __LINE__);
373 json_object_put(listjson);
374 return -1;
375 }
376
377 for (n = 0 ; n < 5; n++) {
378 fileterjson = json_object_array_get_idx(listjson, n);
379 if (NULL == fileterjson) {
380 printf("the fileterjson exit\n");
381 break;
382 }
383
384 json_object_object_get_ex(fileterjson, "tag", &fileter_listjson);
385 char *str = json_object_get_string(fileter_listjson);
386 if (str) {
387 tmp_string = strdup(str);
388 printf("tag is %s\n", tmp_string);
389 if(strcmp(module_name, tmp_string) == 0)
390 {
391 json_object_object_get_ex(fileterjson, "priority", &fileter_listjson);
392 str = json_object_get_string(fileter_listjson);
393 if (str) {
394 tmp_string = str[0];
395 printf("fileter_listjson: %c\n", tmp_string);
396 *level = filter_char_to_pri(tmp_string);
397 //get the log level
398 json_object_put(jsonobj);
399 return 0;
400 }
401 else
402 {
403 break;
404 }
405 }
406 }
407 else
408 {
409 continue;
410 }
411 }
412 *level = LOG_UNSET;
413 /***释放json对象***/
414 json_object_put(jsonobj);
b.liu5fa9e772023-11-23 18:00:55 +0800415
416 return 0;
417}
418
419int lynq_set_special_log_level(const char * exe_name, const char * module_name, log_level_enum level)
420{
421 UNUSED(exe_name);
422 UNUSED(module_name);
423 UNUSED(level);
424
425
426 return 0;
427}
428
429int lynq_get_special_log_level(const char * exe_name, const char * module_name, log_level_enum *level)
430{
431 UNUSED(exe_name);
432 UNUSED(module_name);
433 UNUSED(level);
434
435
436 return 0;
437}
438
439int lynq_notify_recalc_log_level(pid_t pid)
440{
441 UNUSED(pid);
442
443 return 0;
xf.li44e08692024-01-30 01:54:44 -0800444}