rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | #include "liblog.h" |
| 2 | #include <stdarg.h> |
| 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <unistd.h> |
| 6 | #include <string.h> |
| 7 | #include <log/log.h> |
| 8 | |
| 9 | |
| 10 | void hextostring(unsigned char val, char *buf); |
| 11 | |
| 12 | #define LOG_NAME_LEN 128 |
| 13 | |
| 14 | static unsigned int log_level = 0; //logµÈ¼¶£¬ÐèÒª¿ª·¢ÈËÔ±¶¯Ì¬ÅäÖÃlogµÈ¼¶£¬Ä¬Èϵȼ¶Îª0 |
| 15 | static unsigned char log_name_arr[LOG_NAME_LEN] = {0}; |
| 16 | /********************************************************************************************************* |
| 17 | ** Function name: lynq_log_output |
| 18 | ** Description lynq_log_output |
| 19 | ** Input parameters: Tag:TagÀàÐÍ(log_level_enum) |
| 20 | Level:TagÏ´òÓ¡level(log_tag_enum) |
| 21 | format:printf´òÓ¡µÄ²»¶¨³¤²ÎÊý |
| 22 | ** Returned value: None |
| 23 | ** Created by: |
| 24 | *********************************************************************************************************/ |
| 25 | void lynq_log_output(log_tag_enum Tag,log_level_enum Level,const char *format,...) |
| 26 | { |
| 27 | #if LOG_ENABLE |
| 28 | if((Tag >= LOG_TAG_MAX)||(Level >= LOG_LEVEL_MAX)) |
| 29 | return; |
| 30 | // printf("mo:%d---le:%d---Level:%d\n",LogTagEnableInfoTable[Tag],log_level,Level); |
| 31 | // printf("rmodule:%d---rlevel:%d\n",((LogTagEnableInfoTable[Tag] >> Level)&0x00000001),((log_level >> Level)&0x00000001)); |
| 32 | if(((LogTagEnableInfoTable[Tag] >> Level)&0x00000001)&((log_level >> Level)&0x00000001)) |
| 33 | { |
| 34 | va_list args; |
| 35 | |
| 36 | //TagName |
| 37 | printf("[%s]",LogTagNameInfoTable[Tag]); |
| 38 | |
| 39 | //LevelName |
| 40 | printf("[%s]:",LogLevelNameInfoTable[Level]); |
| 41 | |
| 42 | va_start(args,format); |
| 43 | vprintf(format,args); |
| 44 | va_end(args); |
| 45 | } |
| 46 | return ; |
| 47 | #endif //LOG_ENABLE |
| 48 | } |
| 49 | |
| 50 | |
| 51 | void lynq_log_global_output(log_level_enum Level,const char *format,...) |
| 52 | { |
| 53 | #if LOG_ENABLE |
| 54 | |
| 55 | char out_buf[1100] = {0}; |
| 56 | char buf[1024] = {0}; |
| 57 | |
| 58 | if(Level >= LOG_LEVEL_MAX) |
| 59 | return; |
| 60 | if((log_level >> Level)&0x00000001) |
| 61 | { |
| 62 | va_list args; |
| 63 | |
| 64 | //TagName |
| 65 | if(log_name_arr[0] > 0) |
| 66 | { |
| 67 | printf("[%s]",log_name_arr); |
| 68 | } |
| 69 | |
| 70 | //LevelName |
| 71 | printf("%s:",LogLevelNameInfoTable[Level]); //lt dele @2021.7.22 for [] |
| 72 | |
| 73 | va_start(args,format); |
| 74 | vprintf(format,args); |
| 75 | vsnprintf(buf, sizeof(buf), format, args); //lt add @2021.7.22 for write buf |
| 76 | va_end(args); |
| 77 | printf("\r\n"); |
| 78 | |
| 79 | sprintf(out_buf, "%s %s", LogLevelNameInfoTable[Level], buf);//lt add @2021.7.22 for write outbuf |
| 80 | __android_log_print(ANDROID_LOG_DEBUG,log_name_arr, "%s",out_buf); //lt add @2021.7.22 for write syslog.log |
| 81 | } |
| 82 | return ; |
| 83 | #endif //LOG_ENABLE |
| 84 | } |
| 85 | |
| 86 | |
| 87 | |
| 88 | void lynq_log_configuration_set(char *log_name,char log_data_arr) |
| 89 | { |
| 90 | char log_data_buf[LOG_NAME_LEN] = {"persist."}; |
| 91 | char log_data_str[32] = {0}; |
| 92 | if(log_name == NULL) |
| 93 | { |
| 94 | return ; |
| 95 | } |
| 96 | if(log_data_arr < LOG_LEVEL_MAX) |
| 97 | { |
| 98 | sprintf(log_data_str,"%d",log_data_arr); |
| 99 | // printf("log_data_str:%s\n",log_data_str); |
| 100 | if((strlen(log_name)) < (LOG_NAME_LEN - 9)) |
| 101 | { |
| 102 | strcat(log_data_buf,log_name); |
| 103 | property_set(log_data_buf,log_data_str); |
| 104 | } |
| 105 | } |
| 106 | lynq_log_configuration_init(log_name); |
| 107 | } |
| 108 | |
| 109 | void lynq_deal_with_level(unsigned int get_log_level) |
| 110 | { |
| 111 | switch(get_log_level) |
| 112 | { |
| 113 | case LOG_DEBUG: |
| 114 | log_level |= ENABLE(LOG_DEBUG); |
| 115 | case LOG_INFO: |
| 116 | log_level |= ENABLE(LOG_INFO); |
| 117 | case LOG_WARNING: |
| 118 | log_level |= ENABLE(LOG_WARNING); |
| 119 | case LOG_ERROR: |
| 120 | log_level |= ENABLE(LOG_ERROR); |
| 121 | case LOG_VERBOSE: |
| 122 | log_level |= ENABLE(LOG_VERBOSE); |
| 123 | break; |
| 124 | default: |
| 125 | log_level |= ENABLE(LOG_VERBOSE); |
| 126 | break; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | void lynq_log_configuration_init(char *log_name) |
| 131 | { |
| 132 | char log_data_buf[LOG_NAME_LEN] = {"persist."}; |
| 133 | char get_propty_log_data[32] ={0}; |
| 134 | unsigned int get_log_level = 0; |
| 135 | |
| 136 | if(log_name == NULL) |
| 137 | { |
| 138 | return ; |
| 139 | } |
| 140 | if((strlen(log_name)) < (LOG_NAME_LEN - 9)) |
| 141 | { |
| 142 | strcpy(log_name_arr,log_name); |
| 143 | strcat(log_data_buf,log_name); |
| 144 | if(property_get(log_data_buf,(char *)get_propty_log_data,NULL) > 0) |
| 145 | { |
| 146 | get_log_level = atoi(get_propty_log_data); |
| 147 | // printf("get_log_level:%d\n",get_log_level); |
| 148 | } |
| 149 | else |
| 150 | { |
| 151 | log_level = 0; |
| 152 | // printf("get propty data fail\n"); |
| 153 | } |
| 154 | |
| 155 | } |
| 156 | lynq_deal_with_level(get_log_level); |
| 157 | |
| 158 | } |
| 159 | |
| 160 | /********************************************************************************************************* |
| 161 | ** Function name: lynq_update_log_output |
| 162 | ** Description ÖØÐ»ñÈ¡logµÈ¼¶²¢Ê¹ÆäÉúЧ |
| 163 | ** Input parameters: Tag:TagÀàÐÍ(log_level_enum) |
| 164 | Level:TagÏ´òÓ¡level(log_tag_enum) |
| 165 | format:printf´òÓ¡µÄ²»¶¨³¤²ÎÊý |
| 166 | ** Returned value: None |
| 167 | ** Created by: |
| 168 | *********************************************************************************************************/ |
| 169 | void lynq_update_log_output(log_tag_enum Tag,log_level_enum Level,const char *format,...) |
| 170 | { |
| 171 | #if LOG_ENABLE |
| 172 | lynq_enable_log_serve(); |
| 173 | if((Tag >= LOG_TAG_MAX)||(Level >= LOG_LEVEL_MAX)) |
| 174 | return; |
| 175 | // printf("mo:%d---le:%d---Level:%d\n",LogTagEnableInfoTable[Tag],log_level,Level); |
| 176 | // printf("rmodule:%d---rlevel:%d\n",((LogTagEnableInfoTable[Tag] >> Level)&0x00000001),((log_level >> Level)&0x00000001)); |
| 177 | if(((LogTagEnableInfoTable[Tag] >> Level)&0x00000001)&((log_level >> Level)&0x00000001)) |
| 178 | { |
| 179 | va_list args; |
| 180 | |
| 181 | //TagName |
| 182 | printf("[%s]",LogTagNameInfoTable[Tag]); |
| 183 | |
| 184 | //LevelName |
| 185 | printf("[%s]:",LogLevelNameInfoTable[Level]); |
| 186 | |
| 187 | va_start(args,format); |
| 188 | vprintf(format,args); |
| 189 | va_end(args); |
| 190 | } |
| 191 | return ; |
| 192 | #endif //LOG_ENABLE |
| 193 | } |
| 194 | |
| 195 | |
| 196 | /********************************************************************************************************* |
| 197 | ** Function name: lynq_log_printf_output |
| 198 | ** Description ·â×°printf |
| 199 | ** Input parameters: Tag:TagÀàÐÍ(log_level_enum) |
| 200 | Level:TagÏ´òÓ¡level(log_tag_enum) |
| 201 | format:printf´òÓ¡µÄ²»¶¨³¤²ÎÊý |
| 202 | ** Returned value: None |
| 203 | ** Created by: |
| 204 | *********************************************************************************************************/ |
| 205 | void lynq_log_printf_output(log_tag_enum Tag,log_level_enum Level,const char *format,...) |
| 206 | { |
| 207 | #if LOG_ENABLE |
| 208 | if((Tag >= LOG_TAG_MAX)||(Level >= LOG_LEVEL_MAX)) |
| 209 | return; |
| 210 | |
| 211 | va_list args; |
| 212 | |
| 213 | //TagName |
| 214 | printf("[%s]",LogTagNameInfoTable[Tag]); |
| 215 | |
| 216 | //LevelName |
| 217 | printf("[%s]:",LogLevelNameInfoTable[Level]); |
| 218 | |
| 219 | va_start(args,format); |
| 220 | vprintf(format,args); |
| 221 | va_end(args); |
| 222 | |
| 223 | return ; |
| 224 | #endif //LOG_ENABLE |
| 225 | } |
| 226 | |
| 227 | /********************************************************************************************************* |
| 228 | ** Function name: lynq_fget_log_level |
| 229 | ** Description ¿ªÆôlogÈÕÖ¾µÈ¼¶ |
| 230 | ** Input parameters: open_log_level:¿ªÆôlogÈÕÖ¾µÈ¼¶ |
| 231 | open_module_log_num:¿ªÆôÈÕ־ģ¿éµÄ¸öÊý |
| 232 | open_module_log:¿ªÆôÈÕ×Ó·þÎñµÄÄ£¿é |
| 233 | ** Returned value: None |
| 234 | ** Created by: |
| 235 | *********************************************************************************************************/ |
| 236 | void lynq_fget_log_level(log_level_str level_str) |
| 237 | { |
| 238 | log_level = 0; |
| 239 | switch(level_str.open_log_level) |
| 240 | { |
| 241 | case LOG_DEBUG: |
| 242 | log_level |= ENABLE(LOG_DEBUG); |
| 243 | case LOG_INFO: |
| 244 | log_level |= ENABLE(LOG_INFO); |
| 245 | case LOG_WARNING: |
| 246 | log_level |= ENABLE(LOG_WARNING); |
| 247 | case LOG_ERROR: |
| 248 | log_level |= ENABLE(LOG_ERROR); |
| 249 | case LOG_VERBOSE: |
| 250 | log_level |= ENABLE(LOG_VERBOSE); |
| 251 | break; |
| 252 | default: |
| 253 | log_level |= ENABLE(LOG_VERBOSE); |
| 254 | break; |
| 255 | } |
| 256 | |
| 257 | for(int i = 0; i < level_str.open_module_log_num; i++) |
| 258 | { |
| 259 | LogTagEnableInfoTable[level_str.open_module_log[i]] = ENABLE(LOG_VERBOSE)|ENABLE(LOG_DEBUG)|ENABLE(LOG_INFO)|ENABLE(LOG_WARNING)|ENABLE(LOG_ERROR); |
| 260 | } |
| 261 | return ; |
| 262 | } |
| 263 | |
| 264 | /********************************************************************************************************* |
| 265 | ** Function name: lynq_store_log_deploy |
| 266 | ** Description ±£´æÈÕÖ¾ÅäÖÃÐÅÏ¢ |
| 267 | ** Input parameters: open_log_level:¿ªÆôlogÈÕÖ¾µÈ¼¶ |
| 268 | open_module_log_num:¿ªÆôÈÕ־ģ¿éµÄ¸öÊý |
| 269 | open_module_log:¿ªÆôÈÕ×Ó·þÎñµÄÄ£¿é |
| 270 | ** Returned value: None |
| 271 | ** Created by: |
| 272 | *********************************************************************************************************/ |
| 273 | void lynq_store_log_deploy(log_level_str *log_level_write_level_str) |
| 274 | { |
| 275 | char *log_level_writefile = "persist.store_log_level"; |
| 276 | char store_log_str_buf[512] = {0}; |
| 277 | |
| 278 | store_log_str_buf[0] = '+'; |
| 279 | hextostring(log_level_write_level_str->open_log_level,&store_log_str_buf[1]); |
| 280 | hextostring(log_level_write_level_str->open_module_log_num,&store_log_str_buf[3]); |
| 281 | for(int i = 0; i < log_level_write_level_str->open_module_log_num; i++) |
| 282 | { |
| 283 | hextostring(log_level_write_level_str->open_module_log[i],&store_log_str_buf[5+i*2]); |
| 284 | } |
| 285 | |
| 286 | property_set(log_level_writefile,(char *)store_log_str_buf); |
| 287 | return ; |
| 288 | } |
| 289 | |
| 290 | /********************************************************************************************************* |
| 291 | ** Function name: lynq_fget_log_level |
| 292 | ** Description ¶ÁÈ¡ÈÕÖ¾ÅäÖÃÐÅÏ¢ |
| 293 | ** Input parameters: open_log_level:¿ªÆôlogÈÕÖ¾µÈ¼¶ |
| 294 | open_module_log_num:¿ªÆôÈÕ־ģ¿éµÄ¸öÊý |
| 295 | open_module_log:¿ªÆôÈÕ×Ó·þÎñµÄÄ£¿é |
| 296 | ** Returned value: 0£º³É¹¦£¬-1£ºÊ§°Ü |
| 297 | ** Created by: |
| 298 | *********************************************************************************************************/ |
| 299 | int lynq_read_store_log_deploy(log_level_str *log_level_read_level_str) |
| 300 | { |
| 301 | char *log_level_readfile = "persist.store_log_level"; |
| 302 | |
| 303 | if(property_get(log_level_readfile,(char *)log_level_read_level_str,NULL) > 0) |
| 304 | { |
| 305 | // printf("read log level deploy success!!!\n"); |
| 306 | return 0; |
| 307 | } |
| 308 | else |
| 309 | { |
| 310 | // printf("get file error!!!\n"); |
| 311 | return -1; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | int itoa(int val, char *buf,int size) //16½øÖÆ×ª»»³É×Ö·û´® |
| 316 | { |
| 317 | char *p = buf; |
| 318 | char t = '0'; |
| 319 | int len = 0; |
| 320 | int slen = size; |
| 321 | while(val > 0 && len < slen) { |
| 322 | t = val % 16; |
| 323 | val -= t; |
| 324 | val /= 16; |
| 325 | if (t > 9) { |
| 326 | *p++ = t- 10 + 'a'; |
| 327 | } else { |
| 328 | *p++ = t + '0'; |
| 329 | } |
| 330 | ++len; |
| 331 | } |
| 332 | for (int i = 0;i < len/2;++i) { |
| 333 | char c = buf[i]; |
| 334 | buf[i] = buf[len-1-i]; |
| 335 | buf[len-1-i] = c; |
| 336 | } |
| 337 | buf[len] = '\0'; |
| 338 | return len; |
| 339 | } |
| 340 | |
| 341 | void hextostring(unsigned char val, char *buf) |
| 342 | { |
| 343 | char strbuf[64] = {0}; |
| 344 | itoa(val,strbuf,2); |
| 345 | memcpy(buf,strbuf,3); |
| 346 | if((val >= 0) && (val <= 15)) |
| 347 | { |
| 348 | buf[0] = '0'; |
| 349 | buf[1] = strbuf[0]; |
| 350 | buf[2] = '\0'; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | static int pows(int x, int y) |
| 355 | { |
| 356 | int result = 1; |
| 357 | |
| 358 | while (y--) |
| 359 | result *= x; |
| 360 | return result; |
| 361 | } |
| 362 | |
| 363 | int string2hex(const char *buffer, int cnt) |
| 364 | { |
| 365 | int c = 0; |
| 366 | char t = 0; |
| 367 | int count = cnt; |
| 368 | |
| 369 | while (count--) { |
| 370 | t = *(buffer + cnt - count - 1); |
| 371 | if (t >= 'A' && t <= 'F') |
| 372 | c += ((t - 'A') + 10) * pows(16, count); |
| 373 | else if (t >= 'a' && t <= 'f') |
| 374 | c += ((t - 'a') + 10) * pows(16, count); |
| 375 | else if (t >= '0' && t <= '9') |
| 376 | c += (t - '0') * pows(16, count); |
| 377 | else |
| 378 | c = -1; |
| 379 | } |
| 380 | return c; |
| 381 | } |
| 382 | |
| 383 | int get_hexbuffer(char *data_buffer, char *hex_buffer) |
| 384 | { |
| 385 | char *ptr = data_buffer; |
| 386 | int index = 0; |
| 387 | int data_offset = 0; |
| 388 | |
| 389 | while (*ptr && *++ptr) { |
| 390 | *(hex_buffer + index++) = string2hex(ptr - 1, 2); |
| 391 | ptr++; |
| 392 | data_offset++; |
| 393 | } |
| 394 | *(hex_buffer + index) = 0; |
| 395 | return data_offset; |
| 396 | } |
| 397 | /********************************************************************************************************* |
| 398 | ** Function name: lynq_enable_log_serve |
| 399 | ** Description ʹÄÜÈÕÖ¾·þÎñ |
| 400 | ** Input parameters: None |
| 401 | ** Returned value: None |
| 402 | ** Created by: |
| 403 | *********************************************************************************************************/ |
| 404 | void lynq_enable_log_serve(void) |
| 405 | { |
| 406 | log_level_str read_level_str; |
| 407 | int scanf_ret = 0; |
| 408 | char read_buf[] = {0}; |
| 409 | char shift_read_buf[128] = {0}; |
| 410 | static char stcp_read_buf[128] = {0}; |
| 411 | char *log_level_readfile = "persist.store_log_level"; |
| 412 | |
| 413 | if(property_get(log_level_readfile,&read_buf,NULL) > 0) |
| 414 | { |
| 415 | if(strcmp(read_buf,stcp_read_buf) == 0) |
| 416 | { |
| 417 | // printf("strcmp(read_buf,stcp_read_buf) == 0\n"); |
| 418 | return ; |
| 419 | } |
| 420 | strcpy(stcp_read_buf,read_buf); |
| 421 | |
| 422 | if(read_buf[0] == '+') |
| 423 | { |
| 424 | // printf("start\n"); |
| 425 | scanf_ret = sscanf(&read_buf[1],"%128s",shift_read_buf); |
| 426 | // printf("scanf_ret:%s\n",read_buf); |
| 427 | if(scanf_ret) |
| 428 | { |
| 429 | |
| 430 | get_hexbuffer(shift_read_buf,read_buf); |
| 431 | read_level_str.open_log_level = read_buf[0]; |
| 432 | read_level_str.open_module_log_num = read_buf[1]; |
| 433 | memcpy(read_level_str.open_module_log,&read_buf[2],read_level_str.open_module_log_num); |
| 434 | // printf("data:%d %d\n",read_level_str.open_log_level,read_level_str.open_module_log_num); |
| 435 | } |
| 436 | |
| 437 | } |
| 438 | else |
| 439 | { |
| 440 | lynq_read_store_log_deploy(&read_level_str); |
| 441 | } |
| 442 | if((read_level_str.open_log_level < LOG_LEVEL_MAX) && (read_level_str.open_module_log_num < LOG_TAG_MAX)) |
| 443 | { |
| 444 | lynq_fget_log_level(read_level_str); |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | /********************************************************************************************************* |
| 450 | ** Function name: lynq_store_log_test |
| 451 | ** Description ÅäÖÃÈÕÖ¾ÐÅÏ¢£¨ÅäÖÃÍê³Éºó£¬Èô²»ÐÞ¸ÄÅäÖÃÐÅÏ¢£¬¿ÉÒÔºöÂÔ£© |
| 452 | ** Input parameters: None |
| 453 | ** Returned value: None |
| 454 | ** Created by: |
| 455 | *********************************************************************************************************/ |
| 456 | #define MODULE_LOG_NUM 6 |
| 457 | void lynq_store_log_test(void) |
| 458 | { |
| 459 | log_level_str write_level_str; |
| 460 | log_level_str read_level_str; |
| 461 | |
| 462 | char open_log_level = LOG_WARNING; |
| 463 | char module_log_buf[MODULE_LOG_NUM] = {LOG_MQTT,LOG_SPI,LOG_THREADTEST_DBUS, LOG_THREADHANDLE, LOG_HTTP, LOG_FTP}; |
| 464 | |
| 465 | write_level_str.open_log_level = open_log_level; |
| 466 | write_level_str.open_module_log_num = MODULE_LOG_NUM; |
| 467 | memcpy(write_level_str.open_module_log,module_log_buf,MODULE_LOG_NUM); |
| 468 | if(lynq_read_store_log_deploy(&read_level_str) != 0) |
| 469 | { |
| 470 | lynq_store_log_deploy(&write_level_str); |
| 471 | lynq_store_log_deploy(&write_level_str); |
| 472 | } |
| 473 | |
| 474 | // printf("write log deploy success!!!\n"); |
| 475 | return ; |
| 476 | } |
| 477 | |
| 478 | /********************************************************************************************************* |
| 479 | ** Function name: lynq_read_set_log_test |
| 480 | ** Description ʹÄÜÅäÖÃÐÅÏ¢ |
| 481 | ** Input parameters: None |
| 482 | ** Returned value: None |
| 483 | ** Created by: |
| 484 | *********************************************************************************************************/ |
| 485 | void lynq_read_set_log_test(void) |
| 486 | { |
| 487 | lynq_enable_log_serve(); |
| 488 | } |
| 489 | |