cpp-msgpack-rpc 0.2.0
An RPC library implementing MessagePack RPC.
Loading...
Searching...
No Matches
parse_toml_logging.h
Go to the documentation of this file.
1/*
2 * Copyright 2023 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
20#pragma once
21
22#include <cstddef>
23#include <string>
24#include <string_view>
25#include <unordered_map>
26#include <utility>
27
28#include <toml++/toml.h>
29
33
34namespace msgpack_rpc::config::toml::impl {
35
44[[nodiscard]] inline logging::LogLevel parse_log_level(std::string_view str,
45 const ::toml::source_region& source, std::string_view config_key) {
46 if (str == "trace") {
48 }
49 if (str == "debug") {
51 }
52 if (str == "info") {
54 }
55 if (str == "warn") {
57 }
58 if (str == "error") {
60 }
61 if (str == "critical") {
63 }
64 throw_error(source, config_key);
65}
66
73inline void parse_toml(const ::toml::table& table, LoggingConfig& config) {
74 for (const auto& [key, value] : table) {
75 const auto key_str = key.str();
76 if (key_str == "file_path") {
77 MSGPACK_RPC_PARSE_TOML_VALUE("file_path", file_path, std::string);
78 } else if (key_str == "max_file_size") {
80 "max_file_size", max_file_size, std::size_t);
81 } else if (key_str == "max_files") {
82 MSGPACK_RPC_PARSE_TOML_VALUE("max_files", max_files, std::size_t);
83 } else if (key_str == "output_log_level") {
84 const auto config_value = value.value<std::string>();
85 if (!config_value) {
86 throw_error(value.source(), "output_log_level");
87 }
88 config.output_log_level(parse_log_level(
89 *config_value, value.source(), "output_log_level"));
90 }
91 }
92}
93
100inline void parse_toml(const ::toml::table& table,
101 std::unordered_map<std::string, LoggingConfig>& configs) {
102 for (const auto& [key, value] : table) {
103 const auto* table_ptr = value.as_table();
104 if (table_ptr == nullptr) {
105 throw_error(value.source(), "logging",
106 "\"logging\" must be a table of tables.");
107 }
109 parse_toml(*table_ptr, config);
110 configs.try_emplace(std::string(key.str()), std::move(config));
111 }
112}
113
114} // namespace msgpack_rpc::config::toml::impl
Class of Logging configuration.
Definition of LogLevel enumeration.
Definition of LoggingConfig class.
Namespace of configurations.
LogLevel
Enumeration of log levels.
Definition log_level.h:29
@ TRACE
Trace. (Internal operations to send and receive messages.)
Definition log_level.h:31
@ WARN
Warnings. (Unexpected conditions which don't stop operations.)
Definition log_level.h:40
@ INFO
Information. (Not used in this library.)
Definition log_level.h:37
@ CRITICAL
Critical. (Unexpected conditions which can stop all operations in communication.)
Definition log_level.h:46
@ ERROR
Error. (Unexpected conditions which stop some operations in communication.)
Definition log_level.h:43
@ DEBUG
Debug. (Log of messages, initialization and finalization of clients and servers.)
Definition log_level.h:34
void parse_toml(const ::toml::table &table, MessageParserConfig &config)
Parse a configuration of parsers of messages from TOML.
Definition of common functions and macros to parse TOML files.
#define MSGPACK_RPC_PARSE_TOML_VALUE(KEY_STR, CONFIG_FUNCTION, TYPE)
Internal macro to parse a value from TOML.
void throw_error(const ::toml::source_region &source, std::string_view config_key)
Throw an exception for an error of TOML.
logging::LogLevel parse_log_level(std::string_view str, const ::toml::source_region &source, std::string_view config_key)
Parse a log level from a string.