Logger System 0.1.3
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
29// Forward declarations for worker threads (std::jthread-based)
30class network_send_jthread_worker;
31class network_reconnect_jthread_worker;
32
42public:
43 enum class protocol_type {
44 tcp,
45 udp
46 };
47
56 network_writer(const std::string& host,
57 uint16_t port,
58 protocol_type protocol = protocol_type::tcp,
59 size_t buffer_size = 8192,
60 std::chrono::seconds reconnect_interval = std::chrono::seconds(5));
61
65 ~network_writer() override;
66
73 common::VoidResult write(const log_entry& entry) override;
74
78 common::VoidResult flush() override;
79
83 std::string get_name() const override { return "network"; }
84
88 bool is_connected() const { return connected_.load(); }
89
94 uint64_t messages_sent;
95 uint64_t bytes_sent;
97 uint64_t send_failures;
98 std::chrono::system_clock::time_point last_connected;
99 std::chrono::system_clock::time_point last_error;
100 };
101
102 connection_stats get_stats() const;
103
104private:
105
106 // Network operations
107 bool connect();
108 void disconnect();
109 bool send_data(const std::string& data);
110 void process_buffer();
111 void attempt_reconnect();
112
113 // Format log for network transmission
114 std::string format_for_network(const log_entry& entry);
115
116private:
117 std::string host_;
118 uint16_t port_;
121 std::chrono::seconds reconnect_interval_;
122
123 // Socket handling
125 std::atomic<bool> connected_{false};
126 std::atomic<bool> running_{false};
127
128 // Buffering
129 std::queue<log_entry> buffer_;
130 mutable std::mutex buffer_mutex_;
131 std::condition_variable buffer_cv_;
132
133 // Worker threads (using std::jthread)
134 std::unique_ptr<network_send_jthread_worker> send_worker_;
135 std::unique_ptr<network_reconnect_jthread_worker> reconnect_worker_;
136
137 // Statistics
138 mutable std::mutex stats_mutex_;
140
141 // Helper functions
142 std::string escape_json(const std::string& str) const;
143};
144
145} // 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::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.