MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Config.cpp
Go to the documentation of this file.
1#include "Config.hpp"
2#include "Core.hpp"
3
5
9
11
13
14namespace MayaFlux {
15
16namespace {
17 struct JournalConfig {
18 std::string severity; // "TRACE" "DEBUG" "INFO" "WARN" "ERROR" "FATAL"
19 bool sink_to_console { false };
20 std::string log_file; // empty = no file sink
21 std::vector<std::string> disable_components; // names from Journal::Component enum
22 std::vector<std::string> disable_contexts; // names from Journal::Context enum
23 std::vector<std::string> include_components;
24 std::vector<std::string> include_contexts;
25
26 static constexpr auto describe()
27 {
28 return std::make_tuple(
29 Reflect::member("severity", &JournalConfig::severity),
30 Reflect::member("sink_to_console", &JournalConfig::sink_to_console),
31 Reflect::member("log_file", &JournalConfig::log_file),
32 Reflect::member("disable_components", &JournalConfig::disable_components),
33 Reflect::member("disable_contexts", &JournalConfig::disable_contexts),
34 Reflect::member("include_components", &JournalConfig::include_components),
35 Reflect::member("include_contexts", &JournalConfig::include_contexts));
36 }
37 };
38
39 struct EngineConfig {
40 Core::GlobalStreamInfo stream;
41 Core::GlobalGraphicsConfig graphics;
42 Core::GlobalInputConfig input;
43 Core::GlobalNetworkConfig network;
44 JournalConfig journal;
45
46 static constexpr auto describe()
47 {
48 return std::make_tuple(
49 Reflect::member("stream", &EngineConfig::stream),
50 Reflect::member("graphics", &EngineConfig::graphics),
51 Reflect::member("input", &EngineConfig::input),
52 Reflect::member("network", &EngineConfig::network),
53 Reflect::member("journal", &EngineConfig::journal));
54 }
55 };
56}
57
59{
60 return is_configured();
61}
62}
63
65
67{
69 MF_WARN(Journal::Component::API, Journal::Context::Configuration, "Accessing stream info while engine is running may lead to inconsistent state.");
70 }
72}
73
75{
77 MF_WARN(Journal::Component::API, Journal::Context::Configuration, "Accessing graphics config while engine is running may lead to inconsistent state.");
78 }
80}
81
83{
85 MF_WARN(Journal::Component::API, Journal::Context::Configuration, "Accessing input config while engine is running may lead to inconsistent state.");
86 }
88}
89
91{
93 MF_WARN(Journal::Component::API, Journal::Context::Configuration, "Accessing network config while engine is running may lead to inconsistent state.");
94 }
96}
97
102
104{
105 if (!is_engine_configured()) {
106 MF_WARN(Journal::Component::API, Journal::Context::Configuration, "Engine is not running. Setting node config on context directly.");
107 }
109}
110
112{
113 if (!is_engine_configured()) {
114 MF_WARN(Journal::Component::API, Journal::Context::Configuration, "Engine is not running. Returning default sample rate");
115 return 48000;
116 }
118}
119
121{
122 if (!is_engine_configured()) {
123 MF_WARN(Journal::Component::API, Journal::Context::Configuration, "Engine is not running. Returning default buffer size");
124 return 512;
125 }
127}
128
130{
131 if (!is_engine_configured()) {
132 MF_WARN(Journal::Component::API, Journal::Context::Configuration, "Engine is not running. Returning default number of output channels");
133 return 2;
134 }
136}
137
142
143void set_journal_component_filter(const std::vector<Journal::Component>& component, bool enabled)
144{
145 for (const auto& comp : component) {
147 }
148}
149
150void set_journal_component_includes(const std::vector<Journal::Component>& components)
151{
153}
154
155void set_journal_context_filter(const std::vector<Journal::Context>& context, bool enabled)
156{
157 for (const auto& ctx : context) {
159 }
160}
161
162void set_journal_context_includes(const std::vector<Journal::Context>& contexts)
163{
165}
166
167namespace {
168 std::string journal_path(const std::string& path)
169 {
170 namespace fs = std::filesystem;
171 if (fs::path(path).is_absolute())
172 return path;
173 const fs::path root(project_root());
174 return (fs::path(path).has_parent_path() ? root / path : root / "data" / "log" / path).string();
175 }
176}
177
178void store_journal_entries(const std::string& file_name)
179{
180 Journal::Archivist::instance().add_sink(std::make_unique<Journal::FileSink>(journal_path(file_name)));
182}
183
185{
186 Journal::Archivist::instance().add_sink(std::make_unique<Journal::ConsoleSink>());
187}
188
189bool load_config_from_file(const std::string& path)
190{
192 auto result = ser.read<EngineConfig>(path);
193 if (!result) {
195 "Failed to load config from {}: {}", path, ser.last_error());
196 return false;
197 }
198 get_global_stream_info() = result->stream;
199 get_global_graphics_config() = result->graphics;
200 get_global_input_config() = result->input;
201 get_global_network_config() = result->network;
202
203 const auto& j = result->journal;
204
205 if (!j.severity.empty()) {
206 auto sev = Reflect::string_to_enum_case_insensitive<Journal::Severity>(j.severity);
207 if (sev) {
209 } else {
211 "Unknown journal severity: {}", j.severity);
212 }
213 }
214 if (j.sink_to_console)
216 if (!j.log_file.empty())
218 for (const auto& name : j.disable_components) {
219 auto comp = Reflect::string_to_enum_case_insensitive<Journal::Component>(name);
220 if (comp) {
221 Config::set_journal_component_filter({ *comp }, false);
222 } else {
224 "Unknown journal component: {}", name);
225 }
226 }
227
228 for (const auto& name : j.disable_contexts) {
229 auto ctx = Reflect::string_to_enum_case_insensitive<Journal::Context>(name);
230 if (ctx) {
231 Config::set_journal_context_filter({ *ctx }, false);
232 } else {
234 "Unknown journal context: {}", name);
235 }
236 }
237
238 std::vector<Journal::Component> included_components;
239 for (const auto& name : j.include_components) {
240 auto comp = Reflect::string_to_enum_case_insensitive<Journal::Component>(name);
241 if (comp) {
242 included_components.push_back(*comp);
243 } else {
245 "Unknown journal component: {}", name);
246 }
247 }
248 Config::set_journal_component_includes(included_components);
249
250 std::vector<Journal::Context> included_contexts;
251 for (const auto& name : j.include_contexts) {
252 auto ctx = Reflect::string_to_enum_case_insensitive<Journal::Context>(name);
253 if (ctx) {
254 included_contexts.push_back(*ctx);
255 } else {
257 "Unknown journal context: {}", name);
258 }
259 }
260 Config::set_journal_context_includes(included_contexts);
261
262 return true;
263}
264
265}
#define MF_ERROR(comp, ctx,...)
#define MF_WARN(comp, ctx,...)
std::string log_file
Definition Config.cpp:20
std::vector< std::string > include_contexts
Definition Config.cpp:24
JournalConfig journal
Definition Config.cpp:44
Core::GlobalNetworkConfig network
Definition Config.cpp:43
bool sink_to_console
Definition Config.cpp:19
Core::GlobalStreamInfo stream
Definition Config.cpp:40
Core::GlobalGraphicsConfig graphics
Definition Config.cpp:41
std::vector< std::string > disable_contexts
Definition Config.cpp:22
std::vector< std::string > disable_components
Definition Config.cpp:21
std::string severity
Definition Config.cpp:18
Core::GlobalInputConfig input
Definition Config.cpp:42
std::vector< std::string > include_components
Definition Config.cpp:23
Core engine lifecycle and configuration API.
std::string name
Definition VKDevice.cpp:123
GlobalStreamInfo & get_stream_info()
Gets the current stream configuration.
Definition Engine.hpp:224
void set_node_config(const Nodes::NodeConfig &config)
Sets the node processing configuration.
Definition Engine.cpp:409
GlobalNetworkConfig & get_network_config()
Gets the current network configuration.
Definition Engine.hpp:242
Nodes::NodeConfig & get_node_config()
Gets the current node processing configuration.
Definition Engine.cpp:400
GlobalInputConfig & get_input_config()
Gets the current input configuration.
Definition Engine.hpp:236
GlobalGraphicsConfig & get_graphics_config()
Gets the current graphics configuration.
Definition Engine.hpp:230
std::optional< T > read(const std::string &path)
Read path and deserialize into T.
const std::string & last_error() const
Last error message, empty if no error.
Converts arbitrary C++ types to/from JSON strings and disk files.
void add_sink(std::unique_ptr< Sink > sink)
Add a log sink for output.
void set_component_includes(const std::vector< Component > &components)
Restrict logging to these components; an empty list includes all.
void set_component_filter(Component comp, bool enabled)
Enable or disable logging for a specific component.
void set_context_includes(const std::vector< Context > &contexts)
Restrict logging to these contexts; an empty list includes all.
static Archivist & instance()
Get the singleton instance of the Archivist.
void set_min_severity(Severity min_sev)
Set the minimum severity level for logging.
void set_context_filter(Context ctx, bool enabled)
Enable or disable logging for a specific context.
std::string project_root()
Core::GlobalGraphicsConfig & get_global_graphics_config()
Gets the graphics configuration from the default engine.
Definition Config.cpp:74
Nodes::NodeConfig & get_node_config()
Definition Config.cpp:98
void set_journal_component_filter(const std::vector< Journal::Component > &component, bool enabled)
Enables or disables logging for specific journal components.
Definition Config.cpp:143
Core::GlobalInputConfig & get_global_input_config()
Gets the input configuration from the default engine.
Definition Config.cpp:82
void set_journal_component_includes(const std::vector< Journal::Component > &components)
Restrict journal output to these components; an empty list includes all.
Definition Config.cpp:150
uint32_t get_buffer_size()
Gets the buffer size from the default engine.
Definition Config.cpp:120
void set_node_config(const Nodes::NodeConfig &config)
Sets the node configuration for the default engine.
Definition Config.cpp:103
bool load_config_from_file(const std::string &path)
Load engine configuration from a JSON file, applying any present fields to the pre-Init config struct...
Definition Config.cpp:189
void store_journal_entries(const std::string &file_name)
Stores journal entries to a file by adding a FileSink to the Archivist.
Definition Config.cpp:178
void set_journal_severity(Journal::Severity severity)
Sets the minimum severity level for journal entries to be logged.
Definition Config.cpp:138
void sink_journal_to_console()
Outputs journal entries to the console by adding a ConsoleSink to the Archivist NOTE: This records th...
Definition Config.cpp:184
void set_journal_context_filter(const std::vector< Journal::Context > &context, bool enabled)
Enables or disables logging for specific journal contexts.
Definition Config.cpp:155
uint32_t get_sample_rate()
Gets the sample rate from the default engine.
Definition Config.cpp:111
Core::GlobalStreamInfo & get_global_stream_info()
Gets the stream configuration from the default engine.
Definition Config.cpp:66
uint32_t get_num_out_channels()
Gets the number of output channels from the default engine.
Definition Config.cpp:129
Core::GlobalNetworkConfig & get_global_network_config()
Gets the network configuration from the default engine.
Definition Config.cpp:90
void set_journal_context_includes(const std::vector< Journal::Context > &contexts)
Restrict journal output to these contexts; an empty list includes all.
Definition Config.cpp:162
Globlal configuration for MayaFlux.
Definition Config.cpp:64
@ Configuration
Configuration and parameter updates.
@ API
MayaFlux/API Wrapper and convenience functions.
constexpr auto member(std::string_view key, T Class::*ptr)
Definition Mirror.hpp:37
bool is_engine_configured()
Checks if the default engine has currently accepted all configurations and initialized all managers.
Definition Config.cpp:58
bool is_configured()
Checks if the default engine has currently accepted all configurations and initialized all managers.
Definition Core.cpp:93
Core::Engine & get_context()
Gets the default engine instance.
Definition Core.cpp:101
Main namespace for the Maya Flux audio engine.
Definition Runtime.cpp:12
Configuration for the InputSubsystem.
Configuration for the NetworkSubsystem.
uint32_t channels
Number of discrete channels in this set.
uint32_t buffer_size
Number of samples per processing block.
uint32_t sample_rate
Number of samples processed per second (Hz)
ChannelConfig output
Configuration for output signal channels.
Comprehensive configuration for digital audio stream processing.
Configuration settings for individual audio nodes.
Definition NodeSpec.hpp:29