diff options
author | Andrei Karas <akaras@inbox.ru> | 2014-01-14 00:56:03 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2014-01-14 00:56:20 +0300 |
commit | 8e4f4e04ca61594e3dec31bad70eaf6cdf86ad8f (patch) | |
tree | b246d23998da0af196460f42af22b7c89b845b2c /src | |
parent | c28aa31fe3d701530280ac92c92e8dff36e65f03 (diff) | |
download | mv-8e4f4e04ca61594e3dec31bad70eaf6cdf86ad8f.tar.gz mv-8e4f4e04ca61594e3dec31bad70eaf6cdf86ad8f.tar.bz2 mv-8e4f4e04ca61594e3dec31bad70eaf6cdf86ad8f.tar.xz mv-8e4f4e04ca61594e3dec31bad70eaf6cdf86ad8f.zip |
add support for thread safe writing to log.
Diffstat (limited to 'src')
-rw-r--r-- | src/gui/gui.cpp | 10 | ||||
-rw-r--r-- | src/gui/gui.h | 1 | ||||
-rw-r--r-- | src/logger.cpp | 58 | ||||
-rw-r--r-- | src/logger.h | 22 |
4 files changed, 91 insertions, 0 deletions
diff --git a/src/gui/gui.cpp b/src/gui/gui.cpp index f84750e17..8dbaec952 100644 --- a/src/gui/gui.cpp +++ b/src/gui/gui.cpp @@ -49,6 +49,7 @@ #include "resources/resourcemanager.h" #include "utils/langs.h" +#include "utils/timer.h" #include <guichan/exception.hpp> @@ -107,6 +108,7 @@ Gui::Gui() : mFocusListeners(), mForegroundColor(Theme::getThemeColor(Theme::TEXT)), mForegroundColor2(Theme::getThemeColor(Theme::TEXT_OUTLINE)), + mTime(0), mCustomCursor(false), mDoubleClick(true) { @@ -366,6 +368,14 @@ void Gui::slowLogic() mNpcFont->slowLogic(5); if (windowContainer) windowContainer->slowLogic(); + + const int time = cur_time; + if (mTime != time) + { + logger->flush(); + mTime = time; + } + BLOCK_END("Gui::slowLogic") } diff --git a/src/gui/gui.h b/src/gui/gui.h index 56895b699..7c34fc5cf 100644 --- a/src/gui/gui.h +++ b/src/gui/gui.h @@ -197,6 +197,7 @@ class Gui final : public gcn::Gui FocusListenerList mFocusListeners; gcn::Color mForegroundColor; gcn::Color mForegroundColor2; + int mTime; bool mCustomCursor; /**< Show custom cursor */ bool mDoubleClick; }; diff --git a/src/logger.cpp b/src/logger.cpp index bac895954..a4cc5096b 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -64,6 +64,9 @@ Logger *logger = nullptr; // Log object Logger::Logger() : mLogFile(), + mDelayedLog(), + mMutex(SDL_CreateMutex()), + mThreadLocked(false), mLogToStandardOut(true), mDebugLog(false) { @@ -73,6 +76,7 @@ Logger::~Logger() { if (mLogFile.is_open()) mLogFile.close(); + SDL_DestroyMutex(mMutex); } void Logger::setLogFile(const std::string &logFilename) @@ -169,6 +173,60 @@ void Logger::log(const char *const log_text, ...) delete [] buf; } +void Logger::log_r(const char *const log_text, ...) +{ + SDL_mutexP(mMutex); + + unsigned size = 1024; + if (strlen(log_text) * 3 > size) + size = static_cast<unsigned>(strlen(log_text) * 3); + + char* buf = new char[size + 1]; + va_list ap; + + // Use a temporary buffer to fill in the variables + va_start(ap, log_text); + vsnprintf(buf, size, log_text, ap); + buf[size] = 0; + va_end(ap); + + // Get the current system time + timeval tv; + gettimeofday(&tv, nullptr); + + // Print the log entry + std::stringstream timeStr; + DATESTREAM + LOG_ANDROID(buf) + + if (mLogFile.is_open()) + { + timeStr << buf << std::endl; + mThreadLocked = true; + mDelayedLog.push_back(timeStr.str()); + mThreadLocked = false; + } + + if (mLogToStandardOut) + std::cout << timeStr.str() << buf << std::endl; + + // Delete temporary buffer + delete [] buf; + + SDL_mutexV(mMutex); +} + +void Logger::flush() +{ + if (!mThreadLocked) + { + SDL_mutexP(mMutex); + FOR_EACH (std::vector<std::string>::const_iterator, it, mDelayedLog) + mLogFile << *it; + SDL_mutexV(mMutex); + } +} + // here string must be safe for any usage void Logger::safeError(const std::string &error_text) { diff --git a/src/logger.h b/src/logger.h index 0941a0e63..c68277ce5 100644 --- a/src/logger.h +++ b/src/logger.h @@ -24,7 +24,11 @@ #define LOGGER_H #include "main.h" + +#include <SDL_thread.h> + #include <fstream> +#include <vector> #include "localconsts.h" @@ -79,6 +83,19 @@ class Logger final ; /** + * Enters a message in the log (thread safe). + */ + void log_r(const char *const log_text, ...) +#ifdef __GNUC__ +#ifdef __OpenBSD__ + __attribute__((__format__(printf, 2, 3))) +#else + __attribute__((__format__(gnu_printf, 2, 3))) +#endif +#endif + ; + + /** * Enters a message in the log. The message will be timestamped. */ void log1(const char *const log_text); @@ -88,6 +105,8 @@ class Logger final */ void log(const std::string &str); + void flush(); + #ifdef ENABLEDEBUGLOG /** * Enters debug message in the log. The message will be timestamped. @@ -113,6 +132,9 @@ class Logger final private: std::ofstream mLogFile; + std::vector<std::string> mDelayedLog; + SDL_mutex *mMutex; + volatile bool mThreadLocked; bool mLogToStandardOut; bool mDebugLog; }; |