1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
/*
* Modification History
*
* 2004-January-26 Jason Rohrer
* Created.
*
* 2004-January-27 Jason Rohrer
* Changed to support externally-specified file name prefixes.
*/
#include "minorGems/common.h"
#ifndef LOGGING_SOCKET_STREAM_CLASS_INCLUDED
#define LOGGING_SOCKET_STREAM_CLASS_INCLUDED
#include "SocketStream.h"
#include "minorGems/util/stringUtils.h"
#include <stdio.h>
/**
* A SocketStream that logs inbound and outbound data.
*
* @author Jason Rohrer
*/
class LoggingSocketStream : public SocketStream {
public:
/**
* Constructs a SocketStream.
*
* @param inSocket the newtork socket wrapped by this stream.
* inSocket is NOT destroyed when the stream is destroyed.
* @param inEnableInboundLog set to true to enable inbound logging.
* @param inEnableOutboundLog set to true to enable outbound logging.
* @param inLogSizeLimit the size limit of each log (both inbound
* and outbound), in bytes.
* Logging will stop after inLogSizeLimit bytes have been
* written to each log.
* @param inLogNamePrefix a string that will be used to name
* the log files (appending .in and .out for the two logs).
* Should be unique.
* Must be destroyed by caller.
*/
LoggingSocketStream( Socket *inSocket, char inEnableInboundLog,
char inEnableOutboundLog,
unsigned long inLogSizeLimit,
char *inLogNamePrefix );
virtual ~LoggingSocketStream();
// overrides the SocketStream implementations
long read( unsigned char *inBuffer, long inNumBytes );
long write( unsigned char *inBuffer, long inNumBytes );
protected:
char mEnableInboundLog;
char mEnableOutboundLog;
unsigned long mLogSizeLimit;
FILE *mInboundLogFile;
FILE *mOutboundLogFile;
unsigned long mInboundSizeSoFar;
unsigned long mOutboundSizeSoFar;
};
inline LoggingSocketStream::LoggingSocketStream(
Socket *inSocket,
char inEnableInboundLog,
char inEnableOutboundLog,
unsigned long inLogSizeLimit,
char *inLogNamePrefix )
: SocketStream( inSocket ),
mEnableInboundLog( inEnableInboundLog ),
mEnableOutboundLog( inEnableOutboundLog ),
mLogSizeLimit( inLogSizeLimit ),
mInboundLogFile( NULL ),
mOutboundLogFile( NULL ),
mInboundSizeSoFar( 0 ),
mOutboundSizeSoFar( 0 ) {
if( mEnableInboundLog ) {
char *inboundFileName = autoSprintf( "%s.in", inLogNamePrefix );
mInboundLogFile = fopen( inboundFileName, "wb" );
delete [] inboundFileName;
}
if( mEnableOutboundLog ) {
char *outboundFileName = autoSprintf( "%s.out", inLogNamePrefix );
mOutboundLogFile = fopen( outboundFileName, "wb" );
delete [] outboundFileName;
}
}
inline LoggingSocketStream::~LoggingSocketStream() {
if( mInboundLogFile != NULL ) {
fclose( mInboundLogFile );
mInboundLogFile = NULL;
}
if( mOutboundLogFile != NULL ) {
fclose( mOutboundLogFile );
mOutboundLogFile = NULL;
}
}
inline long LoggingSocketStream::read( unsigned char *inBuffer,
long inNumBytes ) {
long returnVal = SocketStream::read( inBuffer, inNumBytes );
if( mEnableInboundLog &&
mInboundSizeSoFar < mLogSizeLimit &&
returnVal == inNumBytes ) {
int numToLog = inNumBytes;
if( mInboundSizeSoFar + numToLog > mLogSizeLimit ) {
numToLog = mLogSizeLimit - mInboundSizeSoFar;
}
if( mInboundLogFile != NULL ) {
fwrite( inBuffer, 1, numToLog, mInboundLogFile );
fflush( mInboundLogFile );
}
mInboundSizeSoFar += numToLog;
}
return returnVal;
}
inline long LoggingSocketStream::write( unsigned char *inBuffer,
long inNumBytes ) {
long returnVal = SocketStream::write( inBuffer, inNumBytes );
if( mEnableOutboundLog &&
mOutboundSizeSoFar < mLogSizeLimit &&
returnVal == inNumBytes ) {
int numToLog = inNumBytes;
if( mOutboundSizeSoFar + numToLog > mLogSizeLimit ) {
numToLog = mLogSizeLimit - mOutboundSizeSoFar;
}
if( mOutboundLogFile != NULL ) {
fwrite( inBuffer, 1, numToLog, mOutboundLogFile );
fflush( mOutboundLogFile );
}
mOutboundSizeSoFar += numToLog;
}
return returnVal;
}
#endif
|