NeuralPlus 0.2.0
A small C++17 SDK for AI clients, sessions, tools, and tracing
Loading...
Searching...
No Matches
tracing.hpp
Go to the documentation of this file.
1// Copyright 2026 Aniket Kulkarni
2// SPDX-License-Identifier: Apache-2.0
3
6
7#pragma once
8
10#include "neuralplus/types.hpp"
11
12#include <chrono>
13#include <cstddef>
14#include <functional>
15#include <memory>
16#include <mutex>
17#include <string>
18#include <vector>
19
20namespace neuralplus {
21
23enum class TraceLevel {
24 debug,
25 info,
26 warning,
27 error,
28};
29
31enum class TraceEventType {
32 generation_start,
33 generation_end,
34 provider_start,
35 provider_end,
36 tool_start,
37 tool_end,
38 error,
39};
40
42struct TraceEvent {
44 TraceLevel level{TraceLevel::info};
45
47 TraceEventType type{TraceEventType::generation_start};
48
50 std::chrono::system_clock::time_point timestamp{
51 std::chrono::system_clock::now()};
52
54 std::string session_id;
55
57 std::string run_id;
58
60 std::string operation_id;
61
63 std::string name;
64
66 JsonValue attributes = JsonValue::object();
67
69 std::string payload;
70};
71
73class NEURALPLUS_API Tracer {
74public:
75 virtual ~Tracer();
76
78 virtual void record(const TraceEvent& event) = 0;
79
80protected:
81 Tracer() = default;
82
83private:
84 Tracer(const Tracer&) = delete;
85 Tracer& operator=(const Tracer&) = delete;
86};
87
89class NEURALPLUS_API ConsoleTracer final : public Tracer {
90public:
92 ~ConsoleTracer() override;
93
95 void record(const TraceEvent& event) override;
96
97private:
98 ConsoleTracer(const ConsoleTracer&) = delete;
99 ConsoleTracer& operator=(const ConsoleTracer&) = delete;
100
101 std::mutex mutex_;
102};
103
107 bool append{true};
108
110 bool flush_each_event{false};
111
113 bool include_payloads{false};
114
116 std::size_t max_bytes{0};
117};
118
124class NEURALPLUS_API FileTracer final : public Tracer {
125public:
127 explicit FileTracer(std::string path,
128 FileTracerOptions options = {});
129 ~FileTracer() override;
130
132 void record(const TraceEvent& event) override;
133
134private:
135 FileTracer(const FileTracer&) = delete;
136 FileTracer& operator=(const FileTracer&) = delete;
137
138 class Impl;
139 std::unique_ptr<Impl> impl_;
140};
141
143class NEURALPLUS_API InMemoryTracer final : public Tracer {
144public:
146 ~InMemoryTracer() override;
147
149 void record(const TraceEvent& event) override;
150
152 [[nodiscard]] std::vector<TraceEvent> events() const;
153
155 void clear();
156
157private:
158 InMemoryTracer(const InMemoryTracer&) = delete;
159 InMemoryTracer& operator=(const InMemoryTracer&) = delete;
160
161 mutable std::mutex mutex_;
162 std::vector<TraceEvent> events_;
163};
164
170class NEURALPLUS_API FunctionTracer final : public Tracer {
171public:
173 using Function = std::function<void(const TraceEvent&)>;
174
176 explicit FunctionTracer(Function function);
177 ~FunctionTracer() override;
178
180 void record(const TraceEvent& event) override;
181
182private:
183 FunctionTracer(const FunctionTracer&) = delete;
184 FunctionTracer& operator=(const FunctionTracer&) = delete;
185
186 std::mutex mutex_;
187 Function function_;
188};
189
196class NEURALPLUS_API SyslogTracer final : public Tracer {
197public:
199 explicit SyslogTracer(std::string identity = "neuralplus",
200 int facility = 0);
201 ~SyslogTracer() override;
202
204 void record(const TraceEvent& event) override;
205
206private:
207 SyslogTracer(const SyslogTracer&) = delete;
208 SyslogTracer& operator=(const SyslogTracer&) = delete;
209
210 class Impl;
211 std::unique_ptr<Impl> impl_;
212};
213
215using Tracers = std::vector<std::shared_ptr<Tracer>>;
216
218[[nodiscard]] NEURALPLUS_API const char* to_string(TraceLevel level) noexcept;
219
221[[nodiscard]] NEURALPLUS_API const char* to_string(TraceEventType type) noexcept;
222
223} // namespace neuralplus
Human-readable stderr tracer.
Definition tracing.hpp:89
void record(const TraceEvent &event) override
Writes one human-readable event to standard error.
Definition tracing.hpp:124
FileTracer(std::string path, FileTracerOptions options={})
Opens a JSON Lines trace file at path.
void record(const TraceEvent &event) override
Appends one event subject to the configured payload and size policy.
Definition tracing.hpp:170
std::function< void(const TraceEvent &)> Function
Callback signature for one structured event.
Definition tracing.hpp:173
void record(const TraceEvent &event) override
Delegates one event to the configured callback.
FunctionTracer(Function function)
Creates a tracer that forwards events to function.
Thread-safe tracer that retains events for tests and embedding applications.
Definition tracing.hpp:143
std::vector< TraceEvent > events() const
Returns a thread-safe snapshot of retained events.
void record(const TraceEvent &event) override
Retains a copy of one event.
void clear()
Removes all retained events.
Definition tracing.hpp:196
void record(const TraceEvent &event) override
Sends one metadata-only event to POSIX syslog.
SyslogTracer(std::string identity="neuralplus", int facility=0)
Creates a POSIX syslog destination with an identity prefix and facility.
Thread-safe destination for tracing and optional logging.
Definition tracing.hpp:73
virtual void record(const TraceEvent &event)=0
Records one structured lifecycle event.
JSON Lines file tracer settings.
Definition tracing.hpp:105
bool include_payloads
Persist TraceEvent::payload in addition to metadata.
Definition tracing.hpp:113
bool flush_each_event
Flush the stream after every event.
Definition tracing.hpp:110
bool append
Append to an existing file instead of truncating it.
Definition tracing.hpp:107
std::size_t max_bytes
Stop accepting events at this many bytes; zero means unlimited.
Definition tracing.hpp:116
One metadata-first trace record.
Definition tracing.hpp:42
std::string name
Model or tool name.
Definition tracing.hpp:63
std::string operation_id
Provider round or tool-call identifier.
Definition tracing.hpp:60
TraceEventType type
Lifecycle category.
Definition tracing.hpp:47
JsonValue attributes
Metadata safe for the configured tracing policy.
Definition tracing.hpp:66
std::string payload
Optional sensitive payload, empty unless explicitly enabled.
Definition tracing.hpp:69
std::string session_id
Session correlation identifier.
Definition tracing.hpp:54
std::chrono::system_clock::time_point timestamp
Wall-clock timestamp captured when the event is created.
Definition tracing.hpp:50
TraceLevel level
Event severity.
Definition tracing.hpp:44
std::string run_id
Generation correlation identifier.
Definition tracing.hpp:57
NEURALPLUS_API const char * to_string(TraceLevel level) noexcept
Returns the stable SDK spelling of a trace level.
TraceEventType
Stable lifecycle event categories emitted by AIClient.
Definition tracing.hpp:31
TraceLevel
Severity used by structured tracing and optional logging.
Definition tracing.hpp:23
std::vector< std::shared_ptr< Tracer > > Tracers
Shared tracer collection accepted by ClientOptions.
Definition tracing.hpp:215
nlohmann::json JsonValue
Definition types.hpp:26