SRS-control 0.1.4
Loading...
Searching...
No Matches
ConfigHandler.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <glaze/glaze.hpp>
4#include <spdlog/spdlog.h>
6
7namespace srs::config
8{
9 inline void output_config_to_json(const Config& app_config, std::string_view json_filename)
10 {
11 if (std::filesystem::exists(json_filename))
12 {
13 spdlog::critical("Cannot dump the default value to the configuration file {:?} as it's already existed!",
14 json_filename);
15 return;
16 }
17 const auto parent_dir = std::filesystem::absolute(std::filesystem::path{ json_filename }).parent_path();
18 std::filesystem::create_directories(parent_dir);
19 auto error_code = glz::write_file_json<glz::opts{ .prettify = true, .new_lines_in_arrays = false }>(
20 app_config, json_filename, std::string{});
21 }
22
23 inline void set_config_from_json(Config& app_config, std::string_view json_filename)
24 {
25 if (json_filename.empty())
26 {
27 return;
28 }
29 if (not std::filesystem::exists(json_filename))
30 {
31 spdlog::warn("configuration file {:?} doesn't exist. Creating a new file with default values",
32 json_filename);
33 output_config_to_json(app_config, json_filename);
34 }
35 else
36 {
37 auto error_code = glz::read_file_json(app_config, json_filename, std::string{});
38 }
39 }
40
41} // namespace srs::config
void output_config_to_json(const Config &app_config, std::string_view json_filename)
void set_config_from_json(Config &app_config, std::string_view json_filename)