blob: 3606536e1c22e04e74769efe67855f6ef0992d7d [file] [log] [blame]
#include "liblog.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <log/log.h>
void hextostring(unsigned char val, char *buf);
#define LOG_NAME_LEN 128
static unsigned int log_level = 0; //logµÈ¼¶£¬ÐèÒª¿ª·¢ÈËÔ±¶¯Ì¬ÅäÖÃlogµÈ¼¶£¬Ä¬Èϵȼ¶Îª0
static unsigned char log_name_arr[LOG_NAME_LEN] = {0};
/*********************************************************************************************************
** Function name: lynq_log_output
** Description lynq_log_output
** Input parameters: Tag:TagÀàÐÍ(log_level_enum)
Level:TagÏ´òÓ¡level(log_tag_enum)
format:printf´òÓ¡µÄ²»¶¨³¤²ÎÊý
** Returned value: None
** Created by:
*********************************************************************************************************/
void lynq_log_output(log_tag_enum Tag,log_level_enum Level,const char *format,...)
{
#if LOG_ENABLE
if((Tag >= LOG_TAG_MAX)||(Level >= LOG_LEVEL_MAX))
return;
// printf("mo:%d---le:%d---Level:%d\n",LogTagEnableInfoTable[Tag],log_level,Level);
// printf("rmodule:%d---rlevel:%d\n",((LogTagEnableInfoTable[Tag] >> Level)&0x00000001),((log_level >> Level)&0x00000001));
if(((LogTagEnableInfoTable[Tag] >> Level)&0x00000001)&((log_level >> Level)&0x00000001))
{
va_list args;
//TagName
printf("[%s]",LogTagNameInfoTable[Tag]);
//LevelName
printf("[%s]:",LogLevelNameInfoTable[Level]);
va_start(args,format);
vprintf(format,args);
va_end(args);
}
return ;
#endif //LOG_ENABLE
}
void lynq_log_global_output(log_level_enum Level,const char *format,...)
{
#if LOG_ENABLE
char out_buf[1100] = {0};
char buf[1024] = {0};
if(Level >= LOG_LEVEL_MAX)
return;
if((log_level >> Level)&0x00000001)
{
va_list args;
//TagName
if(log_name_arr[0] > 0)
{
printf("[%s]",log_name_arr);
}
//LevelName
printf("%s:",LogLevelNameInfoTable[Level]); //lt dele @2021.7.22 for []
va_start(args,format);
vprintf(format,args);
vsnprintf(buf, sizeof(buf), format, args); //lt add @2021.7.22 for write buf
va_end(args);
printf("\r\n");
sprintf(out_buf, "%s %s", LogLevelNameInfoTable[Level], buf);//lt add @2021.7.22 for write outbuf
__android_log_print(ANDROID_LOG_DEBUG,log_name_arr, "%s",out_buf); //lt add @2021.7.22 for write syslog.log
}
return ;
#endif //LOG_ENABLE
}
void lynq_log_configuration_set(char *log_name,char log_data_arr)
{
char log_data_buf[LOG_NAME_LEN] = {"persist."};
char log_data_str[32] = {0};
if(log_name == NULL)
{
return ;
}
if(log_data_arr < LOG_LEVEL_MAX)
{
sprintf(log_data_str,"%d",log_data_arr);
// printf("log_data_str:%s\n",log_data_str);
if((strlen(log_name)) < (LOG_NAME_LEN - 9))
{
strcat(log_data_buf,log_name);
property_set(log_data_buf,log_data_str);
}
}
lynq_log_configuration_init(log_name);
}
void lynq_deal_with_level(unsigned int get_log_level)
{
switch(get_log_level)
{
case LOG_DEBUG:
log_level |= ENABLE(LOG_DEBUG);
case LOG_INFO:
log_level |= ENABLE(LOG_INFO);
case LOG_WARNING:
log_level |= ENABLE(LOG_WARNING);
case LOG_ERROR:
log_level |= ENABLE(LOG_ERROR);
case LOG_VERBOSE:
log_level |= ENABLE(LOG_VERBOSE);
break;
default:
log_level |= ENABLE(LOG_VERBOSE);
break;
}
}
void lynq_log_configuration_init(char *log_name)
{
char log_data_buf[LOG_NAME_LEN] = {"persist."};
char get_propty_log_data[32] ={0};
unsigned int get_log_level = 0;
if(log_name == NULL)
{
return ;
}
if((strlen(log_name)) < (LOG_NAME_LEN - 9))
{
strcpy(log_name_arr,log_name);
strcat(log_data_buf,log_name);
if(property_get(log_data_buf,(char *)get_propty_log_data,NULL) > 0)
{
get_log_level = atoi(get_propty_log_data);
// printf("get_log_level:%d\n",get_log_level);
}
else
{
log_level = 0;
// printf("get propty data fail\n");
}
}
lynq_deal_with_level(get_log_level);
}
/*********************************************************************************************************
** Function name: lynq_update_log_output
** Description ÖØÐ»ñÈ¡logµÈ¼¶²¢Ê¹ÆäÉúЧ
** Input parameters: Tag:TagÀàÐÍ(log_level_enum)
Level:TagÏ´òÓ¡level(log_tag_enum)
format:printf´òÓ¡µÄ²»¶¨³¤²ÎÊý
** Returned value: None
** Created by:
*********************************************************************************************************/
void lynq_update_log_output(log_tag_enum Tag,log_level_enum Level,const char *format,...)
{
#if LOG_ENABLE
lynq_enable_log_serve();
if((Tag >= LOG_TAG_MAX)||(Level >= LOG_LEVEL_MAX))
return;
// printf("mo:%d---le:%d---Level:%d\n",LogTagEnableInfoTable[Tag],log_level,Level);
// printf("rmodule:%d---rlevel:%d\n",((LogTagEnableInfoTable[Tag] >> Level)&0x00000001),((log_level >> Level)&0x00000001));
if(((LogTagEnableInfoTable[Tag] >> Level)&0x00000001)&((log_level >> Level)&0x00000001))
{
va_list args;
//TagName
printf("[%s]",LogTagNameInfoTable[Tag]);
//LevelName
printf("[%s]:",LogLevelNameInfoTable[Level]);
va_start(args,format);
vprintf(format,args);
va_end(args);
}
return ;
#endif //LOG_ENABLE
}
/*********************************************************************************************************
** Function name: lynq_log_printf_output
** Description ·â×°printf
** Input parameters: Tag:TagÀàÐÍ(log_level_enum)
Level:TagÏ´òÓ¡level(log_tag_enum)
format:printf´òÓ¡µÄ²»¶¨³¤²ÎÊý
** Returned value: None
** Created by:
*********************************************************************************************************/
void lynq_log_printf_output(log_tag_enum Tag,log_level_enum Level,const char *format,...)
{
#if LOG_ENABLE
if((Tag >= LOG_TAG_MAX)||(Level >= LOG_LEVEL_MAX))
return;
va_list args;
//TagName
printf("[%s]",LogTagNameInfoTable[Tag]);
//LevelName
printf("[%s]:",LogLevelNameInfoTable[Level]);
va_start(args,format);
vprintf(format,args);
va_end(args);
return ;
#endif //LOG_ENABLE
}
/*********************************************************************************************************
** Function name: lynq_fget_log_level
** Description ¿ªÆôlogÈÕÖ¾µÈ¼¶
** Input parameters: open_log_level:¿ªÆôlogÈÕÖ¾µÈ¼¶
open_module_log_num:¿ªÆôÈÕ־ģ¿éµÄ¸öÊý
open_module_log:¿ªÆôÈÕ×Ó·þÎñµÄÄ£¿é
** Returned value: None
** Created by:
*********************************************************************************************************/
void lynq_fget_log_level(log_level_str level_str)
{
log_level = 0;
switch(level_str.open_log_level)
{
case LOG_DEBUG:
log_level |= ENABLE(LOG_DEBUG);
case LOG_INFO:
log_level |= ENABLE(LOG_INFO);
case LOG_WARNING:
log_level |= ENABLE(LOG_WARNING);
case LOG_ERROR:
log_level |= ENABLE(LOG_ERROR);
case LOG_VERBOSE:
log_level |= ENABLE(LOG_VERBOSE);
break;
default:
log_level |= ENABLE(LOG_VERBOSE);
break;
}
for(int i = 0; i < level_str.open_module_log_num; i++)
{
LogTagEnableInfoTable[level_str.open_module_log[i]] = ENABLE(LOG_VERBOSE)|ENABLE(LOG_DEBUG)|ENABLE(LOG_INFO)|ENABLE(LOG_WARNING)|ENABLE(LOG_ERROR);
}
return ;
}
/*********************************************************************************************************
** Function name: lynq_store_log_deploy
** Description ±£´æÈÕÖ¾ÅäÖÃÐÅÏ¢
** Input parameters: open_log_level:¿ªÆôlogÈÕÖ¾µÈ¼¶
open_module_log_num:¿ªÆôÈÕ־ģ¿éµÄ¸öÊý
open_module_log:¿ªÆôÈÕ×Ó·þÎñµÄÄ£¿é
** Returned value: None
** Created by:
*********************************************************************************************************/
void lynq_store_log_deploy(log_level_str *log_level_write_level_str)
{
char *log_level_writefile = "persist.store_log_level";
char store_log_str_buf[512] = {0};
store_log_str_buf[0] = '+';
hextostring(log_level_write_level_str->open_log_level,&store_log_str_buf[1]);
hextostring(log_level_write_level_str->open_module_log_num,&store_log_str_buf[3]);
for(int i = 0; i < log_level_write_level_str->open_module_log_num; i++)
{
hextostring(log_level_write_level_str->open_module_log[i],&store_log_str_buf[5+i*2]);
}
property_set(log_level_writefile,(char *)store_log_str_buf);
return ;
}
/*********************************************************************************************************
** Function name: lynq_fget_log_level
** Description ¶ÁÈ¡ÈÕÖ¾ÅäÖÃÐÅÏ¢
** Input parameters: open_log_level:¿ªÆôlogÈÕÖ¾µÈ¼¶
open_module_log_num:¿ªÆôÈÕ־ģ¿éµÄ¸öÊý
open_module_log:¿ªÆôÈÕ×Ó·þÎñµÄÄ£¿é
** Returned value: 0£º³É¹¦£¬-1£ºÊ§°Ü
** Created by:
*********************************************************************************************************/
int lynq_read_store_log_deploy(log_level_str *log_level_read_level_str)
{
char *log_level_readfile = "persist.store_log_level";
if(property_get(log_level_readfile,(char *)log_level_read_level_str,NULL) > 0)
{
// printf("read log level deploy success!!!\n");
return 0;
}
else
{
// printf("get file error!!!\n");
return -1;
}
}
int itoa(int val, char *buf,int size) //16½øÖÆ×ª»»³É×Ö·û´®
{
char *p = buf;
char t = '0';
int len = 0;
int slen = size;
while(val > 0 && len < slen) {
t = val % 16;
val -= t;
val /= 16;
if (t > 9) {
*p++ = t- 10 + 'a';
} else {
*p++ = t + '0';
}
++len;
}
for (int i = 0;i < len/2;++i) {
char c = buf[i];
buf[i] = buf[len-1-i];
buf[len-1-i] = c;
}
buf[len] = '\0';
return len;
}
void hextostring(unsigned char val, char *buf)
{
char strbuf[64] = {0};
itoa(val,strbuf,2);
memcpy(buf,strbuf,3);
if((val >= 0) && (val <= 15))
{
buf[0] = '0';
buf[1] = strbuf[0];
buf[2] = '\0';
}
}
static int pows(int x, int y)
{
int result = 1;
while (y--)
result *= x;
return result;
}
int string2hex(const char *buffer, int cnt)
{
int c = 0;
char t = 0;
int count = cnt;
while (count--) {
t = *(buffer + cnt - count - 1);
if (t >= 'A' && t <= 'F')
c += ((t - 'A') + 10) * pows(16, count);
else if (t >= 'a' && t <= 'f')
c += ((t - 'a') + 10) * pows(16, count);
else if (t >= '0' && t <= '9')
c += (t - '0') * pows(16, count);
else
c = -1;
}
return c;
}
int get_hexbuffer(char *data_buffer, char *hex_buffer)
{
char *ptr = data_buffer;
int index = 0;
int data_offset = 0;
while (*ptr && *++ptr) {
*(hex_buffer + index++) = string2hex(ptr - 1, 2);
ptr++;
data_offset++;
}
*(hex_buffer + index) = 0;
return data_offset;
}
/*********************************************************************************************************
** Function name: lynq_enable_log_serve
** Description ʹÄÜÈÕÖ¾·þÎñ
** Input parameters: None
** Returned value: None
** Created by:
*********************************************************************************************************/
void lynq_enable_log_serve(void)
{
log_level_str read_level_str;
int scanf_ret = 0;
char read_buf[] = {0};
char shift_read_buf[128] = {0};
static char stcp_read_buf[128] = {0};
char *log_level_readfile = "persist.store_log_level";
if(property_get(log_level_readfile,&read_buf,NULL) > 0)
{
if(strcmp(read_buf,stcp_read_buf) == 0)
{
// printf("strcmp(read_buf,stcp_read_buf) == 0\n");
return ;
}
strcpy(stcp_read_buf,read_buf);
if(read_buf[0] == '+')
{
// printf("start\n");
scanf_ret = sscanf(&read_buf[1],"%128s",shift_read_buf);
// printf("scanf_ret:%s\n",read_buf);
if(scanf_ret)
{
get_hexbuffer(shift_read_buf,read_buf);
read_level_str.open_log_level = read_buf[0];
read_level_str.open_module_log_num = read_buf[1];
memcpy(read_level_str.open_module_log,&read_buf[2],read_level_str.open_module_log_num);
// printf("data:%d %d\n",read_level_str.open_log_level,read_level_str.open_module_log_num);
}
}
else
{
lynq_read_store_log_deploy(&read_level_str);
}
if((read_level_str.open_log_level < LOG_LEVEL_MAX) && (read_level_str.open_module_log_num < LOG_TAG_MAX))
{
lynq_fget_log_level(read_level_str);
}
}
}
/*********************************************************************************************************
** Function name: lynq_store_log_test
** Description ÅäÖÃÈÕÖ¾ÐÅÏ¢£¨ÅäÖÃÍê³Éºó£¬Èô²»ÐÞ¸ÄÅäÖÃÐÅÏ¢£¬¿ÉÒÔºöÂÔ£©
** Input parameters: None
** Returned value: None
** Created by:
*********************************************************************************************************/
#define MODULE_LOG_NUM 6
void lynq_store_log_test(void)
{
log_level_str write_level_str;
log_level_str read_level_str;
char open_log_level = LOG_WARNING;
char module_log_buf[MODULE_LOG_NUM] = {LOG_MQTT,LOG_SPI,LOG_THREADTEST_DBUS, LOG_THREADHANDLE, LOG_HTTP, LOG_FTP};
write_level_str.open_log_level = open_log_level;
write_level_str.open_module_log_num = MODULE_LOG_NUM;
memcpy(write_level_str.open_module_log,module_log_buf,MODULE_LOG_NUM);
if(lynq_read_store_log_deploy(&read_level_str) != 0)
{
lynq_store_log_deploy(&write_level_str);
lynq_store_log_deploy(&write_level_str);
}
// printf("write log deploy success!!!\n");
return ;
}
/*********************************************************************************************************
** Function name: lynq_read_set_log_test
** Description ʹÄÜÅäÖÃÐÅÏ¢
** Input parameters: None
** Returned value: None
** Created by:
*********************************************************************************************************/
void lynq_read_set_log_test(void)
{
lynq_enable_log_serve();
}