/* 输出日志 */ int WriteLogBaseV( int log_level , char *c_filename , long c_fileline , char *format , va_list valist ) { char c_filename_copy[ MAXLEN_FILENAME + 1 ] ; char *p_c_filename = NULL ; char log_buffer[ 1024 + 1 ] ; char *log_bufptr = NULL ; size_t log_buflen ; size_t log_buf_remain_len ; size_t len ; int nret = 0 ; /* 处理源代码文件名 */ memset( c_filename_copy , 0x00 , sizeof(c_filename_copy) ); strncpy( c_filename_copy , c_filename , sizeof(c_filename_copy)-1 ); p_c_filename = strrchr( c_filename_copy , '\\' ) ; if( p_c_filename ) p_c_filename++; else p_c_filename = c_filename_copy ; /* 填充行日志 */ /* memset( log_buffer , 0x00 , sizeof(log_buffer) ); */ log_bufptr = log_buffer ; log_buflen = 0 ; log_buf_remain_len = sizeof(log_buffer) - 1 - sizeof(NEWLINE) ; /* { struct timeval tv ; gettimeofday( & tv , NULL ); len = SNPRINTF( log_bufptr , log_buf_remain_len , "%s.%06ld | %-5s | %lu:%lu:%s:%ld | " , g_date_time_cache[g_date_time_cache_index].date_and_time_str , tv.tv_usec , log_level_itoa[log_level] , g_pid , g_tid , p_c_filename , c_fileline ) ; } */ len = SNPRINTF( log_bufptr , log_buf_remain_len , "%s | %-5s | %lu:%lu:%s:%ld | " , g_date_time_cache[g_date_time_cache_index].date_and_time_str , log_level_itoa[log_level] , g_pid , g_tid , p_c_filename , c_fileline ) ; OFFSET_BUFPTR( log_buffer , log_bufptr , len , log_buflen , log_buf_remain_len ); len = VSNPRINTF( log_bufptr , log_buf_remain_len , format , valist ); OFFSET_BUFPTR( log_buffer , log_bufptr , len , log_buflen , log_buf_remain_len ); log_buf_remain_len += sizeof(NEWLINE) ; len = SNPRINTF( log_bufptr , log_buf_remain_len , NEWLINE ) ; OFFSET_BUFPTR( log_buffer , log_bufptr , len , log_buflen , log_buf_remain_len ); /* 输出行日志 */ if( g_file_fd == -1 ) { nret = OpenLogFile() ; if( nret ) return nret; WRITE( g_file_fd , log_buffer , log_buflen ); } else { WRITE( g_file_fd , log_buffer , log_buflen ); } return 0; }
/* 输出日志 */ static int WriteLogBase( int log_level , char *c_filename , long c_fileline , char *format , va_list valist ) { char c_filename_copy[ MAXLEN_FILENAME + 1 ] ; char *p_c_filename = NULL ; struct timeval tv ; struct tm stime ; char log_buffer[ 1024 + 1 ] ; char *log_bufptr = NULL ; size_t log_buflen ; size_t log_buf_remain_len ; size_t len ; /* 处理源代码文件名 */ memset( c_filename_copy , 0x00 , sizeof(c_filename_copy) ); strncpy( c_filename_copy , c_filename , sizeof(c_filename_copy)-1 ); p_c_filename = strrchr( c_filename_copy , '\\' ) ; if( p_c_filename ) p_c_filename++; else p_c_filename = c_filename_copy ; /* 填充行日志 */ #if ( defined __linux__ ) || ( defined __unix ) || ( defined _AIX ) gettimeofday( & tv , NULL ); localtime_r( &(tv.tv_sec) , & stime ); #elif ( defined _WIN32 ) { SYSTEMTIME stNow ; GetLocalTime( & stNow ); tv.tv_usec = stNow.wMilliseconds * 1000 ; stime.tm_year = stNow.wYear - 1900 ; stime.tm_mon = stNow.wMonth - 1 ; stime.tm_mday = stNow.wDay ; stime.tm_hour = stNow.wHour ; stime.tm_min = stNow.wMinute ; stime.tm_sec = stNow.wSecond ; } #endif memset( log_buffer , 0x00 , sizeof(log_buffer) ); log_bufptr = log_buffer ; log_buflen = 0 ; log_buf_remain_len = sizeof(log_buffer) - 1 ; len = strftime( log_bufptr , log_buf_remain_len , "%Y-%m-%d %H:%M:%S" , & stime ) ; OFFSET_BUFPTR( log_buffer , log_bufptr , len , log_buflen , log_buf_remain_len ); len = SNPRINTF( log_bufptr , log_buf_remain_len , ".%06ld" , (long)(tv.tv_usec) ) ; OFFSET_BUFPTR( log_buffer , log_bufptr , len , log_buflen , log_buf_remain_len ); len = SNPRINTF( log_bufptr , log_buf_remain_len , " | %-5s" , log_level_itoa[log_level] ) ; OFFSET_BUFPTR( log_buffer , log_bufptr , len , log_buflen , log_buf_remain_len ); len = SNPRINTF( log_bufptr , log_buf_remain_len , " | %lu:%lu:%s:%ld | " , PROCESSID , THREADID , p_c_filename , c_fileline ) ; OFFSET_BUFPTR( log_buffer , log_bufptr , len , log_buflen , log_buf_remain_len ); len = VSNPRINTF( log_bufptr , log_buf_remain_len , format , valist ); OFFSET_BUFPTR( log_buffer , log_bufptr , len , log_buflen , log_buf_remain_len ); len = SNPRINTF( log_bufptr , log_buf_remain_len , NEWLINE ) ; OFFSET_BUFPTR( log_buffer , log_bufptr , len , log_buflen , log_buf_remain_len ); /* 输出行日志 */ if( STRCMP( g_log_pathfilename , == , "#stdout" ) ) { printf( "%.*s" , (int)log_buflen , log_buffer ); } else if( g_log_pathfilename[0] )
int WriteHexLogBaseV( int log_level , char *c_filename , long c_fileline , char *buf , long buflen , char *format , va_list valist ) { char hexlog_buffer[ 4096 * 10 + 1 ] ; char *hexlog_bufptr = NULL ; size_t hexlog_buflen ; size_t hexlog_buf_remain_len ; size_t len ; int row_offset , col_offset ; int nret = 0 ; if( buf == NULL && buflen <= 0 ) return 0; if( buflen > sizeof(hexlog_buffer) - 1 ) return -1; /* 输出行日志 */ if( format ) { WriteLogBaseV( log_level , c_filename , c_fileline , format , valist ); } /* 填充十六进制块日志 */ memset( hexlog_buffer , 0x00 , sizeof(hexlog_buffer) ); hexlog_bufptr = hexlog_buffer ; hexlog_buflen = 0 ; hexlog_buf_remain_len = sizeof(hexlog_buffer) - 1 ; len = SNPRINTF( hexlog_bufptr , hexlog_buf_remain_len , " 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF" ) ; OFFSET_BUFPTR( hexlog_buffer , hexlog_bufptr , len , hexlog_buflen , hexlog_buf_remain_len ); len = SNPRINTF( hexlog_bufptr , hexlog_buf_remain_len , NEWLINE ) ; OFFSET_BUFPTR( hexlog_buffer , hexlog_bufptr , len , hexlog_buflen , hexlog_buf_remain_len ); row_offset = 0 ; col_offset = 0 ; while(1) { len = SNPRINTF( hexlog_bufptr , hexlog_buf_remain_len , "0x%08X " , row_offset * 16 ) ; OFFSET_BUFPTR( hexlog_buffer , hexlog_bufptr , len , hexlog_buflen , hexlog_buf_remain_len ); for( col_offset = 0 ; col_offset < 16 ; col_offset++ ) { if( row_offset * 16 + col_offset < buflen ) { len = SNPRINTF( hexlog_bufptr , hexlog_buf_remain_len , "%02X " , *((unsigned char *)buf+row_offset*16+col_offset)) ; OFFSET_BUFPTR( hexlog_buffer , hexlog_bufptr , len , hexlog_buflen , hexlog_buf_remain_len ); } else { len = SNPRINTF( hexlog_bufptr , hexlog_buf_remain_len , " " ) ; OFFSET_BUFPTR( hexlog_buffer , hexlog_bufptr , len , hexlog_buflen , hexlog_buf_remain_len ); } } len = SNPRINTF( hexlog_bufptr , hexlog_buf_remain_len , " " ) ; OFFSET_BUFPTR( hexlog_buffer , hexlog_bufptr , len , hexlog_buflen , hexlog_buf_remain_len ); for( col_offset = 0 ; col_offset < 16 ; col_offset++ ) { if( row_offset * 16 + col_offset < buflen ) { if( isprint( (int)*(buf+row_offset*16+col_offset) ) ) { len = SNPRINTF( hexlog_bufptr , hexlog_buf_remain_len , "%c" , *((unsigned char *)buf+row_offset*16+col_offset) ) ; OFFSET_BUFPTR( hexlog_buffer , hexlog_bufptr , len , hexlog_buflen , hexlog_buf_remain_len ); } else { len = SNPRINTF( hexlog_bufptr , hexlog_buf_remain_len , "." ) ; OFFSET_BUFPTR( hexlog_buffer , hexlog_bufptr , len , hexlog_buflen , hexlog_buf_remain_len ); } } else { len = SNPRINTF( hexlog_bufptr , hexlog_buf_remain_len , " " ) ; OFFSET_BUFPTR( hexlog_buffer , hexlog_bufptr , len , hexlog_buflen , hexlog_buf_remain_len ); } } len = SNPRINTF( hexlog_bufptr , hexlog_buf_remain_len , NEWLINE ) ; OFFSET_BUFPTR( hexlog_buffer , hexlog_bufptr , len , hexlog_buflen , hexlog_buf_remain_len ); if( row_offset * 16 + col_offset >= buflen ) break; row_offset++; } /* 输出十六进制块日志 */ if( g_file_fd == -1 ) { nret = OpenLogFile() ; if( nret ) return nret; WRITE( g_file_fd , hexlog_buffer , hexlog_buflen ); } else { WRITE( g_file_fd , hexlog_buffer , hexlog_buflen ); } return 0; }