www.pudn.com > SyslogTool_Analyse_Final8.6.rar > SyslogInterface.cpp


 
#include "stdafx.h" 
 
#include  
 
#include "SyslogTool.h" 
#include "SyslogToolDlg.h" 
 
#pragma comment(lib,"ws2_32") 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
char buffer_to_write[4096]; 
 
/* The Queue is used to store the datagram */  
Syslog_Queue::Syslog_Queue(Syslog *Syslog_Obj){//set the structure of Queue 
	header = NULL; 
	tail = NULL; 
	SL = Syslog_Obj; 
} 
Syslog_Queue::Syslog_Queue(){//set the structure of Queue 
	header = NULL; 
	tail = NULL; 
	SL = NULL; 
} 
 
void Syslog_Queue::enque(int8_t *p)//put the datagram to the queue 
{ 
	struct syslog_packet_buffer *buffer = new syslog_packet_buffer; 
	if (buffer == NULL)                                
	{ 
		SL->Dlg->MessageBox("ÄÚ´æ²»×ã!");  
		SL->Dlg->exit(); 
	} 
	memcpy(buffer->buf,p,2048); 
	if (header == NULL) 
	{ 
		 
		header = buffer; 
		 
	}else 
	{ 
		tail->next = buffer; 
	} 
	tail = buffer; 
	buffer->next = NULL; 
} 
 
void Syslog_Queue::remove_head()//when the push the data from the queue,remove the head of the queue 
{ 
	if (header != NULL ) 
	{ 
		struct syslog_packet_buffer *temp = header; 
		header = temp->next; 
		delete temp; 
	} 
} 
 
Syslog::Syslog(CSyslogToolDlg *a):syslog_buf_queue(this) 
{ 
	 
	syslog_status = 0; 
 
	Dlg = a; 
 
} 
 
/* process_data() is used to process the datagram */  
void Syslog::syslog_process_data(int8_t *buf){ 
 
    syslog_buf_queue.enque(buf);//get the data from the queue 
	if (syslog_status == 0 ){ 
	    while (syslog_buf_queue.header != NULL ){ 
		    syslog_status = 1; 
		    Process_Syslog_data(syslog_buf_queue.header->buf);//process the data 
	        syslog_buf_queue.remove_head();//remove the head of the queue  
		} 
    	syslog_status = 0; 
	} 
 
} 
 
/* Process_Syslog_data() is used to store the datagram of syslog to d:\syslog_raw.txt */ 
void Syslog::Process_Syslog_data( int8_t *buf ){ 
	char buffer[4096] ; 
    int k=0; 
	int buffer_flag=0; 
	int buffer_to_write_flag=0; 
	int log_flag=0; 
	int write_flag=0; 
	int enter_number=0; 
	int begin_flag=0; 
	int end_flag=0; 
	memset (buffer,0,4096); 
	memset (buffer_to_write,0,4096); 
	struct SYSLOG *syslog_packet=(struct SYSLOG *)buf; 
    strcpy(buffer,syslog_packet->syslog_buff); 
    CStdioFile file;  
 
	for(buffer_flag=0;buffer_flag<4096;buffer_flag++){ 
		if((buffer[buffer_flag]>=0)&&(buffer[buffer_flag]<=127)){ 
			write_flag=1; 
		} 
		else{ 
			write_flag=0; 
			log_flag=0; 
			enter_number=enter_number+1; 
		} 
		if((buffer[buffer_flag]=='<')&&(write_flag==1)){ 
			begin_flag=buffer_to_write_flag+1; 
			log_flag=1; 
			enter_number=0; 
		} 
		if(log_flag==1){ 
			buffer_to_write[buffer_to_write_flag]=buffer[buffer_flag]; 
		    buffer_to_write_flag++; 
		} 
		if(enter_number==1){ 
			end_flag=buffer_to_write_flag-1; 
			buffer_to_write[buffer_to_write_flag]='\n'; 
			buffer_to_write_flag++; 
			Syslog_Analyse(begin_flag,end_flag); 
		} 
	} 
 
 
		 
		file.Open("d:\\syslog_raw.txt",CFile::modeCreate | CFile::modeNoTruncate//open a text to restore the datagram 
			| CFile::modeWrite); 
		file.SeekToEnd();//find the end of the text 
		file.WriteString(buffer_to_write);//write the content of the buffer to the text 
		file.Close();//close the file 
	//	File_Process();//save the text 
	//	File_Analyse();//analyse the text 
} 
 
void Syslog::Syslog_Analyse(int x,int y){ 
	char ch=' '; 
	int k=0; 
	struct syslog_packet_message message; 
     
	ch=buffer_to_write[x]; 
	for(k=0;(ch!='>')&&(k<3);k++){//get the priority 
		switch(k){ 
	    case 0:  
			message.m_priority= ch-48; 
			break; 
		case 1:  
			message.m_priority= message.m_priority*10+ch-48; 
			break; 
		case 2:  
			message.m_priority= message.m_priority*10+ch-48; 
			break; 
		default:  
			message.m_priority=13; 
			break; 
		} 
		x=x+1; 
		ch=buffer_to_write[x]; 
	} 
 
	while((ch<'A')||((ch>'Z')&&(ch<'a'))||(ch>'z')){//skip to date 
		x=x+1; 
		ch=buffer_to_write[x]; 
	} 
 
	for(k=0;(k<6)||((ch<'A')||((ch>'Z')&&(ch<'a'))||(ch>'z'));k++){//get the timestamp 
		message.m_timestamp[k]=ch; 
		x=x+1; 
		ch=buffer_to_write[x]; 
	} 
	message.m_timestamp[k]='\0'; 
	 
	for(k=0;x<=y;k++){//get the message 
		message.m_message[k]=ch; 
		x=x+1; 
		ch=buffer_to_write[x]; 
	} 
	message.m_message[k]='\0'; 
}