typedef struct _protocol_log
{
int m_flag; /*已使用*/
char m_log_str[STR_LEN_256]; /*日志信息*/
struct _protocol_log *m_next;
}protocol_log_t;
typedef struct
{
int m_log_num;
protocol_log_t *m_log_list_head;
}log_list_t;
/************************************* 日志记录模块 **************************************/
#define MAX_LOG_NUM (500)
#define PROTOCOL_LOG_PATH ("/mnt/run_data/event/thirdprotocol.log")
static log_list_t g_log_list_info = { 0 };
static pthread_mutex_t g_log_mutex;
int add_protocol_log(char *p_str)
{
//printf("p_str = %s\n", p_str);
mutex_lock(&g_log_mutex);
protocol_log_t *head = g_log_list_info.m_log_list_head;
protocol_log_t *temp_head = g_log_list_info.m_log_list_head;
if (g_log_list_info.m_log_num >= MAX_LOG_NUM)
{
g_log_list_info.m_log_list_head = head->m_next;
/*找到尾节点*/
while (temp_head->m_next)
{
temp_head = temp_head->m_next;
}
head->m_flag = 1;
snprintf(head->m_log_str, STR_LEN_256, "%s", p_str);
head->m_next = NULL;
temp_head->m_next = head;
//printf("add_protocol_log0: m_log_num = %d m_log_str = %s \n", g_log_list_info.m_log_num, head->m_log_str);
mutex_unlock(&g_log_mutex);
return SN_SUCCESS;
}
protocol_log_t *log = (protocol_log_t *)malloc(sizeof(protocol_log_t));
log->m_flag = 1;
snprintf(log->m_log_str, STR_LEN_256, "%s", p_str);
log->m_next = NULL;
if (!g_log_list_info.m_log_list_head)
{
g_log_list_info.m_log_list_head = log;
g_log_list_info.m_log_num = 1;
//printf("add_protocol_log1: m_log_num = %d m_log_str = %s \n", g_log_list_info.m_log_num, log->m_log_str);
mutex_unlock(&g_log_mutex);
return SN_SUCCESS;
}
while (head->m_next)
{
head = head->m_next;
}
head->m_next = log;
g_log_list_info.m_log_num++;
//printf("add_protocol_log1: m_log_num = %d m_log_str = %s \n", g_log_list_info.m_log_num, log->m_log_str);
mutex_unlock(&g_log_mutex);
return SN_SUCCESS;
}
/*读取日志*/
int read_protocol_log()
{
char sz_str[STR_LEN_256] = { 0 };
FILE *fp = fopen(PROTOCOL_LOG_PATH, "r");
if (!fp)
{
return SN_ERROR_INVALID_PARAM;
}
while(fgets(sz_str, STR_LEN_256, fp))
{
add_protocol_log(sz_str);
}
if (fp)
{
fclose(fp);
fp = NULL;
}
return SN_SUCCESS;
}
/*存储日志*/
int write_protocol_log()
{
mutex_lock(&g_log_mutex);
FILE *fp = fopen(PROTOCOL_LOG_PATH, "w+");
if (!fp)
{
mutex_unlock(&g_log_mutex);
return SN_ERROR_INVALID_PARAM;
}
protocol_log_t *head = g_log_list_info.m_log_list_head;
while (head)
{
if (head->m_flag == 1)
{
fputs(head->m_log_str, fp);
}
head = head->m_next;
}
if (fp)
{
fclose(fp);
fp = NULL;
}
mutex_unlock(&g_log_mutex);
return SN_SUCCESS;
}
void thi_protocol_log_init( )
{
if (!g_log_list_info.m_log_list_head)
{
/*初始化锁*/
ydxw_mutex_init(&g_log_mutex);
thi_read_protocol_log();
THR_LOG_Inf("========== log num = %d \n", g_log_list_info.m_log_num);
}
return ;
}
void protocol_log_save(int p_forced_save)
{
static time_t n_last_time = time(NULL);
time_t n_cur_time = time(NULL);
//printf("========== abs(n_cur_time - n_last_time) = %d \n", abs(n_cur_time - n_last_time));
if ((abs(n_cur_time - n_last_time) > 1*60)||(1 == p_forced_save))
{
n_last_time = n_cur_time;
write_protocol_log();
/*protocol_log_t *head = g_log_list_info.m_log_list_head;
while (head)
{
if (head->m_flag == 1)
{
printf("======log:%s\n", head->m_log_str);
}
head = head->m_next;
}*/
}
return ;
}
简单日志记录模块
最新推荐文章于 2024-02-28 00:26:02 发布