用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - c代码库

log

2016-11-03 作者: evol举报

[c]代码库

#include "log.h"


/*
data:      2015-11-13
input:	
output:	
return: 		
function:	  把接收到的IPC日志缓存到队列

*/

int Clog::inqueue_log(netpack_t * netpack)
{
	time_t t;
	time(&t);
	lsg_t *lsg = (lsg_t *)malloc(sizeof(lsg_t));	
	char *buf=(char *)malloc(MAX_CMD_SIZE);
	char *tem=(char *)malloc(MAX_CMD_SIZE);
	bzero(buf,MAX_CMD_SIZE);
	bzero(tem,MAX_CMD_SIZE);
	//1.日志信息处理
	//dbg("writing log 1:%s",buf);
	sprintf(tem,"%s",ctime(&t));
	del_char(tem,'\n');
	del_char(tem,'\r');
	
	sprintf(buf,"%.*s,DEV_SN:[%.*s] log message:[%.*s]\n",\
		strlen(tem),tem,\
		DEV_ID_SIZE,netpack->netdev.dev_sn,\
		netpack->datalen,netpack->data);		
	lsg->log_msg = buf;
	
	lsg->log_type = netpack->log.type;

	//2.插入链表
	switch(lsg->log_type)
		{
			case OK:
			case INIT_ERR:
			case RTMP_DOWN:
			case RTMP_UP:
			case RTMP_CONNECT_ERR:
			case RTMP_NO_CONNECT:
				lsg->next = NULL;
				if(!loglist.tail)
					loglist.tail = lsg;
				if(loglist.head)
					loglist.head->next = lsg;
				loglist.head = lsg;
				loglist.log_len++;
				break;
			case URL_ERROR:
			case NET_ERROR:
			case SN_ID_ERROR:
				lsg->next = NULL;
				if(!serlog_list.tail)
					serlog_list.tail = lsg;
				if(serlog_list.head)
					serlog_list.head->next = lsg;
				serlog_list.head = lsg;
				serlog_list.log_len++;
				break;
			default:
				break;
		}
	if(tem)
		free(tem);
	return 0;
}


/*
data:      2015-11-13
input:	
output:	
return:    
		
function:	  把队列里的日志写入文件

*/

int Clog::write_log()
{
	int rtmplogfd = 0;
	int serverlogfd = 0;
	lsg_t *cur_log = loglist.tail;
	lsg_t *cur_serverlog= serlog_list.tail;
	lsg_t *tem;

	//每次写文件前只读一次链表头长度,减少资源竞争
	int glog_size = loglist.log_len;
	int serverlog_size = serlog_list.log_len;
	
	if((rtmplogfd=open(RTMP_LOG_FILE,O_CREAT|O_WRONLY|O_APPEND,0600))>0)
	{
		//>1 保证头和尾不重合,防止资源竞争
		while(cur_log && (loglist.log_len>1))
		{


			switch(cur_log->log_type)
			{
				case OK:
				case INIT_ERR:
				case RTMP_DOWN:
				case RTMP_UP:
				case RTMP_CONNECT_ERR:
				case RTMP_NO_CONNECT:
					write(rtmplogfd,cur_log->log_msg,strlen(cur_log->log_msg));
					break;
				default:
					break;
			}
			//dbg("%s 1\n",__FUNCTION__);
			if(cur_log->log_msg)
				free(cur_log->log_msg);
			
			//dbg("%s 2\n",__FUNCTION__);
			if(cur_log)
				free(cur_log);

			
			//dbg("%s 3\n",__FUNCTION__);
			cur_log = cur_log->next;
			loglist.tail = cur_log;
			glog_size--;
		}
		if(rtmplogfd) 
			close(rtmplogfd);

		
	}
	//平台日志直接写入
	#if 0
	if((serverlogfd=open(PLATFORM_LOG,O_CREAT|O_WRONLY|O_APPEND,0600))>0)
	{
		//>1 保证头和尾不重合,防止资源竞争
		while(cur_serverlog && (serlog_list.log_len > 1))
		{
			switch(cur_serverlog->log_type)
			{
				case URL_ERROR:
				case NET_ERROR:
				case SN_ID_ERROR:
					write(serverlogfd,cur_serverlog->log_msg,strlen(cur_serverlog->log_msg));
					break;
				default:
					break;
			}
			//dbg("%s 3\n",__FUNCTION__);
			if(cur_serverlog->log_msg)
				free(cur_serverlog->log_msg);
			if(cur_serverlog)
				free(cur_serverlog);
			//dbg("%s 4\n",__FUNCTION__);
			cur_serverlog = cur_serverlog->next;
			serlog_list.tail = cur_serverlog;
			serverlog_size--;
		}	
		if(serverlogfd) 
			close(serverlogfd);
	}
	#endif
	return 0;
	
}

/*
data:      2015-12-8
input:	
output:	
return:     		
function:	  平台日志

*/

void Clog::platform_log(const char *msg)
{
	int fd,buf_size;
	time_t t;
	char *buf=(char *)malloc(MAX_CMD_SIZE);
	char *tem=(char *)malloc(MAX_CMD_SIZE);
	bzero(buf,MAX_CMD_SIZE);
	bzero(tem,MAX_CMD_SIZE);
	time(&t);
	
	//dbg("writing log 1:%s",buf);
	sprintf(tem,"%s",ctime(&t));
	del_char(tem,'\n');
	del_char(tem,'\r');
	sprintf(buf,"%.*s %s\n",strlen(tem),tem,msg);
	//dbg("writing platfrom log 2:%s",buf);
	buf_size = strlen(buf);
	if((fd=open(PLATFORM_LOG,O_CREAT|O_WRONLY|O_APPEND,0600))>0)
	{
		write(fd,buf,buf_size);
		close(fd);
	}
	if(buf)
		free(buf);
	if(buf)
		free(tem);
}



网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...