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

📄 logger_api.c

📁 one tool used to control the real time logger
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <stdarg.h>
#include <pthread.h>
#include <asm/page.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <syslog.h>

#include <linux/stat.h>
#include <linux/unistd.h>

#include <linux/mman.h>

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <linux/ioctl.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>

#include <sys_typedef.h>
#include <sys_logger.h>
#include <misc.h>
#include <list.h>

#define FIFO_FILE 		"/var/logfifo"
#define READ_BUF_LEN	0x100

#define STR_BUF_LEN		0x200

typedef struct logger_node_s {
	list_t List;
	sys_log_cfg_t * pLogCfg;
} logger_node_t;

typedef struct logger_info_s {
	int init;
	pthread_t ReadThread;

	char pbyInfoBuf[ STR_BUF_LEN ];
	int iInfoLen;
	pthread_mutex_t	InfoMutex;
	
	/* used to parse the command */
	char * pbyName;
	char * pbyObject;
	char * pbyOp;
	u32 uwBitmap;
	
	
	/* shared with the thread */
	int quit;
	char * pbyFileName;
	char pbyBuf[ READ_BUF_LEN ];
	int iBufLen;
	
	list_t * pList;
	pthread_mutex_t	Mutex;
	u32 uwNodeNum;
} logger_info_t;

static logger_info_t s_LogInfo = {
	init          : 0,
	quit          : 0,
	iInfoLen      : STR_BUF_LEN,
 	pList         : NULL,
 	pbyFileName   : FIFO_FILE,
	iBufLen       : READ_BUF_LEN,
	uwNodeNum     : 0,
};

/* locals */
static void stdout_printf( char * pbyFormattedStr, ... );
static void node_info_dump( sys_log_cfg_t * pLogCfg );
static logger_info_t * logger_info_get( void );
static int node_compare(list_t *pElem, void *pUserDefined1, void *pUserDefined2);
static int logger_cmd_help( void );
static int logger_cmd_parse( char * pbyCmd );
static void * logger_thread( void *arg );

/* locals */

static void stdout_printf( char * pbyFormattedStr, ... )
{
    va_list va;
    va_start( va, pbyFormattedStr );
    vfprintf( stderr, pbyFormattedStr, va );
    va_end( va );
    return;
}

static void node_info_dump( sys_log_cfg_t * pLogCfg )
{
	stdout_printf( "logger node name       : %s\n", pLogCfg->pbyName );
	stdout_printf( "logger node mode       : 0x%08x\n", pLogCfg->uwModeUsed );
	stdout_printf( "logger node dbg set    : 0x%08x\n", pLogCfg->uwDbgAllSet );
	stdout_printf( "logger node dbg clr    : 0x%08x\n", pLogCfg->uwDbgAllClr );	
	stdout_printf( "logger node trc set    : 0x%08x\n", pLogCfg->uwTrcAllSet );
	stdout_printf( "logger node trc clr    : 0x%08x\n", pLogCfg->uwTrcAllClr );

	stdout_printf( "logger node dbg stdout : 0x%08x\n", pLogCfg->uwDbgStdout );
	stdout_printf( "logger node trc stdout : 0x%08x\n", pLogCfg->uwTrcStdout );
	
	stdout_printf( "logger node dbg log    : 0x%08x\n", pLogCfg->uwDbgLogger );
	stdout_printf( "logger node trc log    : 0x%08x\n", pLogCfg->uwTrcLogger );
	stdout_printf( "logger node log pri    : 0x%08x\n", pLogCfg->uwLogPriBitmap );	
}

static logger_info_t * logger_info_get( void )
{
	logger_info_t * pLogInfo = &s_LogInfo;
	if( pLogInfo->init )
	{
		return pLogInfo;
	}
	else
	{
		/* Create the FIFO if it does not exist */
		umask( 0 );
		mknod( pLogInfo->pbyFileName, S_IFIFO | 0666, 0 );

		pLogInfo->init = 1;
		pLogInfo->quit = 0;
		if( pthread_mutex_init( &pLogInfo->Mutex, NULL ) )
		{
			stdout_printf( "mutex create failed\n" );
			return NULL;
		}

		if( pthread_mutex_init( &pLogInfo->InfoMutex, NULL ) )
		{
			stdout_printf( "mutex create failed\n" );
			return NULL;
		}		
		
		return pLogInfo;
	}
}

static int node_compare(list_t *pElem, void *pUserDefined1, void *pUserDefined2)
{
	if( NULL != pElem )
	{
		logger_node_t * pNode = (logger_node_t *) pElem;
		if( !strcmp( pNode->pLogCfg->pbyName, (char *) pUserDefined1 ) )
		{
			return (1==1);
		}
	}
	
	return (1==0);
}

static int logger_cmd_help( void )
{
	logger_info_t * pLogInfo = logger_info_get( );
	if( NULL != pLogInfo )
	{
		stdout_printf( "the logger command : \n" );
		stdout_printf( "    echo \"mode_name object_name operation bitmap\">%s\n", pLogInfo->pbyFileName );
		stdout_printf( "Currently supported commands are : \n" );
		
		stdout_printf( "ech \"total dump\">%s\n", pLogInfo->pbyFileName );
		
		stdout_printf( "ech \"name dump\">%s\n", pLogInfo->pbyFileName );
		stdout_printf( "ech \"name mode   set     mode_bitmap\">%s\n", pLogInfo->pbyFileName );
		
		stdout_printf( "ech \"name debug  set     debug_set_bitmap\">%s\n", pLogInfo->pbyFileName );
		stdout_printf( "ech \"name debug  clear   debug_clr_bitmap\">%s\n", pLogInfo->pbyFileName );
		stdout_printf( "ech \"name debug  stdout  debug_stdout_bitmap\">%s\n", pLogInfo->pbyFileName );
		stdout_printf( "ech \"name debug  logger  debug_logger_bitmap\">%s\n", pLogInfo->pbyFileName );
		
		stdout_printf( "ech \"name trace  set     trace_set_bitmap\">%s\n", pLogInfo->pbyFileName );
		stdout_printf( "ech \"name trace  clear   trace_clr_bitmap\">%s\n", pLogInfo->pbyFileName );
		stdout_printf( "ech \"name trace  stdout  trace_stdout_bitmap\">%s\n", pLogInfo->pbyFileName );
		stdout_printf( "ech \"name trace  logger  trace_logger_bitmap\">%s\n", pLogInfo->pbyFileName );
	}	
	
}
/*
 * The format of the command is as follows: 
 * > name dump
 *
 * > name mode   set     mode_bitmap
 *
 * > name debug  set     debug_set_bitmap
 * > name debug  clear   debug_clr_bitmap
 * > name debug  stdout  debug_stdout_bitmap
 * > name debug  logger  debug_logger_bitmap
 *
 * > name trace  set     trace_set_bitmap
 * > name trace  clear   trace_clr_bitmap
 * > name trace  stdout  trace_stdout_bitmap
 * > name trace  logger  trace_logger_bitmap
 */
static int logger_cmd_parse( char * pbyCmd )
{
	int ret = -1;
	logger_info_t * pLogInfo = logger_info_get( );
	if( NULL != pLogInfo )
	{
		/* strip the '\r' and '\n' */
		char * pbyStr = pbyCmd;
		while( '\0' != *pbyStr )
		{
			if( ('\r' == *pbyStr) || ('\n' == *pbyStr) ) 
			{
				*pbyStr = '\0';
				break;
			}
			pbyStr++;	
		}
		/* parse for the name */
		while( ' ' == *pbyCmd ) pbyCmd++;
		pLogInfo->pbyName = pbyCmd;
		while( ('\0' != *pbyCmd) && (' ' != *pbyCmd) ) pbyCmd++;
		if( ' ' == *pbyCmd ) *pbyCmd++ = '\0';

		/* parse for the object */
		while( ' ' == *pbyCmd ) pbyCmd++;
		pLogInfo->pbyObject = pbyCmd;
		while( ('\0' != *pbyCmd) && (' ' != *pbyCmd) ) pbyCmd++;
		if( ' ' == *pbyCmd ) *pbyCmd++ = '\0';
		

		/* parse for the operation */
		while( ' ' == *pbyCmd ) pbyCmd++;
		pLogInfo->pbyOp = pbyCmd;
		while( ('\0' != *pbyCmd) && (' ' != *pbyCmd) ) pbyCmd++;
		if( ' ' == *pbyCmd ) *pbyCmd++ = '\0';
		
		
		/* parse for the bitmap */
		while( ' ' == *pbyCmd ) pbyCmd++;
		pLogInfo->uwBitmap = get_hex_number( pbyCmd );
	
		ret = 0;
	}
	
	return ret;
}

static void * logger_thread( void *arg )
{
	logger_info_t * pLogInfo = (logger_info_t *) arg;
	
	if( 0 != pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) )
	{
		stdout_printf( "logger thread: pthread_setcancelstate() failed\n" );
		pthread_exit(NULL);
	}

	if( 0 != pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL) )
	{
		stdout_printf( "logger thread: pthread_setcanceltype() failed \n" );
		pthread_exit(NULL);
	}

	/* waiting for the sys init to finish */
	do {
		FILE * pFile;
		thread_sleep( 0, 200000 );
		
		pFile = fopen( pLogInfo->pbyFileName, "r" );
		if( NULL == pFile )
		{
			stdout_printf( "logger file %s open error\n", pLogInfo->pbyFileName );
			break;
		}
		
		fgets( pLogInfo->pbyBuf, pLogInfo->iBufLen, pFile );
		if( pLogInfo->quit )
		{
			break;
		}
		pLogInfo->pbyBuf[ pLogInfo->iBufLen - 1 ] = '\0';
		stdout_printf( "logger read : %s\n", pLogInfo->pbyBuf );
		fclose( pFile );
		if( logger_cmd_parse( pLogInfo->pbyBuf ) )
		{
			stdout_printf( "logger cmd partse error\n" );
			continue;
		}
		
		/* treat the command */
		if( NULL == pLogInfo->pList )
		{
			stdout_printf( "The logger does not have any valid node\n" );
		}
		else if( !strcmp( "total", pLogInfo->pbyName ) )
		{
			if( !strcmp( "dump", pLogInfo->pbyObject ) )
			{
				void * pElement;
				
				stdout_printf( "The logger has the following nodes:\n" );
				pthread_mutex_lock(&pLogInfo->Mutex);
				LIST_FOR_EACH(pLogInfo->pList, pElement)
				{
					logger_node_t * pNode = (logger_node_t *) pElement;
					stdout_printf( "    %s\n", pNode->pLogCfg->pbyName );
				}
				pthread_mutex_unlock(&pLogInfo->Mutex);
			}
		}
		else
		{
			logger_node_t * pNode;

⌨️ 快捷键说明

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