You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
# include "stdafx.h"
# include "WriteLog.h"
# include "ModulePath.h"
# include <stdarg.h>
static string logFileName ;
void WriteLog ( char * format , . . . )
{
/* 初始时假设我们只需要不超过100字节大小的空间 */
int n , size = 1024 ;
char * p ;
va_list ap ;
if ( ( p = ( char * ) malloc ( size * sizeof ( char ) ) ) = = NULL )
{
return ;
}
while ( 1 )
{
/* 尝试在申请的空间中进行打印操作 */
va_start ( ap , format ) ;
n = _vsnprintf ( p , size , format , ap ) ;
va_end ( ap ) ;
/* 如果vsnprintf调用成功, 返回该字符串 */
if ( n > - 1 & & n < size )
{
if ( logFileName . empty ( ) )
{
logFileName = CModulePath : : GetCurrentModuleDirectory ( ) ;
logFileName + = " log/BlackFinder.log " ;
}
WriteLog2 ( logFileName . c_str ( ) , p , 10 , 10 * 1024 * 1024 ) ;
free ( p ) ;
return ;
}
/* vsnprintf调用失败(n<0), 或者p的空间不足够容纳size大小的字符串(n>=size),尝试申请更大的空间*/
size + = 1024 ; /* 两倍原来大小的空间 */
if ( ( p = ( char * ) realloc ( p , size * sizeof ( char ) ) ) = = NULL )
{
return ;
}
}
}