This is an automated email from the git hooks/post-receive script. odyx pushed a commit to branch debian/master in repository colobot.
commit 254891d8f1eecb1329dd60a7d8d583eaf174d595 Author: krzys-h <krzy...@interia.pl> Date: Sat Feb 13 20:32:26 2016 +0100 Save log to file, closes #696 --- src/app/main.cpp | 10 ++++++- src/common/logger.cpp | 77 ++++++++++++++++++++++----------------------------- src/common/logger.h | 13 ++++----- 3 files changed, 47 insertions(+), 53 deletions(-) diff --git a/src/app/main.cpp b/src/app/main.cpp index 04daf54..de3f842 100644 --- a/src/app/main.cpp +++ b/src/app/main.cpp @@ -92,11 +92,19 @@ extern "C" int main(int argc, char *argv[]) { CLogger logger; // single instance of logger + logger.AddOutput(stderr); auto systemUtils = CSystemUtils::Create(); // platform-specific utils systemUtils->Init(); - //logger.SetOutputFile(systemUtils->GetSaveDir() + "/log.txt"); + // Add file output to the logger + std::string logfile; + #if DEV_BUILD + logfile = "log.txt"; + #else + logfile = systemUtils->GetSaveDir() + "/log.txt"; + #endif + logger.AddOutput(fopen(logfile.c_str(), "w")); // Workaround for character encoding in argv on Windows #if PLATFORM_WINDOWS diff --git a/src/common/logger.cpp b/src/common/logger.cpp index e615bad..b5e5b9c 100644 --- a/src/common/logger.cpp +++ b/src/common/logger.cpp @@ -28,7 +28,6 @@ template<> CLogger* CSingleton<CLogger>::m_instance = nullptr; CLogger::CLogger() { - m_file = nullptr; #if DEV_BUILD m_logLevel = LOG_DEBUG; #else @@ -38,7 +37,10 @@ CLogger::CLogger() CLogger::~CLogger() { - Close(); + for (FILE* out : m_outputs) + { + fclose(out); + } } void CLogger::Log(LogLevel type, const char* str, va_list args) @@ -46,28 +48,34 @@ void CLogger::Log(LogLevel type, const char* str, va_list args) if (type < m_logLevel) return; - switch (type) + for (FILE* out : m_outputs) { - case LOG_TRACE: - fprintf(IsOpened() ? m_file : stderr, "[TRACE]: "); - break; - case LOG_DEBUG: - fprintf(IsOpened() ? m_file : stderr, "[DEBUG]: "); - break; - case LOG_WARN: - fprintf(IsOpened() ? m_file : stderr, "[WARN]: "); - break; - case LOG_INFO: - fprintf(IsOpened() ? m_file : stderr, "[INFO]: "); - break; - case LOG_ERROR: - fprintf(IsOpened() ? m_file : stderr, "[ERROR]: "); - break; - default: - break; + switch (type) + { + case LOG_TRACE: + fprintf(out, "[TRACE]: "); + break; + case LOG_DEBUG: + fprintf(out, "[DEBUG]: "); + break; + case LOG_WARN: + fprintf(out, "[WARN]: "); + break; + case LOG_INFO: + fprintf(out, "[INFO]: "); + break; + case LOG_ERROR: + fprintf(out, "[ERROR]: "); + break; + default: + break; + } + + va_list args2; + va_copy(args2, args); + vfprintf(out, str, args2); + va_end(args2); } - - vfprintf(IsOpened() ? m_file : stderr, str, args); } void CLogger::Trace(const char* str, ...) @@ -126,29 +134,10 @@ void CLogger::Log(LogLevel logLevel, const char* str, ...) va_end(args); } -void CLogger::SetOutputFile(std::string filename) -{ - m_filename = filename; - Open(); -} - -void CLogger::Open() -{ - m_file = fopen(m_filename.c_str(), "w"); - - if (m_file == nullptr) - fprintf(stderr, "Could not create file %s\n", m_filename.c_str()); -} - -void CLogger::Close() -{ - if (IsOpened()) - fclose(m_file); -} - -bool CLogger::IsOpened() +void CLogger::AddOutput(FILE* file) { - return m_file != nullptr; + assert(file != nullptr); + m_outputs.push_back(file); } void CLogger::SetLogLevel(LogLevel level) diff --git a/src/common/logger.h b/src/common/logger.h index f207fae..e0a935d 100644 --- a/src/common/logger.h +++ b/src/common/logger.h @@ -30,6 +30,7 @@ #include <string> #include <cstdarg> #include <cstdio> +#include <vector> /** @@ -104,9 +105,10 @@ public: void Log(LogLevel logLevel, const char *str, ...); /** Set output file to write logs to - * \param filename - output file to write to + * The given file will be automatically closed when the logger exits + * \param file - file pointer to write to */ - void SetOutputFile(std::string filename); + void AddOutput(FILE* file); /** Set log level. Logs with level below will not be shown * \param level - minimum log level to write @@ -123,13 +125,8 @@ public: static bool ParseLogLevel(const std::string& str, LogLevel& logLevel); private: - std::string m_filename; - FILE *m_file; + std::vector<FILE*> m_outputs; LogLevel m_logLevel; - - void Open(); - void Close(); - bool IsOpened(); void Log(LogLevel type, const char* str, va_list args); }; -- Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-games/colobot.git _______________________________________________ Pkg-games-commits mailing list Pkg-games-commits@lists.alioth.debian.org http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/pkg-games-commits