Branch data Line data Source code
1 : : // Copyright 2026 HPActor Contributors
2 : : //
3 : : // Licensed under the Apache License, Version 2.0 (the "License");
4 : : // you may not use this file except in compliance with the License.
5 : : // You may obtain a copy of the License at
6 : : //
7 : : // http://www.apache.org/licenses/LICENSE-2.0
8 : : //
9 : : // Unless required by applicable law or agreed to in writing, software
10 : : // distributed under the License is distributed on an "AS IS" BASIS,
11 : : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : : // See the License for the specific language governing permissions and
13 : : // limitations under the License.
14 : :
15 : : #include <hpactor/config/toml_config_parser.hpp>
16 : : #include <hpactor/config/toml_parser_registry.hpp>
17 : : #include <hpactor/log/log_category.hpp>
18 : : #include <hpactor/log/log_level.hpp>
19 : :
20 : : namespace hpactor::config {
21 : : namespace {
22 : :
23 : : class LoggingConfigParser final : public ITomlSystemConfigParser {
24 : : public:
25 : : static constexpr std::string_view kName = "system.logging";
26 : : static constexpr int kOrder = 110;
27 : :
28 : 0 : std::string_view name() const noexcept override {
29 : 0 : return kName;
30 : : }
31 : 0 : int order() const noexcept override {
32 : 0 : return kOrder;
33 : : }
34 : :
35 : 35 : result<void> parse(const TomlTableView& system, SystemDef& out,
36 : : TomlParseContext& /*ctx*/) const override {
37 : 35 : auto lt = system.table("logging");
38 : 35 : if (!lt.valid())
39 : 34 : return result<void>::make();
40 : :
41 : 1 : out.logging.enabled = lt.read_bool("enabled", true);
42 : :
43 : : // default_level (string → LogLevel)
44 : 1 : auto lvl_str = lt.read_string("default_level", "info");
45 : 1 : if (auto parsed = hpactor::log::parse_level(lvl_str); parsed.has_value())
46 : 1 : out.logging.default_level = parsed.value();
47 : :
48 : : // format (string → LogFormat)
49 : 1 : auto fmt_str = lt.read_string("format", "json");
50 : 1 : if (fmt_str == "text")
51 : 1 : out.logging.format = hpactor::log::LogFormat::kText;
52 : : else
53 : 0 : out.logging.format = hpactor::log::LogFormat::kJson;
54 : :
55 : 1 : out.logging.ring_buffer_capacity =
56 : 1 : lt.read_uint32("ring_buffer_capacity", 65536);
57 : :
58 : : // flush_on_level (string → LogLevel)
59 : 1 : auto flush_str = lt.read_string("flush_on_level", "error");
60 : 1 : if (auto parsed = hpactor::log::parse_level(flush_str); parsed.has_value())
61 : 1 : out.logging.flush_on_level = parsed.value();
62 : :
63 : 1 : out.logging.file_path = lt.read_string("file_path", "");
64 : :
65 : : // drop_policy (string → DropPolicy)
66 : 1 : auto drop_str = lt.read_string("drop_policy", "drop_newest");
67 : 1 : if (drop_str == "drop_newest")
68 : 1 : out.logging.drop_policy = hpactor::log::DropPolicy::kDropNewest;
69 : :
70 : : // sinks (array of strings → vector<LogSinkKind>)
71 : 1 : lt.for_each_string_array("sinks", [&](std::string_view s) {
72 : 3 : if (s == "stderr")
73 : 1 : out.logging.sinks.emplace_back(hpactor::log::LogSinkKind::kStderr);
74 : 2 : else if (s == "file")
75 : 1 : out.logging.sinks.emplace_back(hpactor::log::LogSinkKind::kFile);
76 : 1 : else if (s == "rotating_file")
77 : 1 : out.logging.sinks.emplace_back(
78 : 1 : hpactor::log::LogSinkKind::kRotatingFile);
79 : 3 : });
80 : :
81 : : // [system.logging.levels] sub-table
82 : 1 : auto levels_tbl = lt.table("levels");
83 : 1 : if (levels_tbl.valid()) {
84 : 1 : levels_tbl.for_each_entry([&](std::string_view key, TomlValueView val) {
85 : 2 : if (!val.is_string())
86 : 0 : return;
87 : 2 : auto cat = hpactor::log::parse_category(key);
88 : 2 : if (!cat.has_value())
89 : 0 : return;
90 : : auto lvl =
91 : 2 : hpactor::log::parse_level(std::string_view{val.as_string("")});
92 : 2 : if (!lvl.has_value())
93 : 0 : return;
94 : 2 : auto idx = static_cast<size_t>(cat.value());
95 : 2 : out.logging.levels[idx] = lvl.value();
96 : 2 : });
97 : : }
98 : :
99 : : // [system.logging.rotating_file] sub-table
100 : 1 : auto rf = lt.table("rotating_file");
101 : 1 : if (rf.valid()) {
102 : 1 : out.logging.rotating_file.path = rf.read_string("path", "");
103 : 1 : out.logging.rotating_file.max_bytes =
104 : 1 : static_cast<uint64_t>(rf.read_uint32("max_bytes", 104857600));
105 : 1 : out.logging.rotating_file.max_files = rf.read_uint32("max_files", 5);
106 : : }
107 : :
108 : 1 : return result<void>::make();
109 : 1 : }
110 : : };
111 : :
112 : : const TomlSystemParserRegistration<LoggingConfigParser> kRegisterLoggingConfigParser;
113 : :
114 : : } // anonymous namespace
115 : : } // namespace hpactor::config
|