More verbose logging for the server.

This commit is contained in:
lotodore
2008-03-19 23:39:59 +00:00
parent 0575d1eb1c
commit c8b38d0e57
6 changed files with 88 additions and 11 deletions
+15 -3
View File
@@ -27,21 +27,33 @@
using namespace std; using namespace std;
static int g_logLevel = 1;
void void
loghelper_init(const std::string & /*logDir*/) loghelper_init(const std::string & /*logDir*/, int logLevel)
{ {
// Do not log to file as client. // Do not log to file as client.
g_logLevel = logLevel;
} }
void void
internal_log_err(const string &msg) internal_log_err(const string &msg)
{ {
cout << msg; cerr << msg;
} }
void void
internal_log_msg(const std::string &msg) internal_log_msg(const std::string &msg)
{ {
cout << msg; if (g_logLevel)
cout << msg;
}
void
internal_log_level(const std::string &msg, int logLevel)
{
if (g_logLevel >= logLevel)
cout << msg;
} }
+24 -5
View File
@@ -39,14 +39,16 @@ using namespace boost::posix_time;
#define SERVER_MSG_LOG_FILE_NAME "server_messages.log" #define SERVER_MSG_LOG_FILE_NAME "server_messages.log"
static string g_logFile; static string g_logFile;
static int g_logLevel = 1;
void void
loghelper_init(const string &logDir) loghelper_init(const string &logDir, int logLevel)
{ {
path tmpLogFile(logDir); path tmpLogFile(logDir);
tmpLogFile /= SERVER_MSG_LOG_FILE_NAME; tmpLogFile /= SERVER_MSG_LOG_FILE_NAME;
g_logFile = tmpLogFile.directory_string(); g_logFile = tmpLogFile.directory_string();
g_logLevel = logLevel;
} }
void void
@@ -63,11 +65,28 @@ internal_log_err(const string &msg)
void void
internal_log_msg(const std::string &msg) internal_log_msg(const std::string &msg)
{ {
if (!g_logFile.empty()) if (g_logLevel)
{ {
ofstream o(g_logFile.c_str(), ios_base::out | ios_base::app); if (!g_logFile.empty())
if (!o.fail()) {
o << second_clock::local_time() << " MSG: " << msg; ofstream o(g_logFile.c_str(), ios_base::out | ios_base::app);
if (!o.fail())
o << second_clock::local_time() << " MSG: " << msg;
}
}
}
void
internal_log_level(const std::string &msg, int logLevel)
{
if (g_logLevel >= logLevel)
{
if (!g_logFile.empty())
{
ofstream o(g_logFile.c_str(), ios_base::out | ios_base::app);
if (!o.fail())
o << second_clock::local_time() << " OUT: " << msg;
}
} }
} }
+10 -1
View File
@@ -24,10 +24,11 @@
#include <string> #include <string>
#include <sstream> #include <sstream>
void loghelper_init(const std::string &logDir); void loghelper_init(const std::string &logDir, int logLevel);
void internal_log_err(const std::string &msg); void internal_log_err(const std::string &msg);
void internal_log_msg(const std::string &msg); void internal_log_msg(const std::string &msg);
void internal_log_level(const std::string &msg, int logLevel);
#define LOG_ERROR(e) \ #define LOG_ERROR(e) \
do \ do \
@@ -45,6 +46,14 @@ void internal_log_msg(const std::string &msg);
internal_log_msg(outStream.str()); \ internal_log_msg(outStream.str()); \
} \ } \
while(false) while(false)
#define LOG_VERBOSE(e) \
do \
{ \
std::ostringstream outStream; \
outStream << e << std::endl; \
internal_log_level(outStream.str(), 2); \
} \
while(false)
#endif #endif
+7
View File
@@ -41,11 +41,14 @@ ServerGameThread::ServerGameThread(ServerLobbyThread &lobbyThread, u_int32_t id,
m_stateTimer(boost::posix_time::time_duration(0, 0, 0), boost::timers::portable::microsec_timer::manual_start), m_stateTimer(boost::posix_time::time_duration(0, 0, 0), boost::timers::portable::microsec_timer::manual_start),
m_stateTimerFlag(0) m_stateTimerFlag(0)
{ {
LOG_VERBOSE("Game object " << GetId() << " created.");
m_receiver.reset(new ReceiverHelper); m_receiver.reset(new ReceiverHelper);
} }
ServerGameThread::~ServerGameThread() ServerGameThread::~ServerGameThread()
{ {
LOG_VERBOSE("Game object " << GetId() << " destructed.");
} }
u_int32_t u_int32_t
@@ -106,6 +109,8 @@ ServerGameThread::RemoveAllSessions()
void void
ServerGameThread::Main() ServerGameThread::Main()
{ {
LOG_VERBOSE("Game thread " << GetId() << "started.");
SetState(SERVER_INITIAL_STATE::Instance()); SetState(SERVER_INITIAL_STATE::Instance());
try try
@@ -138,6 +143,8 @@ ServerGameThread::Main()
ResetComputerPlayerList(); ResetComputerPlayerList();
GetLobbyThread().RemoveGame(GetId()); GetLobbyThread().RemoveGame(GetId());
LOG_VERBOSE("Game thread " << GetId() << "terminating.");
} }
void void
+20 -1
View File
@@ -148,6 +148,8 @@ ServerLobbyThread::RemoveSessionFromGame(SessionWrapper session)
void void
ServerLobbyThread::CloseSession(SessionWrapper session) ServerLobbyThread::CloseSession(SessionWrapper session)
{ {
LOG_VERBOSE("Closing session #" << session.sessionData->GetId() << ".");
m_sessionManager.RemoveSession(session.sessionData->GetId()); m_sessionManager.RemoveSession(session.sessionData->GetId());
m_gameSessionManager.RemoveSession(session.sessionData->GetId()); m_gameSessionManager.RemoveSession(session.sessionData->GetId());
@@ -363,7 +365,9 @@ ServerLobbyThread::ProcessLoop()
CloseSession(session); CloseSession(session);
return; return;
} }
if (packet.get()) if (!packet.get())
LOG_VERBOSE("Select successful but no packet received for session #" << session.sessionData->GetId() << ".");
else
{ {
if (packet->IsClientActivity()) if (packet->IsClientActivity())
session.sessionData->ResetActivityTimer(); session.sessionData->ResetActivityTimer();
@@ -640,6 +644,8 @@ ServerLobbyThread::HandleNetPacketRetrieveAvatar(SessionWrapper session, const N
void void
ServerLobbyThread::HandleNetPacketCreateGame(SessionWrapper session, const NetPacketCreateGame &tmpPacket) ServerLobbyThread::HandleNetPacketCreateGame(SessionWrapper session, const NetPacketCreateGame &tmpPacket)
{ {
LOG_VERBOSE("Creating new game, initiated by session #" << session.sessionData->GetId() << ".");
// Create a new game. // Create a new game.
NetPacketCreateGame::Data createGameData; NetPacketCreateGame::Data createGameData;
tmpPacket.GetData(createGameData); tmpPacket.GetData(createGameData);
@@ -851,6 +857,8 @@ ServerLobbyThread::CleanupAvatarCache()
if (m_cacheCleanupTimer.elapsed().total_seconds() >= SERVER_CACHE_CLEANUP_INTERVAL_SEC if (m_cacheCleanupTimer.elapsed().total_seconds() >= SERVER_CACHE_CLEANUP_INTERVAL_SEC
&& !m_sessionManager.HasSessions() && !m_gameSessionManager.HasSessions()) && !m_sessionManager.HasSessions() && !m_gameSessionManager.HasSessions())
{ {
LOG_VERBOSE("Cleaning up avatar cache.");
m_avatarManager.RemoveOldAvatarCacheEntries(); m_avatarManager.RemoveOldAvatarCacheEntries();
m_cacheCleanupTimer.reset(); m_cacheCleanupTimer.reset();
m_cacheCleanupTimer.start(); m_cacheCleanupTimer.start();
@@ -946,6 +954,8 @@ ServerLobbyThread::HandleNewConnection(boost::shared_ptr<ConnectData> connData)
boost::shared_ptr<SessionData> sessionData(new SessionData(connData->ReleaseSocket(), m_curSessionId++)); boost::shared_ptr<SessionData> sessionData(new SessionData(connData->ReleaseSocket(), m_curSessionId++));
m_sessionManager.AddSession(sessionData); m_sessionManager.AddSession(sessionData);
LOG_VERBOSE("Accepted connection - session #" << sessionData->GetId() << ".");
if (m_sessionManager.GetRawSessionCount() <= SERVER_MAX_NUM_SESSIONS) if (m_sessionManager.GetRawSessionCount() <= SERVER_MAX_NUM_SESSIONS)
{ {
char tmpAddress[MAX_ADDR_STRING_LEN]; char tmpAddress[MAX_ADDR_STRING_LEN];
@@ -992,7 +1002,10 @@ ServerLobbyThread::InternalCheckSessionTimeouts(SessionWrapper session)
if (session.sessionData.get() && session.playerData.get()) if (session.sessionData.get() && session.playerData.get())
{ {
if (session.sessionData->GetState() == SessionData::Init && session.sessionData->GetAutoDisconnectTimerElapsedSec() >= SERVER_INIT_SESSION_TIMEOUT_SEC) if (session.sessionData->GetState() == SessionData::Init && session.sessionData->GetAutoDisconnectTimerElapsedSec() >= SERVER_INIT_SESSION_TIMEOUT_SEC)
{
LOG_VERBOSE("Session init timeout, removing session #" << session.sessionData->GetId() << ".");
closeSession = true; closeSession = true;
}
else if (session.sessionData->GetActivityTimerElapsedSec() >= SERVER_SESSION_ACTIVITY_TIMEOUT_SEC - SERVER_TIMEOUT_WARNING_REMAINING_SEC else if (session.sessionData->GetActivityTimerElapsedSec() >= SERVER_SESSION_ACTIVITY_TIMEOUT_SEC - SERVER_TIMEOUT_WARNING_REMAINING_SEC
&& !session.sessionData->HasActivityNoticeBeenSent()) && !session.sessionData->HasActivityNoticeBeenSent())
{ {
@@ -1006,15 +1019,19 @@ ServerLobbyThread::InternalCheckSessionTimeouts(SessionWrapper session)
} }
else if (session.sessionData->GetActivityTimerElapsedSec() >= SERVER_SESSION_ACTIVITY_TIMEOUT_SEC) else if (session.sessionData->GetActivityTimerElapsedSec() >= SERVER_SESSION_ACTIVITY_TIMEOUT_SEC)
{ {
LOG_VERBOSE("Activity timeout, removing session #" << session.sessionData->GetId() << ".");
closeSession = true; closeSession = true;
} }
else if (session.sessionData->GetAutoDisconnectTimerElapsedSec() >= SERVER_SESSION_FORCED_TIMEOUT_SEC) else if (session.sessionData->GetAutoDisconnectTimerElapsedSec() >= SERVER_SESSION_FORCED_TIMEOUT_SEC)
{ {
LOG_VERBOSE("Auto disconnect timeout, removing session #" << session.sessionData->GetId() << ".");
closeSession = true; closeSession = true;
} }
} }
if (closeSession) if (closeSession)
{
RemovePlayer(session.playerData->GetUniqueId(), ERR_NET_SESSION_TIMED_OUT); RemovePlayer(session.playerData->GetUniqueId(), ERR_NET_SESSION_TIMED_OUT);
}
} }
void void
@@ -1039,6 +1056,7 @@ ServerLobbyThread::SessionError(SessionWrapper session, int errorCode)
void void
ServerLobbyThread::SendError(boost::shared_ptr<SessionData> s, int errorCode) ServerLobbyThread::SendError(boost::shared_ptr<SessionData> s, int errorCode)
{ {
LOG_VERBOSE("Sending error code " << errorCode << " to session #" << s->GetId() << ".");
boost::shared_ptr<NetPacket> packet(new NetPacketError); boost::shared_ptr<NetPacket> packet(new NetPacketError);
NetPacketError::Data errorData; NetPacketError::Data errorData;
errorData.errorCode = errorCode; errorData.errorCode = errorCode;
@@ -1140,6 +1158,7 @@ ServerLobbyThread::SaveStatisticsFile()
{ {
if (m_saveStatisticsTimer.elapsed().total_seconds() >= SERVER_SAVE_STATISTICS_INTERVAL_SEC) if (m_saveStatisticsTimer.elapsed().total_seconds() >= SERVER_SAVE_STATISTICS_INTERVAL_SEC)
{ {
LOG_VERBOSE("Saving statistics.");
{ {
boost::mutex::scoped_lock lock(m_statMutex); boost::mutex::scoped_lock lock(m_statMutex);
if (m_statDataChanged) if (m_statDataChanged)
+12 -1
View File
@@ -83,12 +83,14 @@ main(int argc, char *argv[])
bool readonlyConfig = false; bool readonlyConfig = false;
string pidFile; string pidFile;
int logLevel = 1;
{ {
// Check command line options. // Check command line options.
po::options_description desc("Allowed options"); po::options_description desc("Allowed options");
desc.add_options() desc.add_options()
("help,h", "produce help message") ("help,h", "produce help message")
("version,v", "print version string") ("version,v", "print version string")
("log-level,l", po::value<int>(), "set log level (0=minimal, 1=default, 2=verbose)")
("pid-file,p", po::value<string>(), "create pid-file in different location") ("pid-file,p", po::value<string>(), "create pid-file in different location")
("readonly-config", "treat config file as read-only") ("readonly-config", "treat config file as read-only")
; ;
@@ -108,6 +110,15 @@ main(int argc, char *argv[])
<< "Network protocol version " << NET_VERSION_MAJOR << "." << NET_VERSION_MINOR << endl; << "Network protocol version " << NET_VERSION_MAJOR << "." << NET_VERSION_MINOR << endl;
return 1; return 1;
} }
if (vm.count("log-level"))
{
logLevel = vm["log-level"].as<int>();
if (logLevel < 0 || logLevel > 2)
{
cout << "Invalid log-level: \"" << logLevel << "\", allowed range 1-2." << endl;
return 1;
}
}
if (vm.count("pid-file")) if (vm.count("pid-file"))
pidFile = vm["pid-file"].as<string>(); pidFile = vm["pid-file"].as<string>();
if (vm.count("readonly-config")) if (vm.count("readonly-config"))
@@ -117,7 +128,7 @@ main(int argc, char *argv[])
auto_ptr<QtToolsInterface> myQtToolsInterface(CreateQtToolsWrapper()); auto_ptr<QtToolsInterface> myQtToolsInterface(CreateQtToolsWrapper());
//create defaultconfig //create defaultconfig
ConfigFile *myConfig = new ConfigFile(argv[0], readonlyConfig); ConfigFile *myConfig = new ConfigFile(argv[0], readonlyConfig);
loghelper_init(myQtToolsInterface->stringFromUtf8(myConfig->readConfigString("LogDir"))); loghelper_init(myQtToolsInterface->stringFromUtf8(myConfig->readConfigString("LogDir")), logLevel);
// TODO: Hack // TODO: Hack
#ifndef _WIN32 #ifndef _WIN32