📄 logger_api.c
字号:
pthread_mutex_lock(&pLogInfo->Mutex);
pNode = (logger_node_t *) list_search( pLogInfo->pList, node_compare, pLogInfo->pbyName, NULL);
if( NULL == pNode )
{
stdout_printf( "mode %s not found\n", pLogInfo->pbyName );
logger_cmd_help( );
}
else if( !strcmp( "dump", pLogInfo->pbyObject ) )
{
node_info_dump( pNode->pLogCfg );
}
else if( !strcmp( "mode", pLogInfo->pbyObject ) )
{
if( !strcmp( "set", pLogInfo->pbyOp ) )
{
pNode->pLogCfg->uwModeUsed = pLogInfo->uwBitmap;
}
else
{
logger_cmd_help( );
}
}
else if( !strcmp( "debug", pLogInfo->pbyObject ) )
{
if( !strcmp( "set", pLogInfo->pbyOp ) )
{
pNode->pLogCfg->uwDbgAllSet = pLogInfo->uwBitmap;
}
else if( !strcmp( "clear", pLogInfo->pbyOp ) )
{
pNode->pLogCfg->uwDbgAllClr = pLogInfo->uwBitmap;
}
else if( !strcmp( "stdout", pLogInfo->pbyOp ) )
{
pNode->pLogCfg->uwDbgStdout = pLogInfo->uwBitmap;
}
else if( !strcmp( "logger", pLogInfo->pbyOp ) )
{
pNode->pLogCfg->uwDbgLogger = pLogInfo->uwBitmap;
}
else
{
logger_cmd_help( );
}
}
else if( !strcmp( "trace", pLogInfo->pbyObject ) )
{
if( !strcmp( "set", pLogInfo->pbyOp ) )
{
pNode->pLogCfg->uwTrcAllSet = pLogInfo->uwBitmap;
}
else if( !strcmp( "clear", pLogInfo->pbyOp ) )
{
pNode->pLogCfg->uwTrcAllClr = pLogInfo->uwBitmap;
}
else if( !strcmp( "stdout", pLogInfo->pbyOp ) )
{
pNode->pLogCfg->uwTrcStdout = pLogInfo->uwBitmap;
}
else if( !strcmp( "logger", pLogInfo->pbyOp ) )
{
pNode->pLogCfg->uwTrcLogger = pLogInfo->uwBitmap;
}
else
{
logger_cmd_help( );
}
}
else
{
logger_cmd_help( );
}
pthread_mutex_unlock(&pLogInfo->Mutex);
}
} while( !pLogInfo->quit );
stdout_printf( "logger thread quit\n" );
pthread_exit(NULL);
}
/* externals */
u32 sys_logger_create( sys_log_cfg_t * pLogCfg )
{
int ret = 0;
logger_info_t * pLogInfo;
pLogInfo = logger_info_get( );
if( NULL == pLogInfo )
{
stdout_printf( "sys_logger_create : log info get error\n" );
}
else if( NULL == pLogCfg )
{
stdout_printf( "sys_logger_create pointer null error\n" );
}
else if( NULL == pLogCfg->pbyName )
{
stdout_printf( "sys_logger_create name null error\n" );
}
else if( !pLogCfg->uwModeUsed )
{
stdout_printf( "sys_logger_create : %s mode null error\n", pLogCfg->pbyName );
}
else if( pLogCfg->uwModeUsed & SYS_LOG_FILE )
{
stdout_printf( "sys_logger_create : %s file mode not support error\n", pLogCfg->pbyName );
}
else if( pLogCfg->uwModeUsed & SYS_LOG_NET )
{
stdout_printf( "sys_logger_create : %s net mode not support error\n", pLogCfg->pbyName );
}
else
{
logger_node_t * pNode;
pthread_mutex_lock(&pLogInfo->Mutex);
pLogInfo->quit = 0;
if( NULL != list_search( pLogInfo->pList, node_compare, pLogCfg->pbyName, NULL) )
{
/* check whether or not is it already created */
stdout_printf( "sys_logger_create : %s mode exist error\n", pLogCfg->pbyName );
}
else if( NULL == (pNode = (logger_node_t *) malloc( sizeof(logger_node_t) ) ) )
{
stdout_printf( "sys_logger_create : %s node alloc error\n", pLogCfg->pbyName );
}
else if( !pLogInfo->uwNodeNum && (pthread_create( &pLogInfo->ReadThread, NULL, logger_thread, (void *) pLogInfo ) < 0) )
{
stdout_printf( "sys_logger_create : %s thread create error\n", pLogCfg->pbyName );
free( pNode );
}
else
{
pLogInfo->uwNodeNum = 1;
list_init( (list_t *) pNode );
pNode->pLogCfg = pLogCfg;
list_enqueue( &pLogInfo->pList, (list_t *) pNode );
ret = (u32) pLogCfg;
}
pthread_mutex_unlock(&pLogInfo->Mutex);
}
return ret;
}
void sys_logger_release( u32 uwHd )
{
logger_info_t * pLogInfo = logger_info_get( );
sys_log_cfg_t * pLogCfg = (sys_log_cfg_t *) uwHd;
if( NULL == pLogInfo )
{
stdout_printf( "sys_logger_release : log info get error\n" );
}
else if( NULL == pLogCfg )
{
stdout_printf( "sys_logger_release hd null error\n" );
}
else if( NULL == pLogCfg->pbyName )
{
stdout_printf( "sys_logger_release name null error\n" );
}
else
{
list_t * pNode;
pthread_mutex_lock(&pLogInfo->Mutex);
if( NULL == (pNode = list_search( pLogInfo->pList, node_compare, pLogCfg->pbyName, NULL) ) )
{
/* check whether or not is it already created */
stdout_printf( "sys_logger_release : %s node not exist error\n", pLogCfg->pbyName );
}
else if( NULL == list_extract_if_present( &pLogInfo->pList, pNode ) )
{
stdout_printf( "sys_logger_release : %s node delete error\n", pLogCfg->pbyName );
}
else if( list_empty( &pLogInfo->pList ) )
{
FILE * pFile;
pLogInfo->quit = 1;
pFile = fopen( pLogInfo->pbyFileName, "w" );
if( NULL != pFile )
{
void * pResult;
fputs( "quit", pFile );
fclose( pFile );
pthread_join( pLogInfo->ReadThread, &pResult );
}
pLogInfo->uwNodeNum = 0;
}
pthread_mutex_unlock(&pLogInfo->Mutex);
free( (void *) pNode );
}
}
void sys_logger_printf( u32 uwHd, char * pbyFormattedStr, ... )
{
sys_log_cfg_t * pLogCfg = (sys_log_cfg_t *) uwHd;
logger_info_t * pLogInfo = logger_info_get( );
if( NULL == pLogInfo )
{
printf( "logger info get error\n" );
}
else if( NULL == pLogCfg )
{
stdout_printf( "pointer null error\n" );
}
else
{
va_list va;
va_start( va, pbyFormattedStr );
pthread_mutex_lock(&pLogInfo->InfoMutex);
vsnprintf( pLogInfo->pbyInfoBuf, pLogInfo->iInfoLen - 1, pbyFormattedStr, va );
pLogInfo->pbyInfoBuf[ pLogInfo->iInfoLen - 1 ] = '\0';
va_end( va );
if( pLogCfg->uwModeUsed & SYS_LOG_STDOUT )
{
stdout_printf( "%s", pLogInfo->pbyInfoBuf );
}
if( pLogCfg->uwModeUsed & SYS_LOG_LOG )
{
syslog( pLogCfg->uwLogPriBitmap, "%s", pLogInfo->pbyInfoBuf );
}
pthread_mutex_unlock(&pLogInfo->InfoMutex);
}
return;
}
void sys_logger_trc( u32 uwHd, u32 uwLevel, char * pbyFileName, char * pbyFuncName, int iLine, char * pbyFlag )
{
sys_log_cfg_t * pLogCfg = (sys_log_cfg_t *) uwHd;
if( NULL != pLogCfg )
{
if( pLogCfg->uwModeUsed & SYS_LOG_STDOUT )
{
if( uwLevel & ((pLogCfg->uwTrcStdout | pLogCfg->uwTrcAllSet) & ~pLogCfg->uwTrcAllClr) )
{
stdout_printf( "MODE: %s, FILE: %s, FUNC: %s, Line: %d -- %s", \
pLogCfg->pbyName, base_name(pbyFileName), pbyFuncName, iLine, pbyFlag );
}
}
if( pLogCfg->uwModeUsed & SYS_LOG_LOG )
{
if( uwLevel & ((pLogCfg->uwTrcLogger | pLogCfg->uwTrcAllSet) & ~pLogCfg->uwTrcAllClr) )
{
syslog( pLogCfg->uwLogPriBitmap, "MODE: %s, FILE: %s, FUNC: %s, Line: %d -- %s", \
pLogCfg->pbyName, base_name(pbyFileName), pbyFuncName, iLine, pbyFlag );
}
}
}
return;
}
void sys_logger_dbg( u32 uwHd, u32 uwLevel, char * pbyFileName, char * pbyFuncName, int iLine, char * pbyFormattedStr, ... )
{
sys_log_cfg_t * pLogCfg = (sys_log_cfg_t *) uwHd;
logger_info_t * pLogInfo = logger_info_get( );
if( NULL == pLogInfo )
{
printf( "logger info get error\n" );
}
else if( NULL == pLogCfg )
{
stdout_printf( "pointer null error\n" );
}
else
{
char * pbyStr = pLogInfo->pbyInfoBuf;
int iLen = pLogInfo->iInfoLen - 1;
va_list va;
pthread_mutex_lock(&pLogInfo->InfoMutex);
snprintf( pbyStr, iLen, "MODE: %s, FILE: %s, FUNC: %s, Line: %d -- ", \
pLogCfg->pbyName, base_name(pbyFileName), pbyFuncName, iLine );
pbyStr += strlen( pbyStr );
iLen -= strlen( pbyStr );
va_start( va, pbyFormattedStr );
vsnprintf( pbyStr, iLen, pbyFormattedStr, va );
va_end( va );
pLogInfo->pbyInfoBuf[ pLogInfo->iInfoLen - 1 ] = '\0';
if( pLogCfg->uwModeUsed & SYS_LOG_STDOUT )
{
if( uwLevel & ((pLogCfg->uwDbgStdout | pLogCfg->uwDbgAllSet) & ~pLogCfg->uwDbgAllClr) )
{
stdout_printf( "%s", pLogInfo->pbyInfoBuf );
}
}
if( pLogCfg->uwModeUsed & SYS_LOG_LOG )
{
if( uwLevel & ((pLogCfg->uwDbgLogger | pLogCfg->uwDbgAllSet) & ~pLogCfg->uwDbgAllClr) )
{
syslog( pLogCfg->uwLogPriBitmap, "%s", pLogInfo->pbyInfoBuf );
}
}
pthread_mutex_unlock(&pLogInfo->InfoMutex);
}
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -