Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
log_sink.hpp
Go to the documentation of this file.
1#pragma once
2
3// Log sinks for <pulp/runtime/log.hpp>.
4//
5// FileLogSink - size-rotated log file in a private directory.
6// CaptureLogSink - bounded in-memory record list for tests and tooling.
7// ScopedLogSink - installs a sink for a scope and restores the previous one.
8//
9// Logging is not realtime-safe, and neither are these sinks: they allocate,
10// take a mutex and (for files) perform I/O. Never log from the audio thread.
11//
12// Every sink here is a cheap shared handle. Copies refer to the same file or
13// record list, so a copy can be installed with `set_log_sink` while the original
14// stays available for inspection.
15
16#include <pulp/runtime/log.hpp>
17
18#include <cstddef>
19#include <cstdint>
20#include <filesystem>
21#include <memory>
22#include <optional>
23#include <string>
24#include <string_view>
25#include <vector>
26
27namespace pulp::runtime {
28
33 std::filesystem::path directory;
34
37 std::string app_name = "pulp";
38
40 std::string file_name = "pulp.log";
41
43 std::uint64_t max_file_bytes = 1024 * 1024;
44
48 int max_files = 5;
49
52};
53
76class FileLogSink {
77 public:
80 static std::optional<FileLogSink> open(FileLogSinkOptions options,
81 std::string* error = nullptr);
82
83 void write(LogLevel level, std::string_view category, std::string_view message) const;
84
86 LogSink sink() const;
87 operator LogSink() const {
88 return sink();
89 } // NOLINT: intentional conversion
90
92 const std::filesystem::path& directory() const;
93 std::filesystem::path path() const;
94
96 std::filesystem::path rotated_path(int index) const;
97
99 std::uint64_t dropped_count() const;
100
101 class Impl;
102
103 private:
104 explicit FileLogSink(std::shared_ptr<Impl> impl) : impl_(std::move(impl)) {}
105 std::shared_ptr<Impl> impl_;
106};
107
108struct LogRecord {
110 std::string category;
111 std::string message;
112};
113
118 public:
119 explicit CaptureLogSink(std::size_t max_records = 4096);
120
121 void write(LogLevel level, std::string_view category, std::string_view message) const;
122
123 LogSink sink() const;
124 operator LogSink() const {
125 return sink();
126 } // NOLINT: intentional conversion
127
129 std::vector<LogRecord> records() const;
131 std::vector<LogRecord> records_at_least(LogLevel min_level) const;
133 std::vector<LogRecord> take();
134 void clear();
135
136 std::size_t size() const;
138 std::uint64_t dropped_count() const;
140 bool contains(std::string_view needle) const;
141
142 class Impl;
143
144 private:
145 std::shared_ptr<Impl> impl_;
146};
147
152 public:
153 explicit ScopedLogSink(LogSink sink) : previous_(set_log_sink(std::move(sink))) {}
155 set_log_sink(std::move(previous_));
156 }
157
158 ScopedLogSink(const ScopedLogSink&) = delete;
160
161 private:
162 LogSink previous_;
163};
164
165} // namespace pulp::runtime
std::vector< LogRecord > records_at_least(LogLevel min_level) const
Snapshot of records at or above min_level.
std::uint64_t dropped_count() const
Records discarded because the list was full.
bool contains(std::string_view needle) const
True if any retained message contains needle.
std::size_t size() const
std::vector< LogRecord > take()
Return the records and empty the list.
std::vector< LogRecord > records() const
Snapshot, oldest first.
CaptureLogSink(std::size_t max_records=4096)
void write(LogLevel level, std::string_view category, std::string_view message) const
LogSink sink() const
The sink function for set_log_sink.
std::uint64_t dropped_count() const
Records that could not be written.
void write(LogLevel level, std::string_view category, std::string_view message) const
static std::optional< FileLogSink > open(FileLogSinkOptions options, std::string *error=nullptr)
Open (creating if needed) the log.
std::filesystem::path rotated_path(int index) const
Path of rotated copy index (1 is the newest).
const std::filesystem::path & directory() const
Resolved directory and live file path.
std::filesystem::path path() const
ScopedLogSink & operator=(const ScopedLogSink &)=delete
ScopedLogSink(const ScopedLogSink &)=delete
~ScopedLogSink()
Definition log_sink.hpp:154
ScopedLogSink(LogSink sink)
Definition log_sink.hpp:153
Definition generated_event_source.hpp:33
LogLevel
Definition log.hpp:42
@ Info
Definition log.hpp:42
std::function< void(LogLevel level, std::string_view category, std::string_view message)> LogSink
Receives every record after the platform writers.
Definition log.hpp:49
LogSink set_log_sink(LogSink sink)
Install sink as the process-wide log sink and return the previous one (empty if none).
Definition log.hpp:147
STL namespace.
Definition log_sink.hpp:29
std::uint64_t max_file_bytes
A record that would grow the file past this rotates it first.
Definition log_sink.hpp:43
int max_files
Live file plus rotated copies kept (pulp.log, pulp.log.1, ..., newest rotation first).
Definition log_sink.hpp:48
LogLevel min_level
Records below this level are not written.
Definition log_sink.hpp:51
std::filesystem::path directory
Directory that holds the log.
Definition log_sink.hpp:33
std::string app_name
Names the subdirectory under the platform log directory when directory is empty.
Definition log_sink.hpp:37
std::string file_name
Log file name inside the directory. A single path component.
Definition log_sink.hpp:40
Definition log_sink.hpp:108
std::string message
Definition log_sink.hpp:111
LogLevel level
Definition log_sink.hpp:109
std::string category
Definition log_sink.hpp:110