diff --git a/source/ps/Replay.cpp b/source/ps/Replay.cpp index ee9c67248f..ccd291d773 100644 --- a/source/ps/Replay.cpp +++ b/source/ps/Replay.cpp @@ -30,20 +30,18 @@ #include "ps/Profile.h" #include "ps/ProfileViewer.h" #include "ps/Pyrogenesis.h" +#include "ps/Util.h" +#include "ps/VisualReplay.h" #include "scriptinterface/ScriptInterface.h" #include "scriptinterface/ScriptStats.h" #include "simulation2/Simulation2.h" #include "simulation2/helpers/SimulationCommand.h" +#include #include #include #include -#if MSC_VERSION -#include -#define getpid _getpid // use the non-deprecated function name -#endif - static std::string Hexify(const std::string& s) { std::stringstream str; @@ -72,20 +70,8 @@ void CReplayLogger::StartGame(JS::MutableHandleValue attribs) m_ScriptInterface.SetProperty(attribs, "engine_version", CStr(engine_version)); m_ScriptInterface.SetProperty(attribs, "mods", g_modsLoaded); - // Construct the directory name based on the PID, to be relatively unique. - // Append "-1", "-2" etc if we run multiple matches in a single session, - // to avoid accidentally overwriting earlier logs. - static int run = -1; - do - { - std::wstringstream name; - name << getpid(); - if (++run) - name << "-" << run; - - m_Directory = psLogDir() / L"sim_log" / name.str(); - } while (DirectoryExists(m_Directory)); - + m_Directory = getDateIndexSubdirectory(VisualReplay::GetDirectoryName()); + debug_printf("Writing replay to %s\n", m_Directory.string8().c_str()); CreateDirectories(m_Directory, 0700); m_Stream = new std::ofstream(OsString(m_Directory / L"commands.txt").c_str(), std::ofstream::out | std::ofstream::trunc); diff --git a/source/ps/Util.cpp b/source/ps/Util.cpp index 17c58d13b9..f46cd84338 100644 --- a/source/ps/Util.cpp +++ b/source/ps/Util.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2014 Wildfire Games. +/* Copyright (C) 2016 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -193,6 +193,26 @@ Status tex_write(Tex* t, const VfsPath& filename) return ret; } +/** + * Return an unused directory, based on date and index (for example 2016-02-09_0001) + */ +OsPath getDateIndexSubdirectory(const OsPath& parentDir) +{ + const std::time_t timestamp = std::time(nullptr); + const struct std::tm* now = std::localtime(×tamp); + + int i = 0; + OsPath path; + char directory[256]; + + do + { + sprintf(directory, "%04d-%02d-%02d_%04d", now->tm_year+1900, now->tm_mon+1, now->tm_mday, ++i); + path = parentDir / CStr(directory); + } while (DirectoryExists(path) || FileExists(path)); + + return path; +} static size_t s_nextScreenshotNumber; diff --git a/source/ps/Util.h b/source/ps/Util.h index b187dd26a1..890d0d783b 100644 --- a/source/ps/Util.h +++ b/source/ps/Util.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2009 Wildfire Games. +/* Copyright (C) 2016 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -26,6 +26,8 @@ extern void WriteSystemInfo(); extern const wchar_t* ErrorString(int err); +extern OsPath getDateIndexSubdirectory(const OsPath& parentDir); + extern void WriteScreenshot(const VfsPath& extension); extern void WriteBigScreenshot(const VfsPath& extension, int tiles); diff --git a/source/ps/VisualReplay.cpp b/source/ps/VisualReplay.cpp index be48949924..81322b5d7e 100644 --- a/source/ps/VisualReplay.cpp +++ b/source/ps/VisualReplay.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2015 Wildfire Games. +/* Copyright (C) 2016 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -27,8 +27,11 @@ #include "ps/CLogger.h" #include "ps/Filesystem.h" #include "ps/Game.h" +#include "ps/GameSetup/Paths.h" +#include "ps/Mod.h" #include "ps/Pyrogenesis.h" #include "ps/Replay.h" +#include "ps/Util.h" #include "scriptinterface/ScriptInterface.h" /** @@ -38,7 +41,8 @@ const u8 minimumReplayDuration = 3; OsPath VisualReplay::GetDirectoryName() { - return OsPath(psLogDir() / L"sim_log"); + const Paths paths(g_args); + return OsPath(paths.UserData() / "replays" / engine_version); } void VisualReplay::StartVisualReplay(const CStrW& directory) diff --git a/source/simulation2/Simulation2.cpp b/source/simulation2/Simulation2.cpp index 2c99692112..7df98104fd 100644 --- a/source/simulation2/Simulation2.cpp +++ b/source/simulation2/Simulation2.cpp @@ -41,15 +41,11 @@ #include "ps/Loader.h" #include "ps/Profile.h" #include "ps/Pyrogenesis.h" +#include "ps/Util.h" #include "ps/XML/Xeromyces.h" #include -#if MSC_VERSION -#include -#define getpid _getpid // use the non-deprecated function name -#endif - static std::string Hexify(const std::string& s) // TODO: shouldn't duplicate this function in so many places { std::stringstream str; @@ -79,6 +75,11 @@ public: CFG_GET_VAL("ooslog", m_EnableOOSLog); CFG_GET_VAL("serializationtest", m_EnableSerializationTest); } + + m_OOSLogPath = getDateIndexSubdirectory(psLogDir() / "oos_logs"); + + if (m_EnableOOSLog) + debug_printf("Writing ooslogs to %s\n", m_OOSLogPath.string8().c_str()); } ~CSimulation2Impl() @@ -132,7 +133,7 @@ public: uint32_t m_TurnNumber; bool m_EnableOOSLog; - + OsPath m_OOSLogPath; // Functions and data for the serialization test mode: (see Update() for relevant comments) @@ -295,7 +296,8 @@ void CSimulation2Impl::ReportSerializationFailure( SerializationTestState* primaryStateBefore, SerializationTestState* primaryStateAfter, SerializationTestState* secondaryStateBefore, SerializationTestState* secondaryStateAfter) { - OsPath path = psLogDir() / "oos_log"; + const OsPath path = getDateIndexSubdirectory(psLogDir() / "serializationtest"); + debug_printf("Writing serializationtest-data to %s\n", path.string8().c_str()); CreateDirectories(path, 0700); // Clean up obsolete files from previous runs @@ -549,11 +551,9 @@ void CSimulation2Impl::DumpState() { PROFILE("DumpState"); - std::stringstream pid; - pid << getpid(); std::stringstream name;\ name << std::setw(5) << std::setfill('0') << m_TurnNumber << ".txt"; - OsPath path = psLogDir() / "sim_log" / pid.str() / name.str(); + const OsPath path = m_OOSLogPath / name.str(); CreateDirectories(path.Parent(), 0700); std::ofstream file (OsString(path).c_str(), std::ofstream::out | std::ofstream::trunc); @@ -589,6 +589,7 @@ CSimulation2::~CSimulation2() void CSimulation2::EnableOOSLog() { m->m_EnableOOSLog = true; + debug_printf("Writing ooslogs to %s\n", m->m_OOSLogPath.string8().c_str()); } void CSimulation2::EnableSerializationTest()