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

📄 debug.c

📁 mini http server,可以集成嵌入到程序中,实现简单的web功能
💻 C
字号:
/*____________________________________________________________________________*\

 Copyright (c) 1997-2003 John Roy, Holger Zimmermann. All rights reserved.

 These sources, libraries and applications are
 FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
 as long as the following conditions are adhered to.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:

 1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer. 

 2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in
    the documentation and/or other materials provided with the
    distribution.

 3. The name of the author may not be used to endorse or promote products
    derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 IN NO EVENT SHALL THE AUTHORS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 OF THE POSSIBILITY OF SUCH DAMAGE.

 *____________________________________________________________________________*|
 *
 * $Source: /cvsroot/pi3web/Pi3Web_200/Source/Plugins/Debug.c,v $
 * $Date: 2003/05/13 18:42:14 $
 *
 Description:
	Debug handler. Writes out debugging information. Place in
	important points of request processing to get context information
	of db values etc.

\*____________________________________________________________________________*/
/* $SourceTop:$ */

#include <string.h>
#include <assert.h>
#include <stdio.h>

#include "Pi3API.h"


#define KEY_CONF_PI3EXPRESSION			"Pi3Expression"

/*____________________________________________________________________________*\
 *
 Description:
	Documentation
\*____________________________________________________________________________*/
#if 0
	/*
	** HTML documentation for this handler
	*/
/*___+++HTMLDOC_BEGIN+++___*/
Name:
	Debug

Description:
	This handler writes a configurable debug message to the debug log.

Options:
<H5>Overview</H5>
<TABLE BORDER=1>
<TH>Option
<TH>Default
<TH>Values
<TH>Short Description
<TH>Example(s)

<TR>
<TD>Pi3Expression
<TD>+
<TD>A Pi3Expression
<TD>Expression to place in debug log
<TD>Pi3Expression="$c/* value of Content-Type */"

</TABLE>
<STRONG>-</STRONG> in the <IT>default</IT> indicates no default<BR>
<STRONG>+</STRONG> in the <IT>default</IT> indicates the field is mandatory<BR>

<H4>Description of Options</H4>
<H5>
	Pi3Expression
</H5>
Pi3Expression to write in the debug log.

Phase:
	All phases

Returns:
	PIAPI_CONTINUE

Note:
Example:
	<PRE>
	&lt;Object&gt;
		Name Debug
		Class DebugClass
	&lt;/Object&gt;

	&lt;Object&gt;
		...
		Handle Debug Pi3Expression="$c/* value of Content-Type */"
		...
	&lt;/Object&gt;
	</PRE>
/*___+++HTMLDOC_END+++___*/
#endif

/*____________________________________________________________________________*\
 *
 Description:
\*____________________________________________________________________________*/
struct _Debug
{
	Pi3Expression *pInfo;
};
typedef struct _Debug Debug;

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
int Debug_fnParameter( void *pData, const char *pVar, const char *pVal,
	const char *pWhere )
{
	PIObject *pObject = (PIObject *)pData;
	Debug *pDebug = (Debug *)PIObject_getUserData( pObject );
	(void)pDebug;
	assert( pVar );
	assert( pVal );

	if ( !PIUtil_stricmp( pVar, KEY_CONF_PI3EXPRESSION ) )
		{
		Pi3String *pError;
		Pi3Expression *pExpr;
		if ( pDebug->pInfo )
			{
			PILog_addMessage( PIObject_getDB( pObject ),
				PIObject_getConfigurationDB( pObject ),
				PILOG_ERROR, "Debug: 'Pi3Expression' should only be \
specified once.", pWhere );
			return 0;
			};
		pError = Pi3String_new( 0 );
		pExpr = Pi3Expression_new( pVal, 0, pError );
		if ( !pExpr )	
			{
			PILog_addMessage( PIObject_getDB( pObject ),
				PIObject_getConfigurationDB( pObject ),
				PILOG_ERROR, "%sDebug: %s", pError, Pi3String_getPtr( pError ) );
			Pi3String_delete( pError );
			return 0;
			};
		Pi3String_delete( pError );
		pDebug->pInfo = pExpr;
		}
	else
		{
		/* --- Configuration error --- */
		PILog_addMessage( PIObject_getDB( pObject ),
			PIObject_getConfigurationDB( pObject ),
			PILOG_ERROR, "%sUnknown directive '%s'.", pWhere, pVar );
		return 0;
		};
	return 1;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
int Debug_init( Debug *pDebug, PIObject *pObject, int iArgc,
	const char *ppArgv[] )
{
	int iRet = PIObject_readParameters( pObject, iArgc, ppArgv,
		Debug_fnParameter, pObject );

	/* --- set defaults --- */
	if ( iRet )
		{
		if ( !pDebug->pInfo )	
			{
			PILog_addMessage(
				PIObject_getDB( pObject ),	
				PIObject_getConfigurationDB( pObject ),
				PILOG_ERROR, "%s", "Debug: 'Pi3Expression' not specified." );
			return 0;
			};
		};

	return iRet;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
int Debug_handle( PIHTTP *pPIHTTP, Debug *pDebug )
{
	/* ---
	Buffer for text 
	--- */
	enum { BUF_SIZE=2047 };
	char szBuf[BUF_SIZE+1];
	char *pBuf = szBuf;
	int iLen;

	assert( pDebug );
	assert( pDebug->pInfo );

	iLen = Pi3Expression_write( pDebug->pInfo, pPIHTTP, 0, pBuf, BUF_SIZE );
	if ( iLen==-1 )
		{ return PIAPI_ERROR; };
	if ( iLen>BUF_SIZE )
		{
		pBuf = PIUtil_malloc( iLen+1 );
		Pi3Expression_write( pDebug->pInfo, pPIHTTP, 0, pBuf, iLen );
		};

	pBuf[iLen] = '\0';
	HTTPCore_logDebug( DBG_HIGH, pBuf );

	if ( pBuf!=szBuf )
		{
		PIUtil_free( pBuf );
		};

	/* ---
	Return continue to allow other handlers to be invoked for this phase
	--- */
	return PIAPI_CONTINUE;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
PUBLIC_PIAPI int Debug_onClassLoad( PIClass_LoadAction eLoad, int i,
	const char *a[] )
{
	return PIAPI_COMPLETED;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
PUBLIC_PIAPI int Debug_constructor( PIObject *pObj,
	int iArgc, const char *ppArgv[] )
{
	Debug *pDebug = PIUtil_malloc( sizeof( Debug ) );
	memset( pDebug, 0, sizeof( Debug ) );
	PIObject_setUserData( pObj, pDebug );
	if ( !Debug_init( pDebug, pObj, iArgc, ppArgv ) )
		{
		return PIAPI_ERROR;
		};
	return PIAPI_COMPLETED;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
PUBLIC_PIAPI int Debug_copyConstructor( PIObject *o, int i, const char *a[] )
{
	return PIAPI_ERROR;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
PUBLIC_PIAPI int Debug_execute( PIObject *pObj,
	int iArgc, const char *ppArgv[] )
{
	PIHTTP *pPIHTTP;

	if ( iArgc<1 )
		{ return PIAPI_ERROR; };

	pPIHTTP = (PIHTTP *)*ppArgv;

	return Debug_handle( pPIHTTP, (Debug *)PIObject_getUserData( pObj ) ); 
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
PUBLIC_PIAPI int Debug_destructor( PIObject *pObj, int i, const char *a[] )
{
	Debug *pDebug = (Debug *)PIObject_getUserData( pObj );
	if ( pDebug->pInfo )
		{ Pi3Expression_delete( pDebug->pInfo ); };
	PIUtil_free( pDebug );
	return PIAPI_COMPLETED;
}

#if 0
/*___+++CNF_BEGIN+++___*/
	<Class>
		Name DebugClass
		Type LogicExtension
		Library Plugins
		OnClassLoad Debug_onClassLoad
		Constructor Debug_constructor
		CopyConstructor Debug_copyConstructor
		Destructor Debug_destructor
		Execute Debug_execute
	</Class>

	<Object>
		Name Debug
		Class DebugClass
	</Object>

/*___+++CNF_END+++___*/
#endif

⌨️ 快捷键说明

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