简单日志记录模块

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 ;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿木小呆呆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值