ORE Studio 0.0.4
Loading...
Searching...
No Matches
session_recorder.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright (C) 2025 Marco Craveiro <marco.craveiro@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 3 of the License, or (at your option) any later
8 * version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20#ifndef ORES_COMMS_RECORDING_SESSION_RECORDER_HPP
21#define ORES_COMMS_RECORDING_SESSION_RECORDER_HPP
22
23#include <mutex>
24#include <atomic>
25#include <chrono>
26#include <fstream>
27#include <cstdint>
28#include <expected>
29#include <filesystem>
30#include <boost/uuid/uuid.hpp>
31#include "ores.logging/make_logger.hpp"
32#include "ores.comms/messaging/frame.hpp"
33#include "ores.comms/recording/session_file.hpp"
34
35namespace ores::comms::recording {
36
60class session_recorder final {
61private:
62 inline static std::string_view logger_name = "ores.comms.recording.session_recorder";
63
64 static auto& lg() {
65 using namespace ores::logging;
66 static auto instance = make_logger(logger_name);
67 return instance;
68 }
69
70public:
71 session_recorder() = default;
73
74 // Non-copyable
75 session_recorder(const session_recorder&) = delete;
76 session_recorder& operator=(const session_recorder&) = delete;
77
78 // Non-movable (due to mutex)
80 session_recorder& operator=(session_recorder&&) = delete;
81
93 std::expected<std::filesystem::path, session_file_error> start(
94 const std::filesystem::path& output_directory,
95 const std::string& server_address,
96 messaging::compression_type compression = messaging::compression_type::none);
97
104 void stop();
105
109 bool is_recording() const;
110
118 void record_sent(const messaging::frame& f);
119
127 void record_received(const messaging::frame& f);
128
134 boost::uuids::uuid session_id() const;
135
141 std::filesystem::path file_path() const;
142
146 std::uint64_t frame_count() const;
147
148private:
152 session_file_error write_header(
153 const std::string& server_address,
154 messaging::compression_type compression);
155
159 void record_frame(const messaging::frame& f, frame_direction direction);
160
161 mutable std::mutex mutex_;
162 std::ofstream file_;
163 std::filesystem::path file_path_;
164 boost::uuids::uuid session_id_;
165 std::chrono::steady_clock::time_point start_time_;
166 std::atomic<bool> recording_{false};
167 std::atomic<std::uint64_t> frame_count_{0};
168};
169
170}
171
172#endif
compression_type
Compression algorithm used for payload compression.
Definition message_types.hpp:259
Session recording infrastructure for debugging and analysis.
Definition ores.comms.recording.hpp:28
session_file_error
Error codes for session file operations.
Definition session_file.hpp:119
frame_direction
Direction of a recorded frame.
Definition session_file.hpp:50
Implements logging infrastructure for ORE Studio.
Definition boost_severity.hpp:28
Complete frame with header and payload.
Definition frame.hpp:77
Records communication frames to a session file.
Definition session_recorder.hpp:60
void record_received(const messaging::frame &f)
Record a frame that was received by the client.
Definition session_recorder.cpp:112
bool is_recording() const
Check if recording is currently active.
Definition session_recorder.cpp:104
void record_sent(const messaging::frame &f)
Record a frame that was sent by the client.
Definition session_recorder.cpp:108
void stop()
Stop recording and close the session file.
Definition session_recorder.cpp:89
std::expected< std::filesystem::path, session_file_error > start(const std::filesystem::path &output_directory, const std::string &server_address, messaging::compression_type compression=messaging::compression_type::none)
Start recording to a new session file.
Definition session_recorder.cpp:36
std::uint64_t frame_count() const
Get the number of frames recorded so far.
Definition session_recorder.cpp:126
std::filesystem::path file_path() const
Get the path to the current recording file.
Definition session_recorder.cpp:121
boost::uuids::uuid session_id() const
Get the session ID for the current recording.
Definition session_recorder.cpp:116