Logger System 1.0.0
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
network_writer.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
12#pragma once
13
14#include "base_writer.h"
17
19
20#include <queue>
21#include <condition_variable>
22#include <atomic>
23#include <memory>
24#include <mutex>
25#include <cstdint>
26
27namespace kcenon::logger {
28
29namespace security { class integrity_policy; }
30
31// Forward declarations for worker threads (std::jthread-based)
32class network_send_jthread_worker;
33class network_reconnect_jthread_worker;
34
44public:
45 enum class protocol_type {
46 tcp,
47 udp
48 };
49
58 network_writer(const std::string& host,
59 uint16_t port,
60 protocol_type protocol = protocol_type::tcp,
61 size_t buffer_size = 8192,
62 std::chrono::seconds reconnect_interval = std::chrono::seconds(5));
63
67 ~network_writer() override;
68
75 common::VoidResult write(const log_entry& entry) override;
76
80 common::VoidResult flush() override;
81
85 std::string get_name() const override { return "network"; }
86
90 bool is_connected() const { return connected_.load(); }
91
96 uint64_t messages_sent;
97 uint64_t bytes_sent;
99 uint64_t send_failures;
100 std::chrono::system_clock::time_point last_connected;
101 std::chrono::system_clock::time_point last_error;
102 };
103
104 connection_stats get_stats() const;
105
116 void set_integrity_policy(std::shared_ptr<security::integrity_policy> policy);
117
118private:
119
120 // Network operations
121 bool connect();
122 void disconnect();
123 bool send_data(const std::string& data);
124 void process_buffer();
125 void attempt_reconnect();
126
127 // Format log for network transmission
128 std::string format_for_network(const log_entry& entry);
129
130private:
131 std::string host_;
132 uint16_t port_;
135 std::chrono::seconds reconnect_interval_;
136
137 // Socket handling
139 std::atomic<bool> connected_{false};
140 std::atomic<bool> running_{false};
141
142 // Buffering
143 std::queue<log_entry> buffer_;
144 mutable std::mutex buffer_mutex_;
145 std::condition_variable buffer_cv_;
146
147 // Worker threads (using std::jthread)
148 std::unique_ptr<network_send_jthread_worker> send_worker_;
149 std::unique_ptr<network_reconnect_jthread_worker> reconnect_worker_;
150
151 // Statistics
152 mutable std::mutex stats_mutex_;
154
155 // Integrity policy (Issue #612) - optional, set before start.
156 std::shared_ptr<security::integrity_policy> integrity_policy_;
157
158 // Helper functions
159 std::string escape_json(const std::string& str) const;
160};
161
162} // namespace kcenon::logger
Abstract base class for all log output writers kcenon.
Abstract base class for all log output writers.
Definition base_writer.h:95
Sends logs over network (TCP/UDP)
std::shared_ptr< security::integrity_policy > integrity_policy_
std::chrono::seconds reconnect_interval_
std::condition_variable buffer_cv_
std::unique_ptr< network_reconnect_jthread_worker > reconnect_worker_
bool is_connected() const
Check if connected.
std::string get_name() const override
Get writer name.
std::queue< log_entry > buffer_
std::unique_ptr< network_send_jthread_worker > send_worker_
Data structures for representing log entries and source locations kcenon.
DLL export/import macros for logger_system shared library support.
#define LOGGER_SYSTEM_API
Tag interface for asynchronous writers.
Represents a single log entry with all associated metadata.
Definition log_entry.h:155
std::chrono::system_clock::time_point last_connected
std::chrono::system_clock::time_point last_error
Writer category interfaces and type traits kcenon.