18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-20 10:24:58 +01:00
Files
maszyna/utilities/physicslog.h
maj00r 03a3b4c09a feat(physicslog): the logs written every two seconds by a thread of their own, no file kept open; the black box's tapes as it goes
"physicslog yes" kept the .dat of every vehicle with a driver open for the whole run and flushed it twenty
times a second from the simulation's thread: some 580 open files on Tomaszewo. The lines are kept now and a
thread of their own writes them every two seconds, each file opened only for the moment it is written to.
The files, their lines and their text mode stay as they were.

- The black box, with the physics logs off, writes the tapes of the user's vehicles through the same
  writer as it goes, not at the end. A log begins when the user is first in the vehicle and goes on
  while the user looks around outside, until another vehicle is taken; back in it, the log goes on after
  the gap. The vehicle's own driver keeps the log only if it is the primary one, as a passive one's clock
  stands still; otherwise the driver of its train does.
- What is kept is written when the simulator closes, after the black box, at the end of a test run, and
  when the test watchdog ends a run. A crash loses the last two seconds.
- On Tomaszewo with the black box alone, the one tape grows every two seconds. With "physicslog yes", the
  581 logs grow and the simulator holds as many handles as without them; writing them takes the thread
  about 1.9 s, which the closing waits for once.
2026-11-14 00:48:17 +01:00

68 lines
2.4 KiB
C++

/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include <chrono>
#include <condition_variable>
#include <filesystem>
#include <map>
#include <mutex>
#include <set>
#include <stop_token>
#include <string>
#include <string_view>
#include <thread>
// the physics logs, <folder>/<name>.dat. the lines given are kept, and written every two seconds by a thread of its
// own, each file opened only for the moment it is written to: the logs of hundreds of vehicles hold no files open, and
// the simulation doesn't wait for the disk. a crash loses the last two seconds of them
class physicslog_writer {
public:
explicit physicslog_writer(std::filesystem::path Folder = "physicslog", std::chrono::milliseconds Interval = std::chrono::seconds{2});
physicslog_writer(physicslog_writer const &) = delete;
physicslog_writer &operator=(physicslog_writer const &) = delete;
// stops, writing what is kept
~physicslog_writer();
// starts the log Name anew: its file is emptied and Header written into it, the next time the logs are written.
// the lines of it not written yet are dropped
void begin(std::string const &Name, std::string_view Header);
// Line at the end of the log Name. a log not begun is added to as its file stands
void append(std::string const &Name, std::string_view Line);
// writes what is kept, now
void flush();
// writes what is kept and ends the thread. what is given after is written by a flush, or at the end
void stop();
private:
struct pending {
bool fresh{false}; // its file is to be emptied first
std::string text;
};
// the thread, on the first line given, unless stopped. called with m_mutex held
void start();
void run(std::stop_token const &Stop);
void write();
std::filesystem::path const m_folder;
std::chrono::milliseconds const m_interval;
std::mutex m_mutex; // the lines kept and the thread
std::map<std::string, pending> m_pending;
bool m_stopped{false};
std::condition_variable_any m_wake;
std::jthread m_thread;
std::mutex m_filemutex; // the files are written one batch at a time, so the lines of a log stay in order
std::set<std::string> m_failed; // the logs whose file couldn't be written, told once
};
// the physics logs of the drivers (physicslog yes), and those the black box writes of the user's vehicles
extern physicslog_writer PhysicsLogs;