FastMM documentation
FastMM C++ API 0.6.0
Public headers, tier 1 and tier 2
Loading...
Searching...
No Matches
raw_recorder.hpp
Go to the documentation of this file.
1#pragma once
2// RawRecorder: appends every raw WebSocket frame to <dir>/<venue>-<channel>.jsonl (one frame per
3// line, prefixed with the receive timestamp; binary frames as hex) so fixtures can be captured
4// from a live session (`fastmm-live --record-raw DIR`, plan 8.8). Off by default; when enabled it
5// does buffered stdio writes on the net thread, so it is a diagnostic tool, not a hot-path
6// feature.
7#include <cstddef>
8#include <cstdint>
9#include <cstdio>
10#include <span>
11#include <string>
12#include <string_view>
13
14namespace fastmm::venues {
15
16class RawRecorder {
17 public:
18 RawRecorder() = default;
19 ~RawRecorder() { close(); }
20 RawRecorder(const RawRecorder&) = delete;
23 bool open(const std::string& dir, std::string_view venue, std::string_view channel) {
24 close();
25 const std::string path = dir + "/" + std::string(venue) + "-" + std::string(channel) + ".jsonl";
26 file_ = std::fopen(path.c_str(), "a");
27 return file_ != nullptr;
28 }
29 void close() noexcept {
30 if (file_ != nullptr) {
31 std::fclose(file_);
32 file_ = nullptr;
33 }
34 }
35 [[nodiscard]] bool enabled() const noexcept { return file_ != nullptr; }
36
37 void record(std::int64_t rx_ns, std::string_view frame) noexcept {
38 if (file_ == nullptr) return;
39 std::fprintf(file_, "%lld\t", static_cast<long long>(rx_ns));
40 std::fwrite(frame.data(), 1, frame.size(), file_);
41 std::fputc('\n', file_);
42 ++frames_;
43 }
44 // Binary frame (SBE market data) as lowercase hex on one line, same timestamp prefix.
45 void record_hex(std::int64_t rx_ns, std::span<const std::byte> frame) noexcept {
46 if (file_ == nullptr) return;
47 static constexpr char kHex[] = "0123456789abcdef";
48 std::fprintf(file_, "%lld\t", static_cast<long long>(rx_ns));
49 for (const std::byte b : frame) {
50 const auto v = static_cast<unsigned>(b);
51 std::fputc(kHex[v >> 4], file_);
52 std::fputc(kHex[v & 15U], file_);
53 }
54 std::fputc('\n', file_);
55 ++frames_;
56 }
57 void flush() noexcept {
58 if (file_ != nullptr) std::fflush(file_);
59 }
60 [[nodiscard]] std::uint64_t frames() const noexcept { return frames_; }
61
62 private:
63 std::FILE* file_ = nullptr;
64 std::uint64_t frames_ = 0;
65};
66
67} // namespace fastmm::venues
Definition raw_recorder.hpp:18
bool enabled() const noexcept
Definition raw_recorder.hpp:37
void record(std::int64_t rx_ns, std::string_view frame) noexcept
Definition raw_recorder.hpp:39
void close() noexcept
Definition raw_recorder.hpp:31
std::uint64_t frames() const noexcept
Definition raw_recorder.hpp:62
void flush() noexcept
Definition raw_recorder.hpp:59
~RawRecorder()
Definition raw_recorder.hpp:21
bool open(const std::string &dir, std::string_view venue, std::string_view channel)
Definition raw_recorder.hpp:25
RawRecorder & operator=(const RawRecorder &)=delete
void record_hex(std::int64_t rx_ns, std::span< const std::byte > frame) noexcept
Binary frame (SBE market data) as lowercase hex on one line, same timestamp prefix.
Definition raw_recorder.hpp:47
Definition venue.hpp:38