ORE Studio 0.0.4
Loading...
Searching...
No Matches
hybrid_log_exporter.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_TELEMETRY_EXPORTING_HYBRID_LOG_EXPORTER_HPP
21#define ORES_TELEMETRY_EXPORTING_HYBRID_LOG_EXPORTER_HPP
22
23#include <atomic>
24#include <chrono>
25#include <condition_variable>
26#include <cstdint>
27#include <filesystem>
28#include <fstream>
29#include <functional>
30#include <memory>
31#include <mutex>
32#include <thread>
33#include <vector>
34#include "ores.telemetry/exporting/log_exporter.hpp"
35#include "ores.telemetry/exporting/telemetry_options.hpp"
36#include "ores.telemetry/exporting/upload_position_tracker.hpp"
37
39
51 std::function<bool(std::vector<domain::log_record>)>;
52
68class hybrid_log_exporter final : public log_exporter {
69public:
78 hybrid_log_exporter(const std::filesystem::path& file_path,
79 telemetry_options options,
80 send_records_callback send_callback = nullptr);
81
82 ~hybrid_log_exporter() override;
83
85 hybrid_log_exporter& operator=(const hybrid_log_exporter&) = delete;
86
96 void export_record(domain::log_record record) override;
97
104 void flush() override;
105
111 void shutdown() override;
112
121 void set_connected(bool connected);
122
126 std::uint64_t upload_position() const;
127
128private:
132 void write_to_file(const domain::log_record& record);
133
137 void add_to_batch(domain::log_record record);
138
144 bool send_batch();
145
149 void flush_thread_func();
150
151 // File export
152 std::mutex file_mutex_;
153 std::ofstream file_;
154 std::filesystem::path file_path_;
155 std::uint64_t file_position_ = 0;
156
157 // Options
158 telemetry_options options_;
159 send_records_callback send_callback_;
160
161 // Streaming state
162 std::mutex batch_mutex_;
163 std::vector<domain::log_record> pending_batch_;
164 upload_position_tracker position_tracker_;
165 std::atomic<bool> connected_{false};
166
167 // Background flush thread
168 std::thread flush_thread_;
169 std::condition_variable flush_cv_;
170 std::atomic<bool> shutdown_requested_{false};
171 std::chrono::steady_clock::time_point last_flush_time_;
172};
173
174}
175
176#endif
Log export functionality for telemetry.
Definition file_log_exporter.hpp:28
std::function< bool(std::vector< domain::log_record >)> send_records_callback
Callback type for sending batched records to the server.
Definition hybrid_log_exporter.hpp:51
A log record with trace correlation.
Definition log_record.hpp:46
Hybrid exporter that writes to file and optionally streams to server.
Definition hybrid_log_exporter.hpp:68
void flush() override
Flushes pending records.
Definition hybrid_log_exporter.cpp:126
void export_record(domain::log_record record) override
Exports a log record.
Definition hybrid_log_exporter.cpp:116
void shutdown() override
Shuts down the exporter.
Definition hybrid_log_exporter.cpp:139
void set_connected(bool connected)
Updates the connection status.
Definition hybrid_log_exporter.cpp:161
std::uint64_t upload_position() const
Returns the current upload position.
Definition hybrid_log_exporter.cpp:170
Interface for exporting telemetry log records.
Definition log_exporter.hpp:36
Options related to telemetry export.
Definition telemetry_options.hpp:38
Tracks the upload position for telemetry log files.
Definition upload_position_tracker.hpp:43