Network logging has been improved with a more flexible set o classes (sinks). Two implementations for a file sink and the game console sink are provided so currently network logging is output using both these sinks.
This was SVN commit r5411.
This commit is contained in:
parent
3c836d1e96
commit
746c081c9c
File diff suppressed because it is too large
Load Diff
@ -1,52 +1,658 @@
|
||||
#ifndef INCLUDED_NETWORK_NETLOG
|
||||
#define INCLUDED_NETWORK_NETLOG
|
||||
/**
|
||||
*-----------------------------------------------------------------------------
|
||||
* FILE : NetLog.h
|
||||
* PROJECT : 0 A.D.
|
||||
* DESCRIPTION : Network subsystem logging classes declarations
|
||||
*-----------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#ifndef NETLOG_H
|
||||
#define NETLOG_H
|
||||
|
||||
// INCLUDES
|
||||
#include "ps/Pyrogenesis.h"
|
||||
#include "ps/ThreadUtil.h"
|
||||
#include "ps/CStr.h"
|
||||
#include "lib/timer.h"
|
||||
|
||||
#include <list>
|
||||
#include <vector>
|
||||
#include <fstream>
|
||||
|
||||
// DECLARATIONS
|
||||
typedef enum
|
||||
{
|
||||
LOG_LEVEL_ALL = 0x0000, // Lowest level possible
|
||||
LOG_LEVEL_DEBUG = 0x0001, // Informational events for app debugging
|
||||
LOG_LEVEL_INFO = 0x0002, // Useful for highlighting app progress
|
||||
LOG_LEVEL_WARN = 0x0004, // Potentially dangerous situations
|
||||
LOG_LEVEL_ERROR = 0x0008, // Error events but the app can continue
|
||||
LOG_LEVEL_FATAL = 0x0010, // Very severe errors, the app aborts
|
||||
LOG_LEVEL_OFF = 0xFFFF, // The highest level possible
|
||||
} LogLevel;
|
||||
|
||||
class CNetLogger;
|
||||
class CNetLogSink;
|
||||
|
||||
typedef std::list< CNetLogger* > LoggerList;
|
||||
typedef std::vector< CNetLogSink* > SinkList;
|
||||
|
||||
/*
|
||||
CNetLog: an interface for writing to the network trace log. This log is
|
||||
intended for spewing. Don't hold back! ;-)
|
||||
|
||||
The log should be self-initializing and is only safe to call after main is
|
||||
started. Log functions may be called from any thread at any time.
|
||||
CLASS : CNetLogEvent
|
||||
DESCRIPTION : CNetLogEvent represents an object passed between
|
||||
different network logging components when a decision
|
||||
for logging is made
|
||||
NOTES :
|
||||
*/
|
||||
class CNetLog
|
||||
|
||||
class CNetLogEvent
|
||||
{
|
||||
bool m_Flush;
|
||||
bool m_Initialized;
|
||||
FILE *m_pFile;
|
||||
CMutex m_Mutex;
|
||||
|
||||
/*
|
||||
Open the log file for writing.
|
||||
*/
|
||||
void Open(const char *filename);
|
||||
/*
|
||||
Flush any pending log entries to disk.
|
||||
*/
|
||||
void Flush();
|
||||
/*
|
||||
Open the log file and set up everything (if not already set up)
|
||||
*/
|
||||
void Initialize();
|
||||
|
||||
public:
|
||||
CNetLog();
|
||||
~CNetLog();
|
||||
|
||||
/*
|
||||
Write a log entry to the network log. Works like printf.
|
||||
*/
|
||||
void Write(const char *format, ...);
|
||||
/*
|
||||
Enable/disable flushing after every log line. Flushing starts enabled.
|
||||
Setting flush to true will also flush any currently buffered log lines.
|
||||
*/
|
||||
void SetFlush(bool flush);
|
||||
|
||||
CNetLogEvent(
|
||||
LogLevel level,
|
||||
const CStr& message,
|
||||
const CStr& loggerName );
|
||||
~CNetLogEvent( void );
|
||||
|
||||
/**
|
||||
* Returns the level of the event
|
||||
*
|
||||
* @return Event level;
|
||||
*/
|
||||
inline LogLevel GetLevel( void ) const { return m_Level; }
|
||||
|
||||
/**
|
||||
* Returns the time of the event
|
||||
*
|
||||
* @return Event time
|
||||
*/
|
||||
inline Timer GetTimeStamp( void ) const { return m_TimeStamp; }
|
||||
|
||||
/**
|
||||
* Returns the name of the logger which logged the event
|
||||
*
|
||||
* @return Logger name
|
||||
*/
|
||||
inline const CStr& GetLoggerName( void ) const { return m_LoggerName; }
|
||||
|
||||
/**
|
||||
* Returns the message used when the event was initialized
|
||||
*
|
||||
* @return Event message
|
||||
*/
|
||||
inline const CStr& GetMessage( void ) const { return m_Message; }
|
||||
|
||||
protected:
|
||||
|
||||
private:
|
||||
|
||||
// Not implemented
|
||||
CNetLogEvent( const CNetLogEvent& );
|
||||
CNetLogEvent& operator=( CNetLogEvent& );
|
||||
|
||||
LogLevel m_Level; // Current level
|
||||
CStr m_LoggerName; // Logger which processed the event
|
||||
CStr m_Message; // Application message for the event
|
||||
Timer m_TimeStamp; // Event logging time
|
||||
};
|
||||
extern CNetLog g_NetLog;
|
||||
|
||||
#define NET_LOG (g_NetLog.Write)
|
||||
/*
|
||||
CLASS : CNetLogSink
|
||||
DESCRIPTION : CNetLogSink is the basic interface for events logging
|
||||
NOTES :
|
||||
*/
|
||||
|
||||
class CNetLogSink
|
||||
{
|
||||
public:
|
||||
|
||||
CNetLogSink( );
|
||||
virtual ~CNetLogSink( );
|
||||
|
||||
/**
|
||||
* Set the name of the sink
|
||||
*
|
||||
* @param name New sink name
|
||||
*/
|
||||
void SetName( const CStr& name );
|
||||
|
||||
/**
|
||||
* Retrieves the name of the sink
|
||||
*
|
||||
* @return Sink name
|
||||
*/
|
||||
inline const CStr& GetName( void ) const { return m_Name; }
|
||||
|
||||
/**
|
||||
* Set the level of the sink
|
||||
*
|
||||
* @param level New sink level
|
||||
*/
|
||||
void SetLevel( LogLevel level );
|
||||
|
||||
/**
|
||||
* Retrieves the current level of the sink
|
||||
*
|
||||
* @return Sink current level
|
||||
*/
|
||||
inline LogLevel GetLevel( void ) const { return m_Level; }
|
||||
|
||||
/**
|
||||
* Retrieves the header text
|
||||
*
|
||||
* @return The header text
|
||||
*/
|
||||
inline const CStr& GetHeader( void ) const { return m_Header; }
|
||||
|
||||
/**
|
||||
* Set header text which will be logged before an event logging
|
||||
*
|
||||
* @param header New header text
|
||||
*/
|
||||
void SetHeader( const CStr& header );
|
||||
|
||||
/**
|
||||
* Retrieves the footer text
|
||||
*
|
||||
* @return The footer text
|
||||
*/
|
||||
inline const CStr& GetFooter( void ) const { return m_Footer; }
|
||||
|
||||
/**
|
||||
* Set footer text which will be logged after an event logging
|
||||
*
|
||||
* @param footer New footer text
|
||||
*/
|
||||
void SetFooter( const CStr& footer );
|
||||
|
||||
/**
|
||||
* Activates the sink
|
||||
*/
|
||||
void Activate( void );
|
||||
|
||||
/**
|
||||
* Closes the sink and release any resources
|
||||
*/
|
||||
void Close( void );
|
||||
|
||||
/**
|
||||
* Check if the level of the event is greater than or equal to sink level
|
||||
* and if it succeeds it performs the actual logging of the event.
|
||||
*
|
||||
* @param event Event to log
|
||||
*/
|
||||
void DoSink( const CNetLogEvent& event );
|
||||
|
||||
/**
|
||||
* For each event from the passed array, check if its level is greater than
|
||||
* or equal to sink level and performs the logging for it.
|
||||
*
|
||||
* @param pEvents List of events to log
|
||||
* @param eventCount The number of events in pEvents list
|
||||
*/
|
||||
void DoBulkSink( const CNetLogEvent* pEvents, uint eventCount );
|
||||
|
||||
/**
|
||||
* Check if the sink can log the specified event
|
||||
*
|
||||
* @param event Event to check
|
||||
* @return true if the event can be logged,
|
||||
* false otherwise
|
||||
*/
|
||||
virtual bool TestEvent( const CNetLogEvent& event );
|
||||
|
||||
protected:
|
||||
|
||||
/**
|
||||
* Activates the sink object
|
||||
*/
|
||||
virtual void OnActivate( void );
|
||||
|
||||
/**
|
||||
* Writes a header into the sink
|
||||
*/
|
||||
virtual void WriteHeader( void );
|
||||
|
||||
/**
|
||||
* Writes a footer into the sink
|
||||
*/
|
||||
virtual void WriteFooter( void );
|
||||
|
||||
/**
|
||||
* Writes a string message to the logging output
|
||||
*
|
||||
* @param message The message to log
|
||||
*/
|
||||
virtual void Write( const CStr& message ) = 0;
|
||||
|
||||
/**
|
||||
* Writes a single character to the logging output
|
||||
*
|
||||
* @param c The character to log
|
||||
*/
|
||||
virtual void Write( char c ) = 0;
|
||||
|
||||
/**
|
||||
* This is called by Close method. It can be overriden by specialized sinks
|
||||
* if any resources needs to be released on close.
|
||||
*/
|
||||
virtual void OnClose( void );
|
||||
|
||||
/**
|
||||
* This method is called by DoSink and DoBulkSink and it must be
|
||||
* implemented by specialized sinks to perform actual logging
|
||||
*
|
||||
* @param event Event to log
|
||||
*/
|
||||
virtual void Sink( const CNetLogEvent& event ) = 0;
|
||||
|
||||
LogLevel m_Level; // Current level
|
||||
CMutex m_Mutex; // Multithreading synchronization object
|
||||
CStr m_Header; // Header text
|
||||
CStr m_Footer; // Footer text
|
||||
CStr m_Name; // Sink name
|
||||
bool m_Closed; // Indicates whether the sink is closed
|
||||
bool m_Active; // Indicates whether the sink is active
|
||||
|
||||
private:
|
||||
|
||||
// Not implemented
|
||||
CNetLogSink( const CNetLogSink& );
|
||||
CNetLogSink& operator=( const CNetLogSink& );
|
||||
};
|
||||
|
||||
/*
|
||||
CLASS : CNetLogFileSink
|
||||
DESCRIPTION : Log network events to a file
|
||||
NOTES :
|
||||
*/
|
||||
|
||||
class CNetLogFileSink : public CNetLogSink
|
||||
{
|
||||
public:
|
||||
|
||||
CNetLogFileSink( void );
|
||||
CNetLogFileSink( const CStr& filename );
|
||||
CNetLogFileSink( const CStr& filename, bool append );
|
||||
~CNetLogFileSink( void );
|
||||
|
||||
protected:
|
||||
|
||||
/**
|
||||
* Activates the sink object and opens the file specified in constructor
|
||||
*/
|
||||
virtual void OnActivate( void );
|
||||
|
||||
/**
|
||||
* Closes the log file
|
||||
*/
|
||||
virtual void OnClose( void );
|
||||
|
||||
/**
|
||||
* Writes the event to the log file if opened
|
||||
*
|
||||
* @param event Event to log
|
||||
*/
|
||||
virtual void Sink( const CNetLogEvent& event );
|
||||
|
||||
/**
|
||||
* Writes the message passed as parameter to file
|
||||
*
|
||||
* @param message The message to log
|
||||
*/
|
||||
virtual void Write( const CStr& message );
|
||||
|
||||
/**
|
||||
* Writes the character passed as parameter to file
|
||||
*
|
||||
* @param c The character to log
|
||||
*/
|
||||
virtual void Write( char c );
|
||||
|
||||
private:
|
||||
|
||||
// Not implemented
|
||||
CNetLogFileSink( const CNetLogFileSink& );
|
||||
CNetLogFileSink& operator=( const CNetLogFileSink& );
|
||||
|
||||
/**
|
||||
* Open the file where logging goes. The header text will be written each
|
||||
* time the file is opened. If append parameter is true, then the file may
|
||||
* contain the header many times.
|
||||
*
|
||||
* @param filename The path to the log file
|
||||
* @param append Indicates whether logging should append to
|
||||
* the file or truncate the file
|
||||
*/
|
||||
void OpenFile( const CStr& fileName, bool append );
|
||||
|
||||
/**
|
||||
* Close the previously opened file. The footer text will be written each time
|
||||
* the file is closed. If the file was opened for appending, the footer might
|
||||
* appera many times.
|
||||
*/
|
||||
void CloseFile( void );
|
||||
|
||||
std::ofstream m_File; // The log file handle
|
||||
CStr m_FileName; // The name of the log file
|
||||
bool m_Append; // Logging should append to file
|
||||
};
|
||||
|
||||
/*
|
||||
CLASS : CNetLogConsoleSink
|
||||
DESCRIPTION : Log network events to the game console
|
||||
NOTES :
|
||||
*/
|
||||
|
||||
class CNetLogConsoleSink : public CNetLogSink
|
||||
{
|
||||
public:
|
||||
|
||||
CNetLogConsoleSink( void );
|
||||
~CNetLogConsoleSink( void );
|
||||
|
||||
protected:
|
||||
|
||||
/**
|
||||
* Activates the sink object and the game console
|
||||
*/
|
||||
virtual void OnActivate( void );
|
||||
|
||||
/**
|
||||
* Toggle off game console
|
||||
*/
|
||||
virtual void OnClose( void );
|
||||
|
||||
/**
|
||||
* Writes the event to the game console if active
|
||||
*
|
||||
* @param event Event to log
|
||||
*/
|
||||
virtual void Sink( const CNetLogEvent& event );
|
||||
|
||||
/**
|
||||
* Writes the message passed as parameter to game console
|
||||
*
|
||||
* @param message The message to log
|
||||
*/
|
||||
virtual void Write( const CStr& message );
|
||||
|
||||
/**
|
||||
* Writes the character passed as parameter to game console
|
||||
*
|
||||
* @param c The character to log
|
||||
*/
|
||||
virtual void Write( char c );
|
||||
|
||||
private:
|
||||
|
||||
// Not implemented
|
||||
CNetLogConsoleSink( const CNetLogConsoleSink& );
|
||||
CNetLogConsoleSink& operator=( const CNetLogConsoleSink& );
|
||||
};
|
||||
|
||||
/*
|
||||
CLASS : CNetLogger
|
||||
DESCRIPTION : CNetLogger serves for logging messages for network subsytem.
|
||||
It contains methods for logging at different levels.
|
||||
NOTES : CNetLogManager is used to obtain an instance of a logger.
|
||||
*/
|
||||
|
||||
class CNetLogger
|
||||
{
|
||||
public:
|
||||
|
||||
CNetLogger( const CStr& name );
|
||||
virtual ~CNetLogger( void );
|
||||
|
||||
bool IsDebugEnabled ( void ) const;
|
||||
bool IsInfoEnabled ( void ) const;
|
||||
bool IsWarnEnabled ( void ) const;
|
||||
bool IsErrorEnabled ( void ) const;
|
||||
bool IsFatalEnabled ( void ) const;
|
||||
|
||||
void Debug ( const CStr& message );
|
||||
void Warn ( const CStr& message );
|
||||
void Info ( const CStr& message );
|
||||
void Error ( const CStr& message );
|
||||
void Fatal ( const CStr& message );
|
||||
|
||||
void DebugFormat ( const char* pFormat, ... );
|
||||
void WarnFormat ( const char* pFormat, ... );
|
||||
void InfoFormat ( const char* pFormat, ... );
|
||||
void ErrorFormat ( const char* pFormat, ... );
|
||||
void FatalFormat ( const char* pFormat, ... );
|
||||
|
||||
/**
|
||||
* Retrieves the name of the logger
|
||||
*
|
||||
* @return Logger name
|
||||
*/
|
||||
const CStr& GetName( void ) const { return m_Name; }
|
||||
|
||||
/**
|
||||
* Retrieves the level of the logger
|
||||
*
|
||||
* @return Logger level
|
||||
*/
|
||||
LogLevel GetLevel( void ) const { return m_Level; }
|
||||
|
||||
/**
|
||||
* Set the level for the logger
|
||||
*
|
||||
* @param level New logger level
|
||||
*/
|
||||
void SetLevel( LogLevel level );
|
||||
|
||||
/**
|
||||
* Attaches a new sink to the list of sinks. The sink will be activated.
|
||||
*
|
||||
* @param pSink The sink to add
|
||||
*/
|
||||
void AddSink( CNetLogSink* pSink );
|
||||
|
||||
/**
|
||||
* Removes the specified sink from the list of attached sinks. The sink
|
||||
* will not be closed.
|
||||
*
|
||||
* @param pSink The sink to remove
|
||||
* @return The removed sink or NULL if not found
|
||||
*/
|
||||
CNetLogSink* RemoveSink( CNetLogSink* pSink );
|
||||
|
||||
/**
|
||||
* Remove the named sink passed as parameter. The sink will not be closed.
|
||||
*
|
||||
* @param name The name of sink to remove
|
||||
* @return The removed sink or NULL if not found
|
||||
*/
|
||||
CNetLogSink* RemoveSink( const CStr& name );
|
||||
|
||||
/**
|
||||
* Removes all attached sinks
|
||||
*
|
||||
*/
|
||||
void RemoveAllSinks( void );
|
||||
|
||||
/**
|
||||
* Retrieve the number of attached sinks
|
||||
*
|
||||
* @return The number of sink objects
|
||||
*/
|
||||
uint GetSinkCount( void );
|
||||
|
||||
/**
|
||||
* Retrieves the sink by its index
|
||||
*
|
||||
* @param index The index of the sink
|
||||
* @return NULL if index is out of boundaries or
|
||||
* the sink at the specified index
|
||||
*/
|
||||
CNetLogSink* GetSink( uint index );
|
||||
|
||||
/**
|
||||
* Retrieves a sink by its name
|
||||
*
|
||||
* @param name The name of the sink
|
||||
* @return NULL if the sink does not exists or
|
||||
* the sink with the specified name
|
||||
*/
|
||||
CNetLogSink* GetSink( const CStr& name );
|
||||
|
||||
/**
|
||||
* Helper function used to retrieve local date time in a string
|
||||
*/
|
||||
static void GetStringDateTime( CStr& str );
|
||||
|
||||
/**
|
||||
* Helper function used to retrieve local time in a string
|
||||
*/
|
||||
static void GetStringTime( CStr& str );
|
||||
|
||||
/**
|
||||
* Helper function used to retrieve the current timestamp in a string
|
||||
*/
|
||||
static void GetStringTimeStamp( CStr& str );
|
||||
|
||||
protected:
|
||||
|
||||
private:
|
||||
|
||||
// Not implemented
|
||||
CNetLogger( const CNetLogger& );
|
||||
CNetLogger& operator=( const CNetLogger& );
|
||||
|
||||
/**
|
||||
* Dispatch the event passed as parameter to all sinks
|
||||
*
|
||||
* @param event The event to log
|
||||
*/
|
||||
void CallSinks( const CNetLogEvent& event );
|
||||
|
||||
CMutex m_Mutex; // Multithread synchronization object
|
||||
SinkList m_Sinks; // Holds the list of sink objects
|
||||
LogLevel m_Level; // Logger level
|
||||
CStr m_Name; // Logger name
|
||||
};
|
||||
|
||||
/*
|
||||
CLASS : CNetLogManager
|
||||
DESCRIPTION : CNetLogManager serves clients requesting log instances
|
||||
NOTES : The GetLogger method can be used to retrieve a log
|
||||
*/
|
||||
|
||||
class CNetLogManager
|
||||
{
|
||||
public:
|
||||
|
||||
/**
|
||||
* Shutdown the log manager, closes all sinks in the loggers.
|
||||
*
|
||||
*/
|
||||
static void Shutdown( void );
|
||||
|
||||
/**
|
||||
* Retrieves a named logger. If the logger does not exist, it is created.
|
||||
*
|
||||
* @param name Logger name
|
||||
* @return A logger object
|
||||
*/
|
||||
static CNetLogger* GetLogger( const CStr& name );
|
||||
|
||||
/**
|
||||
* Return the list of all defined loggers.
|
||||
*
|
||||
* @return The list of all loggers
|
||||
*/
|
||||
static const LoggerList& GetAllLoggers( void );
|
||||
|
||||
private:
|
||||
|
||||
// Not implemented
|
||||
CNetLogManager( void );
|
||||
~CNetLogManager( void );
|
||||
CNetLogManager( const CNetLogManager& );
|
||||
CNetLogManager& operator=( const CNetLogManager& );
|
||||
|
||||
static LoggerList m_Loggers; // Holds the list of loggers
|
||||
};
|
||||
|
||||
// TODO: Replace with better access to log manager
|
||||
#define START_LOGGER( sinkName, sinkType )\
|
||||
CNetLogger *pLogger = CNetLogManager::GetLogger( "net.log" );\
|
||||
if ( pLogger )\
|
||||
{\
|
||||
CNetLogSink* pSink = pLogger->GetSink( sinkName );\
|
||||
if ( !pSink )\
|
||||
{\
|
||||
pSink = new sinkType();\
|
||||
if ( pSink )\
|
||||
{\
|
||||
CStr startTime;\
|
||||
CNetLogger::GetStringDateTime( startTime );\
|
||||
CStr header = "***************************************************\n";\
|
||||
header += "LOG STARTED: ";\
|
||||
header += startTime;\
|
||||
header += "\n";\
|
||||
header += "Timestamps are in seconds since engine startup\n";\
|
||||
pSink->SetHeader( header );\
|
||||
pSink->SetName( sinkName );\
|
||||
pSink->SetLevel( LOG_LEVEL_INFO );\
|
||||
pLogger->AddSink( pSink );\
|
||||
}\
|
||||
}
|
||||
|
||||
#define END_LOGGER\
|
||||
}
|
||||
|
||||
#define NET_LOG( parameter )\
|
||||
{\
|
||||
START_LOGGER( "sink.file", CNetLogFileSink )\
|
||||
pLogger->Info( parameter );\
|
||||
END_LOGGER\
|
||||
}\
|
||||
{\
|
||||
START_LOGGER( "sink.console", CNetLogConsoleSink )\
|
||||
pLogger->Info( parameter );\
|
||||
END_LOGGER\
|
||||
}
|
||||
|
||||
#define NET_LOG2( format, parameter )\
|
||||
{\
|
||||
START_LOGGER( "sink.file", CNetLogFileSink )\
|
||||
pLogger->InfoFormat( format, parameter );\
|
||||
END_LOGGER\
|
||||
}\
|
||||
{\
|
||||
START_LOGGER( "sink.console", CNetLogConsoleSink )\
|
||||
pLogger->InfoFormat( format, parameter );\
|
||||
END_LOGGER\
|
||||
}
|
||||
|
||||
#define NET_LOG3( format, parameter1, parameter2 )\
|
||||
{\
|
||||
START_LOGGER( "sink.file", CNetLogFileSink )\
|
||||
pLogger->InfoFormat( format, parameter1, parameter2 );\
|
||||
END_LOGGER\
|
||||
}\
|
||||
{\
|
||||
START_LOGGER( "sink.console", CNetLogConsoleSink )\
|
||||
pLogger->InfoFormat( format, parameter1, parameter2 );\
|
||||
END_LOGGER\
|
||||
}
|
||||
|
||||
#define NET_LOG4( format, parameter1, parameter2, parameter3 )\
|
||||
{\
|
||||
START_LOGGER( "sink.file", CNetLogFileSink )\
|
||||
pLogger->InfoFormat( format, parameter1, parameter2, parameter3 );\
|
||||
END_LOGGER\
|
||||
}\
|
||||
{\
|
||||
START_LOGGER( "sink.console", CNetLogConsoleSink )\
|
||||
pLogger->InfoFormat( format, parameter1, parameter2, parameter3 );\
|
||||
END_LOGGER\
|
||||
}
|
||||
|
||||
#endif // NETLOG_H
|
||||
|
||||
#endif
|
||||
|
@ -102,7 +102,7 @@ CStr CCloseRequestMessage::GetString() const
|
||||
|
||||
void CMessageSocket::Push(CNetMessage *msg)
|
||||
{
|
||||
NET_LOG("CMessageSocket::Push(): %s", msg->GetString().c_str());
|
||||
NET_LOG2( "CMessageSocket::Push(): %s", msg->GetString().c_str() );
|
||||
|
||||
m_OutQ.Lock();
|
||||
m_OutQ.push_back(msg);
|
||||
@ -184,7 +184,8 @@ void CMessageSocket::StartWriteNextMessage()
|
||||
PS_RESULT res=Write(m_pWrBuffer, hdr.m_MsgLength+HEADER_LENGTH);
|
||||
if (res != PS_OK)
|
||||
{
|
||||
NET_LOG("CMessageSocket::StartWriteNextMessage(): %s", res);
|
||||
NET_LOG2( "CMessageSocket::StartWriteNextMessage(): %s", res );
|
||||
|
||||
// Queue Error Message
|
||||
m_InQ.Lock();
|
||||
m_InQ.push_back(new CNetErrorMessage(res, GetState()));
|
||||
@ -194,16 +195,21 @@ void CMessageSocket::StartWriteNextMessage()
|
||||
else
|
||||
{
|
||||
if (m_IsWriting)
|
||||
NET_LOG("CMessageSocket::StartWriteNextMessage(): Already writing");
|
||||
{
|
||||
NET_LOG( "CMessageSocket::StartWriteNextMessage(): Already writing" );
|
||||
}
|
||||
else
|
||||
{
|
||||
NET_LOG("CMessageSocket::StartWriteNextMessage(): Nothing to write");
|
||||
}
|
||||
m_OutQ.Unlock();
|
||||
}
|
||||
}
|
||||
|
||||
void CMessageSocket::WriteComplete(PS_RESULT ec)
|
||||
{
|
||||
NET_LOG("CMessageSocket::WriteComplete(): %s", ec);
|
||||
NET_LOG2( "CMessageSocket::WriteComplete(): %s", ec );
|
||||
|
||||
if (ec == PS_OK)
|
||||
{
|
||||
if (m_IsWriting)
|
||||
@ -214,7 +220,9 @@ void CMessageSocket::WriteComplete(PS_RESULT ec)
|
||||
StartWriteNextMessage();
|
||||
}
|
||||
else
|
||||
NET_LOG("CMessageSocket::WriteComplete(): Was not writing");
|
||||
{
|
||||
NET_LOG( "CMessageSocket::WriteComplete(): Was not writing" );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -238,7 +246,8 @@ void CMessageSocket::StartReadHeader()
|
||||
PS_RESULT res=Read(m_pRdBuffer, HEADER_LENGTH);
|
||||
if (res != PS_OK)
|
||||
{
|
||||
NET_LOG("CMessageSocket::StartReadHeader(): %s", res);
|
||||
NET_LOG2( "CMessageSocket::StartReadHeader(): %s", res );
|
||||
|
||||
// Push an error message
|
||||
CScopeLock scopeLock(m_InQ.m_Mutex);
|
||||
m_InQ.push_back(new CNetErrorMessage(res, GetState()));
|
||||
@ -271,7 +280,8 @@ void CMessageSocket::StartReadMessage()
|
||||
PS_RESULT res=Read(m_pRdBuffer+HEADER_LENGTH, hdr.m_MsgLength);
|
||||
if (res != PS_OK)
|
||||
{
|
||||
NET_LOG("CMessageSocket::StartReadMessage(): %s", res);
|
||||
NET_LOG2( "CMessageSocket::StartReadMessage(): %s", res );
|
||||
|
||||
// Queue an error message
|
||||
CScopeLock scopeLock(m_InQ);
|
||||
m_InQ.push_back(new CNetErrorMessage(res, GetState()));
|
||||
@ -281,7 +291,8 @@ void CMessageSocket::StartReadMessage()
|
||||
|
||||
void CMessageSocket::ReadComplete(PS_RESULT ec)
|
||||
{
|
||||
NET_LOG("CMessageSocket::ReadComplete(%s): %s", m_ReadingData?"data":"header", ec);
|
||||
NET_LOG3( "CMessageSocket::ReadComplete(%s): %s", m_ReadingData ? "data":"header", ec );
|
||||
|
||||
// Check if we were reading header or message
|
||||
// If header:
|
||||
if (!m_ReadingData)
|
||||
@ -300,13 +311,14 @@ void CMessageSocket::ReadComplete(PS_RESULT ec)
|
||||
}
|
||||
else
|
||||
{
|
||||
NET_LOG("CMessageSocket::ReadComplete(): Deserialization failed! (type %d, length %d)", hdr.m_MsgType, hdr.m_MsgLength);
|
||||
NET_LOG("Data: {");
|
||||
NET_LOG3( "CMessageSocket::ReadComplete(): Deserialization failed! (type %d, length %d)", hdr.m_MsgType, hdr.m_MsgLength );
|
||||
NET_LOG( "Data: {" );
|
||||
|
||||
for (int i=HEADER_LENGTH;i<hdr.m_MsgLength+HEADER_LENGTH;i++)
|
||||
{
|
||||
NET_LOG("\t0x%x [%c]", m_pRdBuffer[i], isalnum(m_pRdBuffer[i])?m_pRdBuffer[i]:'.');
|
||||
NET_LOG3( "\t0x%x [%c]", m_pRdBuffer[i], isalnum(m_pRdBuffer[i])?m_pRdBuffer[i]:'.' );
|
||||
}
|
||||
NET_LOG("};");
|
||||
NET_LOG( "};" );
|
||||
}
|
||||
StartReadHeader();
|
||||
}
|
||||
@ -316,8 +328,8 @@ void CMessageSocket::OnMessage(CNetMessage *pMsg)
|
||||
{
|
||||
m_InQ.Lock();
|
||||
m_InQ.push_back(pMsg);
|
||||
NET_LOG("CMessageSocket::OnMessage(): %s", pMsg->GetString().c_str());
|
||||
NET_LOG("CMessageSocket::OnMessage(): Queue size now %u", m_InQ.size());
|
||||
NET_LOG2( "CMessageSocket::OnMessage(): %s", pMsg->GetString().c_str() );
|
||||
NET_LOG2( "CMessageSocket::OnMessage(): Queue size now %u", m_InQ.size() );
|
||||
m_InQ.Unlock();
|
||||
}
|
||||
|
||||
|
@ -20,7 +20,7 @@ void CServerSocket::OnRead()
|
||||
{
|
||||
// All errors are non-critical, so no need to do anything special besides
|
||||
// not calling OnAccept [ shouldn't be, that is ;-) ]
|
||||
NET_LOG("CServerSocket::OnRead(): PreAccept returned an error: %s", res);
|
||||
NET_LOG2( "CServerSocket::OnRead(): PreAccept returned an error: %s", res );
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -179,14 +179,16 @@ CSocketBase::CSocketBase(CSocketInternal *pInt)
|
||||
m_Error=PS_OK;
|
||||
SetNonBlocking(true);
|
||||
|
||||
NET_LOG("CSocketBase::CSocketBase(): Created socket from fd %d", pInt->m_fd);
|
||||
NET_LOG2( "CSocketBase::CSocketBase(): Created socket from fd %d", pInt->m_fd );
|
||||
}
|
||||
|
||||
CSocketBase::~CSocketBase()
|
||||
{
|
||||
NET_LOG("CSocketBase::~CSocketBase(): fd is %d. "
|
||||
"Received: %lld bytes. Sent: %lld bytes.", m_pInternal->m_fd,
|
||||
m_pInternal->m_RecvBytes, m_pInternal->m_SentBytes);
|
||||
NET_LOG4( "CSocketBase::~CSocketBase(): fd is %d. "
|
||||
"Received: %lld bytes. Sent: %lld bytes.",
|
||||
m_pInternal->m_fd,
|
||||
m_pInternal->m_RecvBytes,
|
||||
m_pInternal->m_SentBytes );
|
||||
|
||||
Destroy();
|
||||
delete m_pInternal;
|
||||
@ -198,14 +200,14 @@ void CSocketBase::Shutdown()
|
||||
|
||||
if (g_SocketSetInternal.m_NumSockets)
|
||||
{
|
||||
NET_LOG("CSocketBase::Shutdown(): %d sockets still open! (forcing network shutdown)", g_SocketSetInternal.m_NumSockets);
|
||||
NET_LOG2( "CSocketBase::Shutdown(): %d sockets still open! (forcing network shutdown)", g_SocketSetInternal.m_NumSockets );
|
||||
}
|
||||
|
||||
#if RECORD_GLOBAL_STATS
|
||||
NET_LOG("GLOBAL SOCKET STATISTICS: "
|
||||
"Received: %lld bytes. Sent: %lld bytes.",
|
||||
g_SocketSetInternal.m_GlobalRecvBytes,
|
||||
g_SocketSetInternal.m_GlobalSentBytes);
|
||||
NET_LOG3( "GLOBAL SOCKET STATISTICS: "
|
||||
"Received: %lld bytes. Sent: %lld bytes.",
|
||||
g_SocketSetInternal.m_GlobalRecvBytes,
|
||||
g_SocketSetInternal.m_GlobalSentBytes);
|
||||
#endif
|
||||
|
||||
GLOBAL_UNLOCK();
|
||||
@ -234,7 +236,7 @@ PS_RESULT CSocketBase::Initialize(ESocketProtocol proto)
|
||||
// Use IPV4 by default, ignoring address type.
|
||||
int res=socket(AF_INET, SOCK_STREAM, 0);
|
||||
|
||||
NET_LOG("CSocketBase::Initialize(): socket() res: %d", res);
|
||||
NET_LOG2("CSocketBase::Initialize(): socket() res: %d", res);
|
||||
|
||||
if (res == -1)
|
||||
{
|
||||
@ -303,7 +305,7 @@ void CSocketBase::SetNonBlocking(bool nonblocking)
|
||||
SendWaitLoopUpdate(); // Need to call WSAAsyncSelect with event=0 before ioctlsocket
|
||||
int res=ioctlsocket(m_pInternal->m_fd, FIONBIO, &nb);
|
||||
if (res != 0)
|
||||
NET_LOG("SetNonBlocking: res %d", res);
|
||||
NET_LOG2("SetNonBlocking: res %d", res);
|
||||
#else
|
||||
int oldflags=fcntl(m_pInternal->m_fd, F_GETFL, 0);
|
||||
if (oldflags != -1)
|
||||
@ -347,7 +349,7 @@ PS_RESULT CSocketBase::Read(void *buf, uint len, uint *bytesRead)
|
||||
case ETIMEDOUT:*/
|
||||
default:
|
||||
Network_GetErrorString(error, errbuf, sizeof(errbuf));
|
||||
NET_LOG("Read error %s [%d]", errbuf, error);
|
||||
NET_LOG3("Read error %s [%d]", errbuf, error);
|
||||
m_State=SS_UNCONNECTED;
|
||||
m_Error=GetPS_RESULT(error);
|
||||
return m_Error;
|
||||
@ -398,7 +400,7 @@ PS_RESULT CSocketBase::Write(void *buf, uint len, uint *bytesWritten)
|
||||
case EHOSTUNREACH:*/
|
||||
default:
|
||||
Network_GetErrorString(err, errbuf, sizeof(errbuf));
|
||||
NET_LOG("Write error %s [%d]", errbuf, err);
|
||||
NET_LOG3("Write error %s [%d]", errbuf, err);
|
||||
m_State=SS_UNCONNECTED;
|
||||
return CONNECTION_BROKEN;
|
||||
}
|
||||
@ -419,12 +421,12 @@ PS_RESULT CSocketBase::Write(void *buf, uint len, uint *bytesWritten)
|
||||
PS_RESULT CSocketBase::Connect(const CSocketAddress &addr)
|
||||
{
|
||||
int res = connect(m_pInternal->m_fd, (struct sockaddr *)(&addr.m_Union), sizeof(struct sockaddr));
|
||||
NET_LOG("connect returned %d [%d]", res, m_NonBlocking);
|
||||
NET_LOG3("connect returned %d [%d]", res, m_NonBlocking);
|
||||
|
||||
if (res != 0)
|
||||
{
|
||||
int error=Network_LastError;
|
||||
NET_LOG("last error was %d", error);
|
||||
NET_LOG2("last error was %d", error);
|
||||
if (m_NonBlocking && error == EWOULDBLOCK)
|
||||
{
|
||||
m_State=SS_CONNECT_STARTED;
|
||||
@ -843,7 +845,7 @@ LRESULT WINAPI WaitLoop_WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lP
|
||||
void CSocketBase::RunWaitLoop()
|
||||
{
|
||||
int ret;
|
||||
char errBuf[256];
|
||||
char errBuf[256] = {0};
|
||||
MSG msg;
|
||||
|
||||
WNDCLASS wc;
|
||||
@ -857,8 +859,8 @@ void CSocketBase::RunWaitLoop()
|
||||
if (!atom)
|
||||
{
|
||||
ret=GetLastError();
|
||||
Network_GetErrorString(ret, errBuf, sizeof(errBuf));
|
||||
NET_LOG("RegisterClass: %s [%d]", errBuf, ret);
|
||||
Network_GetErrorString(ret, (LPSTR)&errBuf, 256);
|
||||
NET_LOG3("RegisterClass: %s [%d]", errBuf, ret);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -869,7 +871,7 @@ void CSocketBase::RunWaitLoop()
|
||||
{
|
||||
ret=GetLastError();
|
||||
Network_GetErrorString(ret, errBuf, sizeof(errBuf));
|
||||
NET_LOG("CreateWindowEx: %s [%d]", errBuf, ret);
|
||||
NET_LOG3("CreateWindowEx: %s [%d]", errBuf, ret);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -883,7 +885,7 @@ void CSocketBase::RunWaitLoop()
|
||||
++it;
|
||||
}
|
||||
|
||||
NET_LOG("Commencing message loop. hWnd %p", g_SocketSetInternal.m_hWnd);
|
||||
NET_LOG2("Commencing message loop. hWnd %p", g_SocketSetInternal.m_hWnd);
|
||||
GLOBAL_UNLOCK();
|
||||
|
||||
while ((ret=GetMessage(&msg, g_SocketSetInternal.m_hWnd, 0, 0))!=0)
|
||||
@ -893,7 +895,7 @@ void CSocketBase::RunWaitLoop()
|
||||
{
|
||||
ret=GetLastError();
|
||||
Network_GetErrorString(ret, errBuf, sizeof(errBuf));
|
||||
NET_LOG("GetMessage: %s [%d]", errBuf, ret);
|
||||
NET_LOG3("GetMessage: %s [%d]", errBuf, ret);
|
||||
}
|
||||
{
|
||||
TranslateMessage(&msg);
|
||||
|
@ -25,7 +25,7 @@ void *CStreamSocket_ConnectThread(void *data)
|
||||
CSocketAddress addr;
|
||||
|
||||
res=CSocketAddress::Resolve(pSock->m_pConnectHost, pSock->m_ConnectPort, addr);
|
||||
NET_LOG("CStreamSocket_ConnectThread: Resolve: %s -> %s [%s]", pSock->m_pConnectHost, addr.GetString().c_str(), res);
|
||||
NET_LOG4("CStreamSocket_ConnectThread: Resolve: %s -> %s [%s]", pSock->m_pConnectHost, addr.GetString().c_str(), res);
|
||||
if (res == PS_OK)
|
||||
{
|
||||
pSock->Initialize();
|
||||
@ -36,7 +36,7 @@ void *CStreamSocket_ConnectThread(void *data)
|
||||
pSock->SetOpMask(0);
|
||||
pSock->SetNonBlocking(false);
|
||||
res=pSock->Connect(addr);
|
||||
NET_LOG("CStreamSocket_ConnectThread: Connect: %s", res);
|
||||
NET_LOG2("CStreamSocket_ConnectThread: Connect: %s", res);
|
||||
}
|
||||
|
||||
if (res == PS_OK)
|
||||
@ -111,7 +111,7 @@ PS_RESULT CStreamSocket::Write(void *buf, uint len)
|
||||
}
|
||||
|
||||
#define MakeDefaultCallback(_nm) void CStreamSocket::_nm(PS_RESULT error) \
|
||||
{ NET_LOG("CStreamSocket::"#_nm"(): %s", error); }
|
||||
{ NET_LOG2("CStreamSocket::"#_nm"(): %s", error); }
|
||||
|
||||
MakeDefaultCallback(OnClose)
|
||||
MakeDefaultCallback(ConnectComplete)
|
||||
@ -135,7 +135,7 @@ void CStreamSocket::OnWrite()
|
||||
WriteComplete(res);
|
||||
return;
|
||||
}
|
||||
NET_LOG("CStreamSocket::OnWrite(): %u bytes", bytes);
|
||||
NET_LOG2("CStreamSocket::OnWrite(): %u bytes", bytes);
|
||||
m_WriteContext.m_Completed+=bytes;
|
||||
if (m_WriteContext.m_Completed == m_WriteContext.m_Length)
|
||||
{
|
||||
@ -156,8 +156,9 @@ void CStreamSocket::OnRead()
|
||||
((u8 *)m_ReadContext.m_pBuffer)+m_ReadContext.m_Completed,
|
||||
m_ReadContext.m_Length-m_ReadContext.m_Completed,
|
||||
&bytes);
|
||||
NET_LOG("CStreamSocket::OnRead(): %s, %u bytes read of %u", res, bytes,
|
||||
m_ReadContext.m_Length-m_ReadContext.m_Completed);
|
||||
NET_LOG4("CStreamSocket::OnRead(): %s, %u bytes read of %u",
|
||||
res, bytes,
|
||||
m_ReadContext.m_Length-m_ReadContext.m_Completed);
|
||||
if (res != PS_OK)
|
||||
{
|
||||
ReadComplete(res);
|
||||
|
Loading…
Reference in New Issue
Block a user