Ticket #3255: t3255_use_datetime_in_simlog_directory_v2.patch

File t3255_use_datetime_in_simlog_directory_v2.patch, 2.0 KB (added by elexis, 9 years ago)

Now uses sim-log/YYYY-MM-DD_id/commands.txt. Additional info like timestamp should be saved into the commands.txt file (see #3258).

  • source/ps/Replay.cpp

     
    3131#include "scriptinterface/ScriptInterface.h"
    3232#include "scriptinterface/ScriptStats.h"
    3333#include "simulation2/Simulation2.h"
    3434#include "simulation2/helpers/SimulationCommand.h"
    3535
     36#include <ctime>
    3637#include <sstream>
    3738#include <fstream>
    3839#include <iomanip>
    3940
    40 #if MSC_VERSION
    41 #include <process.h>
    42 #define getpid _getpid // use the non-deprecated function name
    43 #endif
    44 
    4541static std::string Hexify(const std::string& s)
    4642{
    4743    std::stringstream str;
    4844    str << std::hex;
    4945    for (size_t i = 0; i < s.size(); ++i)
     
    5248}
    5349
    5450CReplayLogger::CReplayLogger(ScriptInterface& scriptInterface) :
    5551    m_ScriptInterface(scriptInterface)
    5652{
    57     // Construct the directory name based on the PID, to be relatively unique.
    58     // Append "-1", "-2" etc if we run multiple matches in a single session,
    59     // to avoid accidentally overwriting earlier logs.
    60 
    61     std::wstringstream name;
    62     name << getpid();
    63 
    64     static int run = -1;
    65     if (++run)
    66         name << "-" << run;
     53    // Get current date
     54    time_t t = time(NULL);
     55    struct tm* now = localtime(&t);
     56    char date[12];
     57    sprintf_s(date, ARRAY_SIZE(date), "%04d-%02d-%02d_", 1900+now->tm_year, 1+now->tm_mon, now->tm_mday);
     58
     59    // Construct a unique directory name based on date and sequential number per date
     60    int i = 1;
     61    OsPath path;
     62    std::stringstream pathname;
     63    do
     64    {
     65        pathname.str("");
     66        pathname.clear();
     67        pathname << date << i++;
     68        path = psLogDir() / L"sim_log" / pathname.str() / L"commands.txt";
     69    } while(DirectoryExists(path.Parent()));
    6770
    68     OsPath path = psLogDir() / L"sim_log" / name.str() / L"commands.txt";
     71    // Create directory and commands.txt
    6972    CreateDirectories(path.Parent(), 0700);
    7073    m_Stream = new std::ofstream(OsString(path).c_str(), std::ofstream::out | std::ofstream::trunc);
    7174}
    7275
    7376CReplayLogger::~CReplayLogger()