📄 snoopio.cpp
字号:
/*____________________________________________________________________________*\
*
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/IO/SnoopIO.cpp,v $
* $Date: 2003/05/13 18:42:04 $
*
Description:
Acts as a link in an IO chain and monitors construction and
destruction of IO objects as well as IO transactions.
\*____________________________________________________________________________*/
/* $SourceTop:$ */
#include <assert.h>
#include <iostream.h>
#include <time.h>
#include <ctype.h>
#include <string.h>
#include <stdio.h>
#if defined(CONFIG_83HEADERS)
# include <strstrea.h>
#else
# include <strstream.h>
#endif
#include <fstream.h>
#include "Pi2API.h"
/* --- Values from configuration file --- */
#define KEY_CONF_IOOBJECT "IOObject"
#define KEY_CONF_OUTFILE "OutFile"
#define KEY_CONF_FLAG "Flag"
#define VALUE_VERBOSE "Verbose"
/* --- other macros --- */
#define CONFIG_ERR(this, msg, arg) \
{ PILog_addMessage( PIObject_getDB(this), \
PIObject_getConfigurationDB(this), PILOG_ERROR, "%s"##msg, arg ); }
/*____________________________________________________________________________*\
Description:
Documentation for this IO object.
\*____________________________________________________________________________*/
#if 0
/*___+++HTMLDOC_BEGIN+++___*/
Name:
SnoopIO
Description:
Acts as a link in an IO chain and monitors construction and
destruction of IO objects as well as IO transactions.
Options:
<H5>Overview</H5>
<TABLE BORDER=1>
<TH>Option
<TH>Default
<TH>Values
<TH>Short Description
<TH>Example(s)
<TR>
<TD>IOObject
<TD>+
<TD>Object name
<TD>Lower level IO object
<TD>IOObject="TCPIPIOObject"
<TR>
<TD>OutFile
<TD>+
<TD><filename>
<TD>An output filename
<TD>OutFile="../Logs/snoop.txt"
<TR>
<TD>Flag
<TD>-
<TD>Verbose
<TD>A flag
<TD>Flag="Verbose"
</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>IOObject</H5>
Lower level IO object which will be loaded to monitor traffic
through this IO layer.
<H5>OutFile</H5>
A file to write IO transaction debugging information to. The path
should be absolute or relative to the current directory.
<H5>Flag</H5>
Specify a flag which effects the behaviour of this object. This
directive can be repeated multiple times to specify different flags.
The 'verbose' flag causes IO traffic to be logged in verbose format,
writing the data contents in the logfile.
Returns:
PIAPI_COMPLETED on success.
PIAPI_ERROR and PIAPI_ABORT respectively for generic and severe
error conditions.
Example:
<PRE>
<Object>
Name SnoopIO
Class SnoopIOClass
IOObject DefaultIOObject
OutFile "../Logs/snoop.log"
</Object>
</PRE>
/*___+++HTMLDOC_END+++___*/
#endif
/*____________________________________________________________________________*\
*
Class:
Description:
\*____________________________________________________________________________*/
class SnoopIO
{
private:
PIObject *pObject;
PIObject *pIOObject;
ofstream *pOfs;
ofstream *pOfsToDelete;
const char *pOutFile;
PIDB *pSaveDB; /* save initial DB */
PISync *pMutex; /* mutex to synchronize access to log */
int iPrototype; /* true if this is prototype object */
int iVerbose; /* verbose flag */
int iOK; /* success flag */
/* --- private methods --- */
ostream &Os() { assert( pOfs ); return *pOfs; };
#define SNOOP( pObj, pDesc, pBuf, iTryLen, iDoneLen, iStatus ) \
{ \
PISync_lock( pMutex ); \
Os().width( 10 ); Os() << (int)(PIThread_getSystemHandle(PIThread_getCurrent())); \
/* \
time_t __tT; time( &__tT ); \
Os().width( 12 ); Os() << (unsigned long)__tT << ":"; \
Os().flags( ios::right ); \
Os().width( 12 ); Os() << (void *)pObj; \
*/ \
Os().width( 5 ); Os() << pDesc << " "; \
Os().width( 12 ); Os() << pBuf << " "; \
Os().width( 7 ); Os() << iTryLen; \
Os().width( 7 ); Os() << iDoneLen; \
Os().width( 5 ); Os() << iStatus; \
Os() << endl; \
PISync_unlock( pMutex ); \
}
int Parameter( const char *pVar, const char *pVal, const char *pWhere )
{
assert( pVar && pVal );
if ( !( pVar && pVal ) )
{ return 0; };
if ( !PIUtil_stricmp( pVar, KEY_CONF_IOOBJECT ) )
{
pIOObject = PIObject_loadFromLine(
PIObject_getDB( pObject ),
PIObject_getConfigurationDB( pObject ),
pVal );
if ( !pIOObject )
{ return 0; };
}
else if ( !PIUtil_stricmp( pVar, KEY_CONF_OUTFILE ) )
{
pOutFile = pVal;
pOfs = new ofstream( pOutFile );
if ( !pOfs->good() ) { delete pOfs; pOfs=0; };
pOfsToDelete = pOfs;
if ( !pOfs )
{
CONFIG_ERR( pObject, "SnoopIO: output file could not \
be opened.", pWhere );
return 0;
};
}
else if ( !PIUtil_stricmp( pVar, KEY_CONF_FLAG ) )
{
if ( !PIUtil_stricmp( pVal, VALUE_VERBOSE ) )
{ iVerbose = 1; }
else
{
ostrstream os;
os << pWhere << "SnoopIO: Unknown flag '" << pVal << "'"
<< ends;
char *pPtr = os.str();
CONFIG_ERR( pObject, "", pPtr );
delete [] pPtr;
return 0;
};
}
else
{
ostrstream os;
os << pWhere << "SnoopIO: Unknown parameter '" << pVar << "'"
<< ends;
char *pPtr = os.str();
CONFIG_ERR( pObject, "", pPtr );
delete [] pPtr;
return 0;
};
return 1;
};
static int ParameterFn( void *pData, const char *pVar, const char *pVal,
const char *pWhere )
{ return ((SnoopIO *)pData)->Parameter( pVar, pVal, pWhere ); };
/* --- */
void WriteVerboseLine( int iOffset, const char *pData, int iLen )
{
/* Here's what the line looks like:
0000: 12 34 45 98 AB 34 45 23 23 45 A0 FF FE 23 23 12 .A.s.ewdfdefsa.a
character postitions:
| | | | | | |
0123456789012345678901234567890123456789012345678901234567890123456789012
111111111122222222223333333333444444444455555555556666666666777
*/
enum { BUF_SIZE=128 };
char szBuf[BUF_SIZE+1];
unsigned char sV[16];
memcpy( sV, pData, iLen );
sprintf( szBuf, "%04X: %02X %02X %02X %02X %02X %02X %02X %02X ",
iOffset, sV[0], sV[1], sV[2], sV[3], sV[4], sV[5], sV[6], sV[7] );
sprintf( &(szBuf[32]), "%02X %02X %02X %02X %02X %02X %02X %02X ",
sV[8], sV[9], sV[10], sV[11], sV[12], sV[13], sV[14], sV[15] );
# define C(x) ((isprint(sV[x]))?sV[x]:'.')
sprintf( &(szBuf[57]), "%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",
C(0),C(1),C(2),C(3),C(4),C(5),C(6),C(7),
C(8),C(9),C(10),C(11),C(12),C(13),C(14),C(15) );
# undef C
int iToBlank = 16 - iLen;
if ( iToBlank )
{
/* --- blank out invalid values at end of list --- */
memset( &(szBuf[55-(iToBlank*3)]), ' ', iToBlank*3 );
memset( &(szBuf[73-iToBlank]), ' ', iToBlank );
};
Os() << szBuf << endl;
};
/* --- */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -