Fray Engine
Loading...
Searching...
No Matches
engine_log_sink.hpp
Go to the documentation of this file.
1/* engine_log_sink.hpp */
2
3#ifndef _FRAY_ENGINE_LOG_SINK_HPP_
4#define _FRAY_ENGINE_LOG_SINK_HPP_
5
6#include <chrono>
7#include <condition_variable>
8#include <cstdint>
9#include <cstdio>
10#include <memory>
11#include <mutex>
12#include <stdexcept>
13#include <string>
14#include <vector>
15
16#include "log_manager.hpp"
17#include "fray_manager.hpp"
18#include "ipc_manager.hpp"
19
20namespace fray
21{
22 namespace log
23 {
29 {
31 struct Captured
32 {
33 std::string level;
35 std::string project;
36 std::string text;
37 std::uint64_t arrivedUs;
39 };
40
42 struct State
43 {
44 std::mutex mutex;
45 std::condition_variable idle;
46 std::vector<Captured> pending;
47 FrayManager* app = nullptr;
48 ipc::IPCManager* ipc = nullptr;
49 std::size_t active = 0;
50 bool forwarding = true;
51 bool detached = false;
52 };
53
56 {
57 std::shared_ptr<State> state;
60 {
61 std::lock_guard<std::mutex> lock(state->mutex);
62 if (--state->active == 0)
63 state->idle.notify_all();
64 }
65 };
66
69 {
70 bool& busy;
72 ~RecursionGuard() { busy = false; }
73 };
74
75 std::shared_ptr<State> _state = std::make_shared<State>();
76
81 void detach()
82 {
83 std::unique_lock<std::mutex> lock(_state->mutex);
84 if (_state->detached)
85 return;
86 _state->detached = true;
87 lock.unlock();
88 setSink(nullptr);
89 lock.lock();
90 _state->idle.wait(lock, [this] { return _state->active == 0; });
91 if (!_state->pending.empty())
92 {
93 std::fprintf(stderr, "Cannot store %zu startup log entries: log sink was never attached.\n",
94 _state->pending.size());
95 _state->pending.clear();
96 }
97 _state->app = nullptr;
98 _state->ipc = nullptr;
99 }
100
101 public:
106 {
107 setSink([state = _state](const F_char*, const F_char* level, F_long thread,
108 const std::string& project, const F_char* message, bool consoleVisible)
109 {
110 thread_local bool busy = false;
111 if (busy)
112 return;
113 busy = true;
114 RecursionGuard guard{busy};
115 const auto now = std::chrono::system_clock::now().time_since_epoch();
116 Captured entry{level, thread, project, message, static_cast<std::uint64_t>(
117 std::chrono::duration_cast<std::chrono::microseconds>(now).count()), consoleVisible};
118 FrayManager* app;
120 {
121 std::lock_guard<std::mutex> lock(state->mutex);
122 if (state->detached)
123 return;
124 if (!state->app)
125 {
126 state->pending.push_back(std::move(entry));
127 return;
128 }
129 app = state->app;
130 ipc = state->forwarding ? state->ipc : nullptr;
131 ++state->active;
132 }
133 ActiveCall active{state};
134 app->recordEngineLog(entry.level.c_str(), entry.thread, entry.project, entry.text.c_str(), ipc,
135 entry.arrivedUs, entry.consoleVisible);
136 });
137 }
138
139 EngineLogSink(const EngineLogSink&) = delete;
141
144
150 {
152 public:
161 EngineLogSink& sink,
162 FrayManager& app,
164 : _sink(sink)
165 {
166 std::lock_guard<std::mutex> lock(sink._state->mutex);
167 if (sink._state->app || sink._state->detached)
168 throw std::logic_error("Engine log sink is already attached or detached.");
169 for (const auto& entry : sink._state->pending)
170 app.recordEngineLog(entry.level.c_str(), entry.thread, entry.project, entry.text.c_str(), &ipc,
171 entry.arrivedUs, entry.consoleVisible);
172 sink._state->pending.clear();
173 sink._state->app = &app;
174 sink._state->ipc = &ipc;
175 }
176 Attachment(const Attachment&) = delete;
177 Attachment& operator=(const Attachment&) = delete;
179 ~Attachment() { _sink.detach(); }
180 };
181
186 {
187 std::lock_guard<std::mutex> lock(_state->mutex);
188 _state->forwarding = false;
189 }
190 };
191 }
192}
193
194#endif
Owns and coordinates projects, their queues, application work, prompts, and logs. Project work runs o...
void recordEngineLog(const char *level, F_long thread, const std::string &project, const char *text, ipc::IPCManager *ipc, std::uint64_t arrivedUs=0, bool consoleVisible=true)
Prepares an Engine log line and independently queues Console delivery and persistence.
Owns the TCP servers and the threads around them.
Attachment(EngineLogSink &sink, FrayManager &app, ipc::IPCManager &ipc)
Attaches the managers and submits buffered startup lines in capture order.
~Attachment()
Detaches before the managers are destroyed.
Attachment & operator=(const Attachment &)=delete
Attachment(const Attachment &)=delete
F_void stopForwarding()
Stops forwarding to the renderer while continuing to store Engine logs.
~EngineLogSink()
Removes the sink if it has not already been detached.
EngineLogSink & operator=(const EngineLogSink &)=delete
std::shared_ptr< State > _state
EngineLogSink(const EngineLogSink &)=delete
EngineLogSink()
Installs the startup buffer before the managers are created.
void detach()
Stops accepting sink calls and waits for existing calls to release the manager references....
bool & consoleVisible()
Whether the calling thread's log entries may be shown in the Console.
F_void setSink(Sink sink)
Replace the sink; an empty function removes it. Locks sinkMutex; an in-flight logging call may still ...
Releases an in-flight call even if recording throws.
~ActiveCall()
Marks the callback complete and wakes a detaching owner.
An owned log line captured before the managers were attached.
Restores the thread-local recursion guard.
~RecursionGuard()
Clears the guard on every exit path.
Shared callback state, kept alive by in-flight sink snapshots.
std::vector< Captured > pending
int32_t F_long
Signed 32-bit integer (also the base of F_err).
Definition types.hpp:32
void F_void
Alias for void.
Definition types.hpp:20
char F_char
Alias for char.
Definition types.hpp:24