⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aid_func.c

📁 socket编程
💻 C
字号:
#include <time.h>
#include <stdio.h>
#include "aid_func.h"

void TraceLog_recv( struct PG_HEAD  * pg_head , char * p_data)
{
	struct DATA_BIN data_bin ;
    time_t temp ;
    struct tm *pTime;
    char buff[1024];
    FILE * fp ;
	int i=0;/* char index*/
	
    time( &temp );
    pTime = localtime( &temp ) ;
	
    switch( pg_head->reqType )
    {
    case DATA_PACKAGE   :
        sprintf( buff , "[%04d/%02d/%02d %02dh:%02dm:%02ds] get the data from client  : %s\n" ,
			pTime->tm_year+1900 , pTime->tm_mon , pTime->tm_mday ,
			pTime->tm_hour , pTime->tm_min , pTime->tm_sec , p_data ) ;
        break ;
		
    case ENQ_PACKAGE :
        sprintf( buff , "[%04d/%02d/%02d %02dh:%02dm:%02ds] get the response from client  : ENQ_PACKAGE\n" ,
			pTime->tm_year+1900 , pTime->tm_mon , pTime->tm_mday ,
			pTime->tm_hour , pTime->tm_min , pTime->tm_sec , p_data ) ;
        break ;
		
    case OK_PACKAGE :
        sprintf( buff , "[%04d/%02d/%02d %02dh:%02dm:%02ds] get the response from client  : OK_PACKAGE\n" ,
			pTime->tm_year+1900 , pTime->tm_mon , pTime->tm_mday ,
			pTime->tm_hour , pTime->tm_min , pTime->tm_sec , p_data ) ;
        break ;
		
    case NG_PACKAGE :
        sprintf( buff , "[%04d/%02d/%02d %02dh:%02dm:%02ds] get the response from client  : NG_PACKAGE\n" ,
			pTime->tm_year+1900 , pTime->tm_mon , pTime->tm_mday ,
			pTime->tm_hour , pTime->tm_min , pTime->tm_sec , p_data ) ;
        break ;
		
    default :
        sprintf( buff , "[%04d/%02d/%02d %02dh:%02dm:%02ds] get the request code from client  : UNKNOWN PACKAGE\n" ,
			
			pTime->tm_year+1900 , pTime->tm_mon , pTime->tm_mday ,
			pTime->tm_hour , pTime->tm_min , pTime->tm_sec ) ;
        break;
    }
	
    fp = fopen("history.log","r+t") ;
    if ( fp == NULL )
    {
        fp = fopen("history.log","w+t") ;
		
        if ( fp == NULL )
        {
            printf("Can not open this log file : history.log!\n");
            return ;
        }
    }
	
    fseek( fp, 0 , SEEK_END );
	
    fwrite( buff , strlen(buff) , 1 , fp ) ;
    fclose(fp);
	
	
    switch( pg_head->reqType )
    {
    case DATA_PACKAGE   :
		if(pg_head->binType == 1)/* bin mode */
		{
			fp = fopen("data.bin","r+b") ;
			if ( fp == NULL )
			{
				fp = fopen("data.bin","w+b") ;
				
				if ( fp == NULL )
				{
					printf("Can not open this log file : data.bin!\n");
					return ;
				}
			}
			
			memset( &data_bin , '\0' , sizeof(data_bin) ) ;
			data_bin.w_len = strlen(p_data);
			memcpy(data_bin.byte , p_data , strlen(p_data) ) ;

			fseek( fp, 0 , SEEK_END );
			
			fwrite( &data_bin, sizeof(data_bin) , 1 , fp ) ;
			TraceLog_str("Write to data.bin successfully.");
			fclose(fp);
		}
		else if(pg_head->binType == 0)/* text mode */
		{
			fp = fopen("data.txt","r+t") ;
			if ( fp == NULL )
			{
				fp = fopen("data.txt","w+t") ;
				
				if ( fp == NULL )
				{
					printf("Can not open this log file : data.txt!\n");
					return ;
				}
			}
			
			sprintf(buff,"%s\n",p_data) ;
			fseek( fp, 0 , SEEK_END );
			
			fwrite( buff, strlen(buff) , 1 , fp ) ;
			
			TraceLog_str("Write to data.txt successfully.");
			fclose(fp);
			
		}
		else { printf("error 1\n");}
        break ;
	}
	
}

void TraceLog_send( struct PG_HEAD  * pg_head , char * p_data)
{
	time_t temp ;
	struct tm *pTime;
	char buff[1024];
	FILE * fp ;
	
	time( &temp );
	pTime = localtime( &temp ) ;
	
	switch( pg_head->reqType )
	{
	case DATA_PACKAGE	:
		sprintf( buff , "[%04d/%02d/%02d %02dh:%02dm:%02ds] send the data to client  : %s\n" , 
			pTime->tm_year+1900 , pTime->tm_mon , pTime->tm_mday ,
			pTime->tm_hour , pTime->tm_min , pTime->tm_sec , p_data ) ;
		break ;
		
	case OK_PACKAGE :
		sprintf( buff , "[%04d/%02d/%02d %02dh:%02dm:%02ds] send the response to client  : OK_PACKAGE\n" , 
			pTime->tm_year+1900 , pTime->tm_mon , pTime->tm_mday ,
			pTime->tm_hour , pTime->tm_min , pTime->tm_sec , p_data ) ;
		break ;
		
	case NG_PACKAGE :
		sprintf( buff , "[%04d/%02d/%02d %02dh:%02dm:%02ds] send the response to client  : NG_PACKAGE\n" , 
			pTime->tm_year+1900 , pTime->tm_mon , pTime->tm_mday ,
			pTime->tm_hour , pTime->tm_min , pTime->tm_sec , p_data ) ;
		break ;
		
	default :
		sprintf( buff , "[%04d/%02d/%02d %02dh:%02dm:%02ds] send the request code to client  : UNKNOWN PACKAGE\n" , 
			pTime->tm_year+1900 , pTime->tm_mon , pTime->tm_mday ,
			pTime->tm_hour , pTime->tm_min , pTime->tm_sec ) ;
		break;
	}
	
	fp = fopen("history.log","r+t") ;
	if ( fp == NULL )
	{
		fp = fopen("history.log","w+t") ;
		
		if ( fp == NULL )
		{
			printf("Can not open this log file : history.log!\n");
			return ;
		}
	}
	
	fseek( fp, 0 , SEEK_END );
	
	fwrite( buff , strlen(buff) , 1 , fp ) ;
	
	fclose(fp);
}

void TraceLog_str( char * pMsg )
{
	time_t temp ;
	struct tm *pTime;
	char buff[256];
	FILE * fp ;
	
	time( &temp );
	pTime = localtime( &temp ) ;
	sprintf( buff , "[%04d/%02d/%02d %02dh:%02dm:%02ds]  result : %s\n" , 
		pTime->tm_year+1900 , pTime->tm_mon , pTime->tm_mday ,
		pTime->tm_hour , pTime->tm_min , pTime->tm_sec , pMsg ) ;
	
	printf("%s",buff) ;
	
	fp = fopen("history.log","r+t") ;
	if ( fp == NULL )
	{
		fp = fopen("history.log","w+t") ;
		
		if ( fp == NULL )
		{
			printf("Can not open this log file : history.log!\n");
			return ;
		}
	}
	
	fseek( fp, 0 , SEEK_END );
	
	fwrite( buff , strlen(buff) , 1 , fp ) ;
	
	fclose(fp);
}

void TraceLog_clear(int binType)
{
    FILE * fp ;
    fp = fopen("history.log","w+t") ;
	
    if ( fp == NULL )
    {
        printf("Can not open this log file : history.log!\n");
        return ;
    }
	
	fclose(fp);
	/*if(binType==0)*/ /*text mode*/
	/*{*/
		fp = fopen("data.txt","w+t") ;
		
		if ( fp == NULL )
		{
			printf("Can not open this log file : data.txt!\n");
			return ;
		}
		TraceLog_str("Clear data.txt\n");
		fclose(fp);
	/*}*/
	/*else if(binType==1)*//*bin mode*/
	/*{*/
		fp = fopen("data.bin","w+b") ;
		
		if ( fp == NULL )
		{
			printf("Can not open this log file : data.bin\n");
			return ;
		}
		TraceLog_str("Clear data.bin\n");
		fclose(fp);
		
	/*}
	else{TraceLog_str("error_3\n");}*/
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -