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

📄 errlog.c

📁 读写INI文件的函数已经日志记载
💻 C
字号:
#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <sys/stat.h>#include <time.h>#include <string.h>#define E_OK       1#define E_FAIL     -1#define FILE_SIZE_MAX 1000000#define   STR_LEN     128typedef struct {         char   year[5];         char   month[3];         char   day[3];         char   hour[3];         char   minute[3];         char   second[3];}CB_TIME;voidGet_Time_Str( char _time_str[128]  ){    CB_TIME              cb_time;    char                 str_tmp[30];    time_t               itm;    struct tm            *stm;   memset( &cb_time,0x00, sizeof( CB_TIME ) );   itm = time( 0 );   stm = localtime( &itm );   strftime( cb_time.year,  5, "%Y", stm );   strftime( cb_time.month, 3, "%m", stm );   strftime( cb_time.day,   3, "%d", stm );   strftime( cb_time.hour,  3, "%H", stm );   strftime( cb_time.minute,3, "%M", stm );   strftime( cb_time.second,3, "%S", stm );   memset( str_tmp, '*', 29 ); str_tmp[29] = '\0';   sprintf( _time_str, "%s %s/%s/%s %s:%s:%s %s\n", \            str_tmp, cb_time.year, cb_time.month, cb_time.day, \            cb_time.hour, cb_time.minute, cb_time.second, str_tmp );}voidGetTime( char _time_str[128]  ){    CB_TIME              cb_time;    char                 str_tmp[30];    time_t               itm;    struct tm            *stm;   memset( &cb_time,0x00, sizeof( CB_TIME ) );   itm = time( 0 );   stm = localtime( &itm );   strftime( cb_time.year,  5, "%Y", stm );   strftime( cb_time.month, 3, "%m", stm );   strftime( cb_time.day,   3, "%d", stm );   strftime( cb_time.hour,  3, "%H", stm );   strftime( cb_time.minute,3, "%M", stm );   strftime( cb_time.second,3, "%S", stm );   sprintf( _time_str, "%s%s%s%s%s%s", cb_time.year, cb_time.month, cb_time.day ,  \                                       cb_time.hour, cb_time.minute,cb_time.second );}voidGetYMD( char _time_str[128]  ){    CB_TIME              cb_time;    char                 str_tmp[30];    time_t               itm;    struct tm            *stm;   memset( &cb_time,0x00, sizeof( CB_TIME ) );   itm = time( 0 );   stm = localtime( &itm );   strftime( cb_time.year,  5, "%Y", stm );   strftime( cb_time.month, 3, "%m", stm );   strftime( cb_time.day,   3, "%d", stm );   strftime( cb_time.hour,  3, "%H", stm );   strftime( cb_time.minute,3, "%M", stm );   strftime( cb_time.second,3, "%S", stm );   sprintf( _time_str, "%s-%s-%s", cb_time.year, cb_time.month, cb_time.day  );}voidGet_minu( char _time_str[128]  ){    CB_TIME              cb_time;    char                 str_tmp[30];    time_t               itm;    struct tm            *stm;   memset( &cb_time,0x00, sizeof( CB_TIME ) );   itm = time( 0 );   stm = localtime( &itm );   strftime( cb_time.year,  5, "%Y", stm );   strftime( cb_time.month, 3, "%m", stm );   strftime( cb_time.day,   3, "%d", stm );   strftime( cb_time.hour,  3, "%H", stm );   strftime( cb_time.minute,3, "%M", stm );   strftime( cb_time.second,3, "%S", stm );   sprintf( _time_str, "%s", cb_time.minute  );}voidGet_hour( char _time_str[128]  ){    CB_TIME              cb_time;    char                 str_tmp[30];    time_t               itm;    struct tm            *stm;   memset( &cb_time,0x00, sizeof( CB_TIME ) );   itm = time( 0 );   stm = localtime( &itm );   strftime( cb_time.year,  5, "%Y", stm );   strftime( cb_time.month, 3, "%m", stm );   strftime( cb_time.day,   3, "%d", stm );   strftime( cb_time.hour,  3, "%H", stm );   strftime( cb_time.minute,3, "%M", stm );   strftime( cb_time.second,3, "%S", stm );   sprintf( _time_str, "%s", cb_time.hour  );}intErrlog ( char *_file, int _line, char *Err_msg ){     FILE          *Err_File;     long          File_Size;     struct stat   file_stat;     char          str_time[128];          char          yyyymmdd[32];          char          FILE_NAME[ 128 ];     memset( FILE_NAME,0x00,128 );     memset( yyyymmdd,0x00,32 );     GetYMD( yyyymmdd );     sprintf( FILE_NAME,"%s/log/Recv_Data_%s.log",getenv("HOME"),yyyymmdd );     if(( Err_File = fopen( FILE_NAME, "a+" ) ) == NULL )           return E_FAIL;     if( stat( FILE_NAME,&file_stat ) == -1 )          return E_FAIL ;     File_Size = file_stat.st_size ;     if( File_Size > FILE_SIZE_MAX )         truncate( _file,0 );     Get_Time_Str( str_time );     fprintf( Err_File,"%s\n", str_time );     fprintf( Err_File,"\nLogmsg :   [%s]\n",Err_msg );           fclose( Err_File ) ;         return E_OK;}intGetted_Log ( char *_file, int _line, char *Err_msg ){     FILE          *Err_File;     long          File_Size;     struct stat   file_stat;     char          str_time[128];          char          yyyymmdd[32];          char          FILE_NAME[ 128 ];     memset( FILE_NAME,0x00,128 );     memset( yyyymmdd,0x00,32 );     GetYMD( yyyymmdd );     sprintf( FILE_NAME,"%s/log/Getted_File_%s.log",getenv("HOME"),yyyymmdd );     if(( Err_File = fopen( FILE_NAME, "a+" ) ) == NULL )           return E_FAIL;     if( stat( FILE_NAME,&file_stat ) == -1 )          return E_FAIL ;     File_Size = file_stat.st_size ;     if( File_Size > FILE_SIZE_MAX )         truncate( _file,0 );    /***     Get_Time_Str( str_time );     fprintf( Err_File,"%s\n", str_time );    ***/     fprintf( Err_File,"[%s]\n",Err_msg );           fclose( Err_File ) ;         return E_OK;}intread_config( char *filename, char *section_name,char *val_name,char *str_ret ) {   FILE        *fp;   char        str_buf[STR_LEN],str_tmp[ STR_LEN ];   int         i,find_sec = 0 ;   char        conf_sec_name[50],conf_val_name[50];    char        *p = NULL,*p_sec = NULL;     if(( fp = fopen( filename,"r" ) ) == NULL ) {        Errlog( __FILE__,__LINE__,"fopen in read_config " ) ;        return   E_FAIL;   }      memset( str_buf ,0x00,STR_LEN  ) ;   memset( str_tmp ,0x00,STR_LEN  ) ;   while( 1 ) {              memset( conf_sec_name,0x00,50+1 );       memset( conf_val_name,0x00,50+1 );          if( fgets( str_buf,STR_LEN,fp ) == NULL ) {            Errlog( __FILE__,__LINE__,"fgets in read config " ) ;            Errlog( __FILE__,__LINE__,str_buf ) ;            break ;       }        if( str_buf[0] =='[' ) {             if(( p_sec = strchr( str_buf, ']' )) == NULL ) {                   Errlog( __FILE__,__LINE__, "strchr in read conf" );                             break;             }                strncpy( conf_sec_name,&str_buf[1] ,p_sec - &str_buf[1] );             if( strcmp(  conf_sec_name,section_name ) == 0 )                  find_sec = 1 ;       }         if( find_sec != 1 )               continue ;                    if( fgets( str_tmp, STR_LEN, fp ) == NULL ) {              Errlog( __FILE__,__LINE__," fgets in second in read conf" );              break;         }                         for( i = 0 ; i < STR_LEN ; i++ )              if( str_tmp[i] =='\n' || str_tmp[i] =='\r' )                    str_tmp[i] = '\0' ;                  while( str_tmp[0] !='[' ) {                if(( p = strchr( str_tmp,'=' )) == NULL )  {                     break;                 }                 strncpy( conf_val_name,&str_tmp[0] ,p - &str_tmp[0] );                 if( strcmp( conf_val_name,val_name ) == 0 ) {                      p = p + 1;                      strcpy( str_ret, p ) ;                       return E_OK ;                  }                            memset( str_tmp, 0x00, STR_LEN ) ;                                    if( fgets( str_tmp, STR_LEN, fp ) == NULL ) {                   Errlog( __FILE__,__LINE__," fgets in second in read conf" );                   break;                 }                              for( i = 0 ; i < STR_LEN ; i++ )                  if( str_tmp[i] =='\n' || str_tmp[i] =='\r' )                      str_tmp[i] = '\0' ;                } //while     } //while      if( find_sec == 0 )            return   E_FAIL;     return  E_OK ;}int daemon_init( void ) {    pid_t    pid ;        if( ( pid = fork( ) ) < 0 )         return  E_FAIL ;    else if( pid != 0 )      /* in parent  */         exit( 0 ) ;        /* in  child process */    setsid( ) ;    chdir( "/" ) ;    umask( 0 ) ;    return E_OK ;}

⌨️ 快捷键说明

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