cpp-msgpack-rpc
0.2.0
An RPC library implementing MessagePack RPC.
Toggle main menu visibility
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
30
#include "
msgpack_rpc/config/logging_config.h
"
31
#include "
msgpack_rpc/config/toml/parse_toml_common.h
"
32
#include "
msgpack_rpc/logging/log_level.h
"
33
34
namespace
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"
) {
47
return
logging::LogLevel::TRACE
;
48
}
49
if
(str ==
"debug"
) {
50
return
logging::LogLevel::DEBUG
;
51
}
52
if
(str ==
"info"
) {
53
return
logging::LogLevel::INFO
;
54
}
55
if
(str ==
"warn"
) {
56
return
logging::LogLevel::WARN
;
57
}
58
if
(str ==
"error"
) {
59
return
logging::LogLevel::ERROR
;
60
}
61
if
(str ==
"critical"
) {
62
return
logging::LogLevel::CRITICAL
;
63
}
64
throw_error
(source, config_key);
65
}
66
73
inline
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"
) {
79
MSGPACK_RPC_PARSE_TOML_VALUE
(
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
100
inline
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
}
108
LoggingConfig
config
;
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
msgpack_rpc::config::LoggingConfig
Class of Logging configuration.
Definition
logging_config.h:34
log_level.h
Definition of LogLevel enumeration.
logging_config.h
Definition of LoggingConfig class.
msgpack_rpc::config
Namespace of configurations.
Definition
client_config.h:32
msgpack_rpc::logging::LogLevel
LogLevel
Enumeration of log levels.
Definition
log_level.h:29
msgpack_rpc::logging::LogLevel::TRACE
@ TRACE
Trace. (Internal operations to send and receive messages.).
Definition
log_level.h:31
msgpack_rpc::logging::LogLevel::WARN
@ WARN
Warnings. (Unexpected conditions which don't stop operations.).
Definition
log_level.h:40
msgpack_rpc::logging::LogLevel::INFO
@ INFO
Information. (Not used in this library.).
Definition
log_level.h:37
msgpack_rpc::logging::LogLevel::CRITICAL
@ CRITICAL
Critical. (Unexpected conditions which can stop all operations in communication.).
Definition
log_level.h:46
msgpack_rpc::logging::LogLevel::ERROR
@ ERROR
Error. (Unexpected conditions which stop some operations in communication.).
Definition
log_level.h:43
msgpack_rpc::logging::LogLevel::DEBUG
@ DEBUG
Debug. (Log of messages, initialization and finalization of clients and servers.).
Definition
log_level.h:34
msgpack_rpc::config::toml::impl::parse_toml
void parse_toml(const ::toml::table &table, MessageParserConfig &config)
Parse a configuration of parsers of messages from TOML.
Definition
parse_toml_client_server.h:46
parse_toml_common.h
Definition of common functions and macros to parse TOML files.
MSGPACK_RPC_PARSE_TOML_VALUE
#define MSGPACK_RPC_PARSE_TOML_VALUE(KEY_STR, CONFIG_FUNCTION, TYPE)
Internal macro to parse a value from TOML.
Definition
parse_toml_common.h:69
msgpack_rpc::config::toml::impl::throw_error
void throw_error(const ::toml::source_region &source, std::string_view config_key)
Throw an exception for an error of TOML.
Definition
parse_toml_common.h:43
msgpack_rpc::config::toml::impl::parse_log_level
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.
Definition
parse_toml_logging.h:44
src
msgpack_rpc
config
toml
parse_toml_logging.h
Generated on
for cpp-msgpack-rpc by
1.18.0