home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / WEBSERVE / PI3 / PI3WEB.EXE / DEVEL / Plugins / Debug.c < prev    next >
C/C++ Source or Header  |  1997-10-19  |  9KB  |  354 lines

  1. /*____________________________________________________________________________*\
  2.  
  3.  Copyright (c) 1997 John Roy. All rights reserved.
  4.  
  5.  These sources, libraries and applications are
  6.  FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
  7.  as long as the following conditions are adhered to.
  8.  
  9.  Redistribution and use in source and binary forms, with or without
  10.  modification, are permitted provided that the following conditions
  11.  are met:
  12.  
  13.  1. Redistributions of source code must retain the above copyright
  14.     notice, this list of conditions and the following disclaimer. 
  15.  
  16.  2. Redistributions in binary form must reproduce the above copyright
  17.     notice, this list of conditions and the following disclaimer in
  18.     the documentation and/or other materials provided with the
  19.     distribution.
  20.  
  21.  3. Redistributions of any form whatsoever and all advertising materials 
  22.     mentioning features must contain the following
  23.     acknowledgment:
  24.     "This product includes software developed by John Roy
  25.     (http://www.johnroy.com/pi3/)."
  26.  
  27.  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  28.  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  29.  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  30.  IN NO EVENT SHALL THE AUTHORS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  31.  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32.  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  33.  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  34.  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  35.  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  36.  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  37.  OF THE POSSIBILITY OF SUCH DAMAGE.
  38.  
  39.  *____________________________________________________________________________*|
  40.  *
  41.  * $Source: Debug.c$
  42.  * $Date: Sun Aug 10 06:37:32 1997$
  43.  *
  44.  Description:
  45.     Debug handler. Writes out debugging information. Place in
  46.     important points of request processing to get context information
  47.     of db values etc.
  48.  
  49. \*____________________________________________________________________________*/
  50. /* $SourceTop:$ */
  51.  
  52. #include <string.h>
  53. #include <assert.h>
  54. #include <stdio.h>
  55.  
  56. #include "Pi3API.h"
  57.  
  58.  
  59. #define KEY_CONF_PI3EXPRESSION            "Pi3Expression"
  60.  
  61. /*____________________________________________________________________________*\
  62.  *
  63.  Description:
  64.     Documentation
  65. \*____________________________________________________________________________*/
  66. #if 0
  67.     /*
  68.     ** HTML documentation for this handler
  69.     */
  70. /*___+++HTMLDOC_BEGIN+++___*/
  71. Name:
  72.     Debug
  73.  
  74. Description:
  75.     This handler writes a configurable debug message to the debug log.
  76.  
  77. Options:
  78. <H5>Overview</H5>
  79. <TABLE BORDER=1>
  80. <TH>Option
  81. <TH>Default
  82. <TH>Values
  83. <TH>Short Description
  84. <TH>Example(s)
  85.  
  86. <TR>
  87. <TD>Pi3Expression
  88. <TD>+
  89. <TD>A Pi3Expression
  90. <TD>Expression to place in debug log
  91. <TD>Pi3Expression="$c/* value of Content-Type */"
  92.  
  93. </TABLE>
  94. <STRONG>-</STRONG> in the <IT>default</IT> indicates no default<BR>
  95. <STRONG>+</STRONG> in the <IT>default</IT> indicates the field is mandatory<BR>
  96.  
  97. <H4>Description of Options</H4>
  98. <H5>
  99.     Pi3Expression
  100. </H5>
  101. Pi3Expression to write in the debug log.
  102.  
  103. Phase:
  104.     All phases
  105.  
  106. Returns:
  107.     PIAPI_CONTINUE
  108.  
  109. Note:
  110. Example:
  111.     <PRE>
  112.     <Object>
  113.         Name Debug
  114.         Class DebugClass
  115.     </Object>
  116.  
  117.     <Object>
  118.         ...
  119.         Handle Debug Pi3Expression="$c/* value of Content-Type */"
  120.         ...
  121.     </Object>
  122.     </PRE>
  123. /*___+++HTMLDOC_END+++___*/
  124. #endif
  125.  
  126. /*____________________________________________________________________________*\
  127.  *
  128.  Description:
  129. \*____________________________________________________________________________*/
  130. struct _Debug
  131. {
  132.     Pi3Expression *pInfo;
  133. };
  134. typedef struct _Debug Debug;
  135.  
  136. /*____________________________________________________________________________*\
  137.  *
  138.  Function:
  139.  Synopsis:
  140.  Description:
  141. \*____________________________________________________________________________*/
  142. int Debug_fnParameter( void *pData, const char *pVar, const char *pVal,
  143.     const char *pWhere )
  144. {
  145.     PIObject *pObject = (PIObject *)pData;
  146.     Debug *pDebug = (Debug *)PIObject_getUserData( pObject );
  147.     (void)pDebug;
  148.     assert( pVar );
  149.     assert( pVal );
  150.  
  151.     if ( !PIUtil_stricmp( pVar, KEY_CONF_PI3EXPRESSION ) )
  152.         {
  153.         Pi3String *pError;
  154.         Pi3Expression *pExpr;
  155.         if ( pDebug->pInfo )
  156.             {
  157.             PILog_addMessage( PIObject_getDB( pObject ),
  158.                 PIObject_getConfigurationDB( pObject ),
  159.                 PILOG_ERROR, "Debug: 'Pi3Expression' should only be \
  160. specified once.", pWhere );
  161.             return 0;
  162.             };
  163.         pError = Pi3String_new( 0 );
  164.         pExpr = Pi3Expression_new( pVal, 0, pError );
  165.         if ( !pExpr )    
  166.             {
  167.             PILog_addMessage( PIObject_getDB( pObject ),
  168.                 PIObject_getConfigurationDB( pObject ),
  169.                 PILOG_ERROR, "%sDebug: %s", pError, Pi3String_getPtr( pError ) );
  170.             Pi3String_delete( pError );
  171.             return 0;
  172.             };
  173.         Pi3String_delete( pError );
  174.         pDebug->pInfo = pExpr;
  175.         }
  176.     else
  177.         {
  178.         /* --- Configuration error --- */
  179.         PILog_addMessage( PIObject_getDB( pObject ),
  180.             PIObject_getConfigurationDB( pObject ),
  181.             PILOG_ERROR, "%sUnknown directive '%s'.", pWhere, pVar );
  182.         return 0;
  183.         };
  184.     return 1;
  185. }
  186.  
  187. /*____________________________________________________________________________*\
  188.  *
  189.  Function:
  190.  Synopsis:
  191.  Description:
  192. \*____________________________________________________________________________*/
  193. int Debug_init( Debug *pDebug, PIObject *pObject, int iArgc,
  194.     const char *ppArgv[] )
  195. {
  196.     int iRet = PIObject_readParameters( pObject, iArgc, ppArgv,
  197.         Debug_fnParameter, pObject );
  198.  
  199.     /* --- set defaults --- */
  200.     if ( iRet )
  201.         {
  202.         if ( !pDebug->pInfo )    
  203.             {
  204.             PILog_addMessage(
  205.                 PIObject_getDB( pObject ),    
  206.                 PIObject_getConfigurationDB( pObject ),
  207.                 PILOG_ERROR, "%s", "Debug: 'Pi3Expression' not specified." );
  208.             return 0;
  209.             };
  210.         };
  211.  
  212.     return iRet;
  213. }
  214.  
  215. /*____________________________________________________________________________*\
  216.  *
  217.  Function:
  218.  Synopsis:
  219.  Description:
  220. \*____________________________________________________________________________*/
  221. int Debug_handle( PIHTTP *pPIHTTP, Debug *pDebug )
  222. {
  223.     /* ---
  224.     Buffer for text 
  225.     --- */
  226.     enum { BUF_SIZE=2047 };
  227.     char szBuf[BUF_SIZE+1];
  228.     char *pBuf = szBuf;
  229.     int iLen;
  230.  
  231.     assert( pDebug );
  232.     assert( pDebug->pInfo );
  233.  
  234.     iLen = Pi3Expression_write( pDebug->pInfo, pPIHTTP, 0, pBuf, BUF_SIZE );
  235.     if ( iLen==-1 )
  236.         { return PIAPI_ERROR; };
  237.     if ( iLen>BUF_SIZE )
  238.         {
  239.         pBuf = PIUtil_malloc( iLen+1 );
  240.         Pi3Expression_write( pDebug->pInfo, pPIHTTP, 0, pBuf, iLen );
  241.         };
  242.  
  243.     pBuf[iLen] = '\0';
  244.     HTTPCore_logDebug( DBG_HIGH, pBuf );
  245.  
  246.     if ( pBuf!=szBuf )
  247.         {
  248.         PIUtil_free( pBuf );
  249.         };
  250.  
  251.     /* ---
  252.     Return continue to allow other handlers to be invoked for this phase
  253.     --- */
  254.     return PIAPI_CONTINUE;
  255. }
  256.  
  257. /*____________________________________________________________________________*\
  258.  *
  259.  Function:
  260.  Synopsis:
  261.  Description:
  262. \*____________________________________________________________________________*/
  263. PUBLIC_PIAPI int Debug_onClassLoad( PIClass_LoadAction eLoad, int i,
  264.     const char *a[] )
  265. {
  266.     return PIAPI_COMPLETED;
  267. }
  268.  
  269. /*____________________________________________________________________________*\
  270.  *
  271.  Function:
  272.  Synopsis:
  273.  Description:
  274. \*____________________________________________________________________________*/
  275. PUBLIC_PIAPI int Debug_constructor( PIObject *pObj,
  276.     int iArgc, const char *ppArgv[] )
  277. {
  278.     Debug *pDebug = PIUtil_malloc( sizeof( Debug ) );
  279.     memset( pDebug, 0, sizeof( Debug ) );
  280.     PIObject_setUserData( pObj, pDebug );
  281.     if ( !Debug_init( pDebug, pObj, iArgc, ppArgv ) )
  282.         {
  283.         return PIAPI_ERROR;
  284.         };
  285.     return PIAPI_COMPLETED;
  286. }
  287.  
  288. /*____________________________________________________________________________*\
  289.  *
  290.  Function:
  291.  Synopsis:
  292.  Description:
  293. \*____________________________________________________________________________*/
  294. PUBLIC_PIAPI int Debug_copyConstructor( PIObject *o, int i, const char *a[] )
  295. {
  296.     return PIAPI_ERROR;
  297. }
  298.  
  299. /*____________________________________________________________________________*\
  300.  *
  301.  Function:
  302.  Synopsis:
  303.  Description:
  304. \*____________________________________________________________________________*/
  305. PUBLIC_PIAPI int Debug_execute( PIObject *pObj,
  306.     int iArgc, const char *ppArgv[] )
  307. {
  308.     PIHTTP *pPIHTTP;
  309.  
  310.     if ( iArgc<1 )
  311.         { return PIAPI_ERROR; };
  312.  
  313.     pPIHTTP = (PIHTTP *)*ppArgv;
  314.  
  315.     return Debug_handle( pPIHTTP, (Debug *)PIObject_getUserData( pObj ) ); 
  316. }
  317.  
  318. /*____________________________________________________________________________*\
  319.  *
  320.  Function:
  321.  Synopsis:
  322.  Description:
  323. \*____________________________________________________________________________*/
  324. PUBLIC_PIAPI int Debug_destructor( PIObject *pObj, int i, const char *a[] )
  325. {
  326.     Debug *pDebug = (Debug *)PIObject_getUserData( pObj );
  327.     if ( pDebug->pInfo )
  328.         { Pi3Expression_delete( pDebug->pInfo ); };
  329.     PIUtil_free( pDebug );
  330.     return PIAPI_COMPLETED;
  331. }
  332.  
  333. #if 0
  334. /*___+++CNF_BEGIN+++___*/
  335.     <Class>
  336.         Name DebugClass
  337.         Type LogicExtension
  338.         Library Plugins
  339.         OnClassLoad Debug_onClassLoad
  340.         Constructor Debug_constructor
  341.         CopyConstructor Debug_copyConstructor
  342.         Destructor Debug_destructor
  343.         Execute Debug_execute
  344.     </Class>
  345.  
  346.     <Object>
  347.         Name Debug
  348.         Class DebugClass
  349.     </Object>
  350.  
  351. /*___+++CNF_END+++___*/
  352. #endif
  353.  
  354.