From 1e68125a911d426df22281690f09396158ba5ded Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Fri, 16 Feb 2018 02:29:41 +0300 Subject: Replace file type from stream to FILE in logger. This will allow log data from C libs. --- src/chatlogger.cpp | 3 +- src/fs/files.cpp | 1 + src/game.cpp | 2 + src/gui/windows/chatwindow.cpp | 1 + src/gui/windows/shopwindow.cpp | 1 + src/gui/windows/updaterwindow.cpp | 1 + src/logger.cpp | 175 ++++++++++++++++++++++++-------------- src/logger.h | 7 +- src/net/download.cpp | 1 + src/net/packetlimiter.cpp | 1 + src/resources/map/map.cpp | 4 +- src/test/testmain.cpp | 2 + src/utils/xml/libxml.cpp | 2 + src/utils/xml/pugixml.cpp | 2 + src/utils/xml/tinyxml2.cpp | 2 + 15 files changed, 136 insertions(+), 69 deletions(-) (limited to 'src') diff --git a/src/chatlogger.cpp b/src/chatlogger.cpp index e2fcb13f3..45a29b709 100644 --- a/src/chatlogger.cpp +++ b/src/chatlogger.cpp @@ -28,8 +28,9 @@ #include "utils/cast.h" -#include +#include #include +#include #ifdef WIN32 #include diff --git a/src/fs/files.cpp b/src/fs/files.cpp index 76d58b7dc..98eea7cfc 100644 --- a/src/fs/files.cpp +++ b/src/fs/files.cpp @@ -37,6 +37,7 @@ #include "utils/stringutils.h" #include +#include #include #include "debug.h" diff --git a/src/game.cpp b/src/game.cpp index bb9559924..43e48943e 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -149,6 +149,8 @@ #undef ERROR #endif // WIN32 +#include + #include "debug.h" QuitDialog *quitDialog = nullptr; diff --git a/src/gui/windows/chatwindow.cpp b/src/gui/windows/chatwindow.cpp index 0b4d550dd..f9ba143bd 100644 --- a/src/gui/windows/chatwindow.cpp +++ b/src/gui/windows/chatwindow.cpp @@ -81,6 +81,7 @@ #include +#include #include #include "debug.h" diff --git a/src/gui/windows/shopwindow.cpp b/src/gui/windows/shopwindow.cpp index 9960cb15f..754cf7eb9 100644 --- a/src/gui/windows/shopwindow.cpp +++ b/src/gui/windows/shopwindow.cpp @@ -92,6 +92,7 @@ #include +#include #include #include "debug.h" diff --git a/src/gui/windows/updaterwindow.cpp b/src/gui/windows/updaterwindow.cpp index d704a404d..7f2970c78 100644 --- a/src/gui/windows/updaterwindow.cpp +++ b/src/gui/windows/updaterwindow.cpp @@ -55,6 +55,7 @@ #include +#include #include #include "debug.h" diff --git a/src/logger.cpp b/src/logger.cpp index 145b1dfb7..f068d5b52 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -78,24 +78,17 @@ #include "debug.h" #define DATESTREAM \ - timeStr << "[" \ - << ((((tv.tv_sec / 60) / 60) % 24 < 10) ? "0" : "") \ - << CAST_S32(((tv.tv_sec / 60) / 60) % 24) \ - << ":" \ - << (((tv.tv_sec / 60) % 60 < 10) ? "0" : "") \ - << CAST_S32((tv.tv_sec / 60) % 60) \ - << ":" \ - << ((tv.tv_sec % 60 < 10) ? "0" : "") \ - << CAST_S32(tv.tv_sec % 60) \ - << "." \ - << (((tv.tv_usec / 10000) % 100) < 10 ? "0" : "") \ - << CAST_S32((tv.tv_usec / 10000) % 100) \ - << "] "; + std::string timeStr = strprintf( \ + "[%02d:%02d:%02d.%02d]", \ + CAST_S32(((tv.tv_sec / 60) / 60) % 24), \ + CAST_S32((tv.tv_sec / 60) % 60), \ + CAST_S32(tv.tv_sec % 60), \ + CAST_S32((tv.tv_usec / 10000) % 100)); Logger *logger = nullptr; // Log object Logger::Logger() : - mLogFile(), + mLogFile(nullptr), mDelayedLog(), mMutex(SDL_CreateMutex()), mThreadLocked(false), @@ -110,19 +103,26 @@ Logger::Logger() : Logger::~Logger() { - if (mLogFile.is_open()) - mLogFile.close(); + closeFile(); SDL_DestroyMutex(mMutex); } +void Logger::closeFile() +{ + if (mLogFile != nullptr) + { + fclose(mLogFile); + mLogFile = nullptr; + } +} + void Logger::setLogFile(const std::string &logFilename) { - if (mLogFile.is_open()) - mLogFile.close(); + closeFile(); - mLogFile.open(logFilename.c_str(), std::ios_base::trunc); + mLogFile = fopen(logFilename.c_str(), "wt"); - if (!mLogFile.is_open()) + if (mLogFile == nullptr) { std::cout << "Warning: error while opening " << logFilename << " for writing.\n"; @@ -150,15 +150,24 @@ void Logger::dlog(const std::string &str) gettimeofday(&tv, nullptr); // Print the log entry - std::stringstream timeStr; DATESTREAM DSPECIALLOG(str.c_str()) - if (mLogFile.is_open()) - mLogFile << timeStr.str() << str << std::endl; + if (mLogFile != nullptr) + { + fprintf(mLogFile, + "%s %s\n", + timeStr.c_str(), + str.c_str()); + } if (mLogToStandardOut) - std::cout << timeStr.str() << str << std::endl; + { + fprintf(stdout, + "%s %s\n", + timeStr.c_str(), + str.c_str()); + } } void Logger::dlog2(const std::string &str, @@ -173,27 +182,27 @@ void Logger::dlog2(const std::string &str, gettimeofday(&tv, nullptr); // Print the log entry - std::stringstream timeStr; DATESTREAM DSPECIALLOG(str.c_str()) - if (mLogFile.is_open()) + if (mLogFile != nullptr) { if (comment != nullptr) { - mLogFile << timeStr.str(); - mLogFile.fill('0'); - mLogFile.width(4); - mLogFile << pos << " "; - mLogFile << str << ": " << comment << std::endl; + fprintf(mLogFile, + "%s %04d %s: %s\n", + timeStr.c_str(), + pos, + str.c_str(), + comment); } else { - mLogFile << timeStr.str(); - mLogFile.fill('0'); - mLogFile.width(4); - mLogFile << pos << " "; - mLogFile << str << std::endl; + fprintf(mLogFile, + "%s %04d %s\n", + timeStr.c_str(), + pos, + str.c_str()); } } @@ -201,19 +210,20 @@ void Logger::dlog2(const std::string &str, { if (comment != nullptr) { - std::cout << timeStr.str(); - std::cout.fill('0'); - std::cout.width(4); - std::cout << pos << " "; - std::cout << str << ": " << comment << std::endl; + fprintf(stdout, + "%s %04d %s: %s\n", + timeStr.c_str(), + pos, + str.c_str(), + comment); } else { - std::cout << timeStr.str(); - std::cout.fill('0'); - std::cout.width(4); - std::cout << pos << " "; - std::cout << str << std::endl; + fprintf(stdout, + "%s %04d %s\n", + timeStr.c_str(), + pos, + str.c_str()); } } } @@ -229,15 +239,24 @@ void Logger::log1(const char *const buf) gettimeofday(&tv, nullptr); // Print the log entry - std::stringstream timeStr; DATESTREAM SPECIALLOG(buf) - if (mLogFile.is_open()) - mLogFile << timeStr.str() << buf << std::endl; + if (mLogFile != nullptr) + { + fprintf(mLogFile, + "%s %s\n", + timeStr.c_str(), + buf); + } if (mLogToStandardOut) - std::cout << timeStr.str() << buf << std::endl; + { + fprintf(stdout, + "%s %s\n", + timeStr.c_str(), + buf); + } } void Logger::log(const char *const log_text, ...) @@ -263,15 +282,24 @@ void Logger::log(const char *const log_text, ...) gettimeofday(&tv, nullptr); // Print the log entry - std::stringstream timeStr; DATESTREAM SPECIALLOG(buf) - if (mLogFile.is_open()) - mLogFile << timeStr.str() << buf << std::endl; + if (mLogFile != nullptr) + { + fprintf(mLogFile, + "%s %s\n", + timeStr.c_str(), + buf); + } if (mLogToStandardOut) - std::cout << timeStr.str() << buf << std::endl; + { + fprintf(stdout, + "%s %s\n", + timeStr.c_str(), + buf); + } // Delete temporary buffer delete [] buf; @@ -300,15 +328,24 @@ void Logger::assertLog(const char *const log_text, ...) gettimeofday(&tv, nullptr); // Print the log entry - std::stringstream timeStr; DATESTREAM SPECIALLOG(buf) - if (mLogFile.is_open()) - mLogFile << timeStr.str() << buf << std::endl; + if (mLogFile != nullptr) + { + fprintf(mLogFile, + "%s %s\n", + timeStr.c_str(), + buf); + } if (mLogToStandardOut) - std::cout << timeStr.str() << buf << std::endl; + { + fprintf(stdout, + "%s %s\n", + timeStr.c_str(), + buf); + } DebugMessageListener::distributeEvent(buf); @@ -341,20 +378,27 @@ void Logger::log_r(const char *const log_text, ...) gettimeofday(&tv, nullptr); // Print the log entry - std::stringstream timeStr; DATESTREAM SPECIALLOG(buf) - if (mLogFile.is_open()) + if (mLogFile != nullptr) { - timeStr << buf; + std::string tmpStr = strprintf( + "%s %s\n", + timeStr.c_str(), + buf); mThreadLocked = true; - mDelayedLog.push_back(timeStr.str()); + mDelayedLog.push_back(tmpStr); mThreadLocked = false; } if (mLogToStandardOut) - std::cout << timeStr.str() << buf << std::endl; + { + fprintf(stdout, + "%s %s\n", + timeStr.c_str(), + buf); + } // Delete temporary buffer delete [] buf; @@ -368,7 +412,10 @@ void Logger::flush() { SDL_mutexP(mMutex); FOR_EACH (STD_VECTOR::const_iterator, it, mDelayedLog) - mLogFile << *it << std::endl; + { + fputs((*it).c_str(), mLogFile); + fputs("\n", mLogFile); + } mDelayedLog.clear(); SDL_mutexV(mMutex); } diff --git a/src/logger.h b/src/logger.h index 91602bcd8..580043108 100644 --- a/src/logger.h +++ b/src/logger.h @@ -32,7 +32,8 @@ PRAGMA48(GCC diagnostic ignored "-Wshadow") #include PRAGMA48(GCC diagnostic pop) -#include +#include +#include #ifdef ENABLEDEBUGLOG #define DEBUGLOG(str) \ @@ -175,6 +176,8 @@ class Logger final const char* const comment); #endif // ENABLEDEBUGLOG + void closeFile(); + void setDebugLog(const bool n) { mDebugLog = n; } @@ -211,7 +214,7 @@ class Logger final const uint32_t id3) const; private: - std::ofstream mLogFile; + FILE *mLogFile; STD_VECTOR mDelayedLog; SDL_mutex *mMutex; volatile bool mThreadLocked; diff --git a/src/net/download.cpp b/src/net/download.cpp index e3f91388d..9fa617fbf 100644 --- a/src/net/download.cpp +++ b/src/net/download.cpp @@ -32,6 +32,7 @@ #include +#include #include #include "debug.h" diff --git a/src/net/packetlimiter.cpp b/src/net/packetlimiter.cpp index 92cf980ff..50279b22c 100644 --- a/src/net/packetlimiter.cpp +++ b/src/net/packetlimiter.cpp @@ -27,6 +27,7 @@ #include "utils/checkutils.h" #include "utils/timer.h" +#include #include #include "debug.h" diff --git a/src/resources/map/map.cpp b/src/resources/map/map.cpp index baddf468b..1e97597bf 100644 --- a/src/resources/map/map.cpp +++ b/src/resources/map/map.cpp @@ -68,11 +68,11 @@ #include "utils/foreach.h" #include "utils/timer.h" -#include - #include #include +#include +#include #include "debug.h" diff --git a/src/test/testmain.cpp b/src/test/testmain.cpp index d0a0cb862..43c2c3f62 100644 --- a/src/test/testmain.cpp +++ b/src/test/testmain.cpp @@ -31,6 +31,8 @@ #include "utils/delete2.h" #include "utils/process.h" +#include + #include "debug.h" std::string fileName; diff --git a/src/utils/xml/libxml.cpp b/src/utils/xml/libxml.cpp index 5d1b4d90a..7fcdfd7fc 100644 --- a/src/utils/xml/libxml.cpp +++ b/src/utils/xml/libxml.cpp @@ -33,6 +33,8 @@ #include "utils/translation/podict.h" +#include + #include "debug.h" namespace diff --git a/src/utils/xml/pugixml.cpp b/src/utils/xml/pugixml.cpp index 01ea0940f..4919b34a8 100644 --- a/src/utils/xml/pugixml.cpp +++ b/src/utils/xml/pugixml.cpp @@ -34,6 +34,8 @@ #include "utils/translation/podict.h" +#include + #include "debug.h" namespace diff --git a/src/utils/xml/tinyxml2.cpp b/src/utils/xml/tinyxml2.cpp index 0a3053a41..48ea9c891 100644 --- a/src/utils/xml/tinyxml2.cpp +++ b/src/utils/xml/tinyxml2.cpp @@ -33,6 +33,8 @@ #include "utils/translation/podict.h" +#include + #include "debug.h" namespace -- cgit v1.2.3-70-g09d2