From 65a3f969bf75fa1238579592e46cff6fb2cdc599 Mon Sep 17 00:00:00 2001 From: lotodore Date: Thu, 22 Nov 2007 17:25:55 +0000 Subject: [PATCH] Log to file as dedicated server (do not use syslog any more). --- src/core/common/loghelper_client.cpp | 6 +++ src/core/common/loghelper_server.cpp | 55 ++++++++++++++++++++-------- src/core/loghelper.h | 2 + src/pokerth_server.cpp | 3 +- 4 files changed, 49 insertions(+), 17 deletions(-) diff --git a/src/core/common/loghelper_client.cpp b/src/core/common/loghelper_client.cpp index cfbc457f..0dba8ddc 100644 --- a/src/core/common/loghelper_client.cpp +++ b/src/core/common/loghelper_client.cpp @@ -27,6 +27,12 @@ using namespace std; +void +loghelper_init(const std::string & /*logDir*/) +{ + // Do not log to file as client. +} + void internal_log_err(const string &msg) { diff --git a/src/core/common/loghelper_server.cpp b/src/core/common/loghelper_server.cpp index 822aa084..5b06b7f1 100644 --- a/src/core/common/loghelper_server.cpp +++ b/src/core/common/loghelper_server.cpp @@ -21,34 +21,57 @@ #error This file is only for the server. #endif -#include -#ifdef _WIN32 - #include -#else - #include - #include +#ifdef _MSC_VER +#pragma warning(push) +#pragma warning(disable : 4100) #endif +#include +#include +#include +#include + using namespace std; +using namespace boost::filesystem; +using namespace boost::posix_time; + +#define SERVER_MSG_LOG_FILE_NAME "server_messages.log" + +static string g_logFile; + +void +loghelper_init(const string &logDir) +{ + path tmpLogFile(logDir); + tmpLogFile /= SERVER_MSG_LOG_FILE_NAME; + + g_logFile = tmpLogFile.directory_string(); +} void internal_log_err(const string &msg) { -#ifdef _WIN32 - cout << msg; -#else - syslog(LOG_ERR, "%s", msg.c_str()); -#endif + if (!g_logFile.empty()) + { + ofstream o(g_logFile.c_str(), ios_base::out | ios_base::app); + if (!o.fail()) + o << second_clock::local_time() << " ERR: " << msg; + } } void internal_log_msg(const std::string &msg) { -#ifdef _WIN32 - cout << msg; -#else - syslog(LOG_INFO, "%s", msg.c_str()); -#endif + if (!g_logFile.empty()) + { + ofstream o(g_logFile.c_str(), ios_base::out | ios_base::app); + if (!o.fail()) + o << second_clock::local_time() << " MSG: " << msg; + } } +#ifdef _MSC_VER +#pragma warning(pop) +#endif + diff --git a/src/core/loghelper.h b/src/core/loghelper.h index e530aa9b..b75eff86 100644 --- a/src/core/loghelper.h +++ b/src/core/loghelper.h @@ -24,6 +24,8 @@ #include #include +void loghelper_init(const std::string &logDir); + void internal_log_err(const std::string &msg); void internal_log_msg(const std::string &msg); diff --git a/src/pokerth_server.cpp b/src/pokerth_server.cpp index 7beb54f8..3b4d0785 100644 --- a/src/pokerth_server.cpp +++ b/src/pokerth_server.cpp @@ -73,6 +73,7 @@ main(int argc, char *argv[]) //create defaultconfig ConfigFile *myConfig = new ConfigFile(argc, argv); + loghelper_init(myConfig->readConfigString("LogDir")); // TODO: Hack #ifndef _WIN32 @@ -103,7 +104,7 @@ main(int argc, char *argv[]) } myServerGuiInterface->getSession().terminateNetworkServer(); - LOG_MSG("Terminating PokerTH dedicated server."); + LOG_MSG("Terminating PokerTH dedicated server." << endl); socket_cleanup(); return 0; }