#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace Void::Logging { namespace { std::vector split_list(std::string_view csv) { std::vector out; std::size_t start = 0; while (start <= csv.size()) { auto comma = csv.find(',', start); if (comma == std::string_view::npos) { comma = csv.size(); } auto item = csv.substr(start, comma - start); while (!item.empty() && item.front() == ' ') { item.remove_prefix(1); } while (!item.empty() && item.back() == ' ') { item.remove_suffix(1); } if (!item.empty()) { out.emplace_back(item); } start = comma + 1; } return out; } std::optional parse_level(std::string_view name) { std::string lower; for (const char c : name) { lower += static_cast( std::tolower(static_cast(c))); } if (lower == "trace") { return Level::Trace; } if (lower == "debug") { return Level::Debug; } if (lower == "info") { return Level::Info; } if (lower == "warning" || lower == "warn") { return Level::Warning; } if (lower == "error") { return Level::Error; } if (lower == "fatal") { return Level::Fatal; } if (lower == "disabled" || lower == "off") { return Level::Disabled; } return std::nullopt; } std::optional parse_facade_level(std::string_view name) { const auto l = parse_level(name); if (!l.has_value()) { return std::nullopt; } switch (*l) { case Level::Trace: return Void::Log::Level::Trace; case Level::Debug: return Void::Log::Level::Debug; case Level::Info: return Void::Log::Level::Info; case Level::Warning: return Void::Log::Level::Warning; case Level::Error: return Void::Log::Level::Error; case Level::Fatal: return Void::Log::Level::Fatal; case Level::Disabled: return Void::Log::Level::Disabled; } return std::nullopt; } FieldFilter make_field_filter(const Configuration::Config& cfg) { FieldFilter f; f.include = split_list(cfg.get_or("fields.include", "")); f.exclude = split_list(cfg.get_or("fields.exclude", "")); f.redact = split_list(cfg.get_or("fields.redact", "")); return f; } Void::Result::Result, Void::Result::Error> make_formatter(const Configuration::Config& cfg) { const auto format = cfg.get_or("format", "default"); if (format == "default") { return Void::Result::ok>(nullptr); } if (format == "pattern") { auto fmt = std::make_shared(std::string{ cfg.get_or("pattern", "{time} [{level}] {scopes}{message}")}); if (const auto col = cfg.get_as("color"); col.has_value()) { const auto parsed = cfg.get_as("color"); if (!parsed.has_value()) { return Void::Result::fail(make_log_error( LogErrorCode::invalid_argument, std::format("Logging::from_config: invalid bool " "'{}' for color", *col))); } if (*parsed) { fmt->set_color_policy(ColorPolicy::defaults()); } } fmt->set_field_filter(make_field_filter(cfg)); return Void::Result::ok>( std::move(fmt)); } if (format == "json") { auto fmt = std::make_shared(); fmt->set_field_filter(make_field_filter(cfg)); return Void::Result::ok>( std::move(fmt)); } if (format == "logfmt") { auto fmt = std::make_shared(); fmt->set_field_filter(make_field_filter(cfg)); return Void::Result::ok>( std::move(fmt)); } return Void::Result::fail(make_log_error( LogErrorCode::invalid_argument, std::format("Logging::from_config: unknown format '{}'", format))); } struct Dispatch { Mode mode = Mode::Async; std::size_t capacity = 512; OverflowPolicy policy = OverflowPolicy::DropNewest; }; Void::Result::Result make_dispatch(const Configuration::Config& cfg) { Dispatch d; if (cfg.get_as("async").has_value()) { const auto async_val = cfg.get_as("async"); if (!async_val.has_value()) { return Void::Result::fail(make_log_error( LogErrorCode::invalid_argument, "Logging::from_config: invalid async bool")); } d.mode = *async_val ? Mode::Async : Mode::Immediate; } if (cfg.get_as("capacity").has_value()) { const auto cap_val = cfg.get_as("capacity"); if (!cap_val.has_value() || *cap_val == 0) { return Void::Result::fail(make_log_error( LogErrorCode::invalid_argument, "Logging::from_config: capacity must be positive")); } d.capacity = *cap_val; } if (const auto overflow = cfg.get_as("overflow"); overflow.has_value()) { if (*overflow == "block") { d.policy = OverflowPolicy::Block; } else if (*overflow == "drop_newest") { d.policy = OverflowPolicy::DropNewest; } else if (*overflow == "drop_oldest") { d.policy = OverflowPolicy::DropOldest; } else { return Void::Result::fail(make_log_error( LogErrorCode::invalid_argument, std::format("Logging::from_config: unknown overflow " "policy '{}'", *overflow))); } } return d; } Void::Result::Result positive_size_or(const Configuration::Config& cfg, std::string_view key, std::size_t fallback) { if (!cfg.get_as(key).has_value()) { return fallback; } const auto parsed = cfg.get_as(key); if (!parsed.has_value() || *parsed == 0) { return Void::Result::fail(make_log_error( LogErrorCode::invalid_argument, std::format("Logging::from_config: '{}' must be > 0", key))); } return *parsed; } } // namespace Void::Result::Result, Void::Result::Error> from_config(const Configuration::Config& cfg, std::string_view prefix) { const auto sub = cfg.subtree(prefix); Level parsed_level = Level::Trace; if (const auto raw_lvl = sub.get_as("level"); raw_lvl.has_value()) { const auto lvl = parse_level(*raw_lvl); if (!lvl.has_value()) { return Void::Result::fail(make_log_error( LogErrorCode::invalid_argument, std::format("Logging::from_config: unknown level '{}'", *raw_lvl))); } parsed_level = *lvl; } auto dispatch_res = make_dispatch(sub); if (!dispatch_res.has_value()) { return Void::Result::fail(std::move(dispatch_res).error()); } const auto dispatch = *dispatch_res; auto formatter_res = make_formatter(sub); if (!formatter_res.has_value()) { return Void::Result::fail(std::move(formatter_res).error()); } const auto formatter = std::move(*formatter_res); const LoggerOptions base_opts{ .mode = dispatch.mode, .policy = dispatch.policy, .limits = DeliveryLimits{ .entries = dispatch.capacity, .owned_bytes = std::max( 8ULL * 1024ULL * 1024ULL, dispatch.capacity * 1024ULL), .max_fields = 64, .max_scopes = 64}, .failure_policy = FailurePolicy::Quarantine, .suppress_reentrancy = true}; std::vector> sinks; for (const auto& name : split_list(sub.get_or("sinks", "console"))) { if (name == "console") { const auto stream = sub.get_or("console.stream", "stderr"); if (stream != "stderr" && stream != "stdout") { return Void::Result::fail(make_log_error( LogErrorCode::invalid_argument, std::format("Logging::from_config: unknown " "console.stream '{}'", stream))); } auto sink = std::make_shared( stream == "stdout" ? std::cout : std::cerr, dispatch.mode, dispatch.capacity); if (formatter) { sink->set_formatter(formatter); } sinks.push_back(std::move(sink)); } else if (name == "file") { const auto path = sub.get_or("file.path"); if (path.empty()) { return Void::Result::fail(make_log_error( LogErrorCode::invalid_argument, "Logging::from_config: sink 'file' needs file.path")); } auto sink = FileLogger::open(std::filesystem::path{path}, base_opts); if (!sink.has_value()) { return Void::Result::fail(std::move(sink).error()); } if (formatter) { (*sink)->set_formatter(formatter); } sinks.push_back(std::shared_ptr{std::move(*sink)}); } else if (name == "rotating") { const auto path = sub.get_or("rotating.path"); if (path.empty()) { return Void::Result::fail(make_log_error( LogErrorCode::invalid_argument, "Logging::from_config: sink 'rotating' needs " "rotating.path")); } auto max_kb = positive_size_or(sub, "rotating.max_kb", 1024); if (!max_kb) { return Void::Result::fail(std::move(max_kb).error()); } auto max_files = positive_size_or(sub, "rotating.max_files", 5); if (!max_files) { return Void::Result::fail(std::move(max_files).error()); } RotatingFileLogger::Options opt; opt.max_bytes = *max_kb * 1024; opt.max_files = *max_files; opt.mode = dispatch.mode; opt.capacity = dispatch.capacity; opt.policy = dispatch.policy; auto sink = RotatingFileLogger::open(std::filesystem::path{path}, opt); if (!sink.has_value()) { return Void::Result::fail(std::move(sink).error()); } if (formatter) { (*sink)->set_formatter(formatter); } sinks.push_back(std::shared_ptr{std::move(*sink)}); } else if (name == "split") { const auto dir = sub.get_or("split.dir"); if (dir.empty()) { return Void::Result::fail(make_log_error( LogErrorCode::invalid_argument, "Logging::from_config: sink 'split' needs split.dir")); } auto max_kb = positive_size_or(sub, "split.max_kb", 1024); if (!max_kb) { return Void::Result::fail(std::move(max_kb).error()); } auto max_files = positive_size_or(sub, "split.max_files", 5); if (!max_files) { return Void::Result::fail(std::move(max_files).error()); } SplitFileLogger::Options opt; opt.combined = sub.get_or_as("split.combined", true); opt.max_bytes = *max_kb * 1024; opt.max_files = *max_files; opt.mode = dispatch.mode; opt.capacity = dispatch.capacity; opt.policy = dispatch.policy; auto sink = SplitFileLogger::open(std::filesystem::path{dir}, opt); if (!sink.has_value()) { return Void::Result::fail(std::move(sink).error()); } if (formatter) { (*sink)->set_formatter(formatter); } sinks.push_back(std::shared_ptr{std::move(*sink)}); } else if (name == "history") { const auto path = sub.get_or("history.path"); if (path.empty()) { return Void::Result::fail(make_log_error( LogErrorCode::invalid_argument, "Logging::from_config: sink 'history' needs " "history.path")); } auto max_kb = positive_size_or(sub, "history.max_kb", 8192); if (!max_kb) { return Void::Result::fail(std::move(max_kb).error()); } auto max_files = positive_size_or(sub, "history.max_files", 10); if (!max_files) { return Void::Result::fail(std::move(max_files).error()); } HistoryLogger::Options opt; opt.max_bytes = *max_kb * 1024; opt.max_files = *max_files; opt.mode = dispatch.mode; opt.capacity = dispatch.capacity; opt.policy = dispatch.policy; auto sink = HistoryLogger::open(std::filesystem::path{path}, opt); if (!sink.has_value()) { return Void::Result::fail(std::move(sink).error()); } sinks.push_back(std::shared_ptr{std::move(*sink)}); } else if (name == "syslog") { #ifdef VOID_HAS_SYSLOG const auto identity = sub.get_or("syslog.identity"); if (identity.empty()) { return Void::Result::fail(make_log_error( LogErrorCode::invalid_argument, "Logging::from_config: sink 'syslog' needs " "syslog.identity")); } sinks.push_back(std::make_shared( std::string{identity}, dispatch.mode, dispatch.capacity)); #else return Void::Result::fail(make_log_error( LogErrorCode::unsupported_capability, "Logging::from_config: syslog is not available on " "this platform")); #endif } else { return Void::Result::fail(make_log_error( LogErrorCode::invalid_argument, std::format("Logging::from_config: unknown sink '{}'", name))); } } std::shared_ptr root; if (sinks.empty()) { root = std::make_shared(base_opts); } else if (sinks.size() == 1) { root = std::move(sinks.front()); } else { if (sinks.size() > kDefaultMaxFanoutChildren) { return Void::Result::fail(make_log_error( LogErrorCode::invalid_argument, "Logging::from_config: too many sinks")); } root = std::make_shared(std::move(sinks)); } root->set_level(parsed_level); Void::Log::debug("Logging", "from_config: built logger tree"); return Void::Result::ok(std::move(root)); } void apply_facade_config(const Configuration::Config& cfg, std::string_view prefix) { const auto sub = cfg.subtree(prefix); if (const auto level = parse_facade_level(sub.get_or("level", "")); level.has_value()) { Void::Log::set_min_level(*level); } for (const auto& key : sub.subtree("module_levels").keys()) { const auto value = sub.get_or(std::string{"module_levels."} + key); if (const auto level = parse_facade_level(value); level.has_value()) { Void::Log::set_module_level(key, *level); } } } } // namespace Void::Logging