From 3932654d5d52a2099e10b4b39e367aa476d0d081 Mon Sep 17 00:00:00 2001 From: David Cernat Date: Fri, 13 Jan 2017 07:58:40 +0200 Subject: [PATCH] Use log filenames w/ timestamps instead of adding numbers to their ends --- apps/openmw-mp/main.cpp | 12 +++++++++--- apps/openmw/main.cpp | 13 ++++++++++--- components/openmw-mp/Log.cpp | 17 ----------------- components/openmw-mp/Log.hpp | 2 -- 4 files changed, 19 insertions(+), 25 deletions(-) diff --git a/apps/openmw-mp/main.cpp b/apps/openmw-mp/main.cpp index 8320c97ae..1399a8da9 100644 --- a/apps/openmw-mp/main.cpp +++ b/apps/openmw-mp/main.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -171,10 +172,15 @@ int main(int argc, char *argv[]) boost::filesystem::ofstream logfile; - // Redirect cout and cerr to openmw.log - cout << Log::renameOldLog(cfgMgr.getLogPath(), "server", "log") << endl; + // Get timestamp to add at the end of the log's filename + time_t rawtime = time(0); + struct tm *timeinfo = localtime(&rawtime); + char buffer[25]; + strftime(buffer, 25, "%Y-%m-%d-%I_%M_%S", timeinfo); + std::string timestamp(buffer); - logfile.open (boost::filesystem::path(cfgMgr.getLogPath() / "/server.log")); + // Redirect cout and cerr to tes3mp server log + logfile.open (boost::filesystem::path(cfgMgr.getLogPath() / "/tes3mp-server-" += timestamp += ".log")); coutsb.open (Tee(logfile, oldcout)); cerrsb.open (Tee(logfile, oldcerr)); diff --git a/apps/openmw/main.cpp b/apps/openmw/main.cpp index ba18c78cf..0109e84d6 100644 --- a/apps/openmw/main.cpp +++ b/apps/openmw/main.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include @@ -334,9 +335,15 @@ int main(int argc, char**argv) std::cout.rdbuf (&sb); std::cerr.rdbuf (&sb); #else - // Redirect cout and cerr to openmw.log - Log::renameOldLog(cfgMgr.getLogPath(), "openmw", "log"); - logfile.open (boost::filesystem::path(cfgMgr.getLogPath() / "/openmw.log")); + // Get timestamp to add at the end of the log's filename + time_t rawtime = time(0); + struct tm *timeinfo = localtime(&rawtime); + char buffer[25]; + strftime(buffer, 25, "%Y-%m-%d-%I_%M_%S", timeinfo); + std::string timestamp(buffer); + + // Redirect cout and cerr to tes3mp client log + logfile.open (boost::filesystem::path(cfgMgr.getLogPath() / "/tes3mp-client-" += timestamp += ".log")); coutsb.open (Tee(logfile, oldcout)); cerrsb.open (Tee(logfile, oldcerr)); diff --git a/components/openmw-mp/Log.cpp b/components/openmw-mp/Log.cpp index f5e6a8a5d..9596d0655 100644 --- a/components/openmw-mp/Log.cpp +++ b/components/openmw-mp/Log.cpp @@ -105,20 +105,3 @@ void Log::print(int level, bool hasPrefix, const char *file, int line, const cha va_end(args); cout << buf.data() << flush; } - -string Log::renameOldLog(boost::filesystem::path path, string name, string extension) -{ - namespace fs = boost::filesystem; - - std::string fullname = name + "." + extension; - - if (fs::exists(path / fullname)) - { - std::string newname; - for (int i = 1; fs::exists(path / (newname = name + boost::lexical_cast(i++) + "." + extension));); - - fs::rename(path / fullname, path / newname); - return newname; - } - return fullname; -} \ No newline at end of file diff --git a/components/openmw-mp/Log.hpp b/components/openmw-mp/Log.hpp index 02573b44c..658046dde 100644 --- a/components/openmw-mp/Log.hpp +++ b/components/openmw-mp/Log.hpp @@ -46,8 +46,6 @@ public: static const Log &Get(); static void SetLevel(int level); void print(int level, bool hasPrefix, const char *file, int line, const char *message, ...) const; - - static std::string renameOldLog(boost::filesystem::path path, std::string name, std::string extension); private: Log(int logLevel); /// Not implemented