Coverage Report

Created: 2025-08-01 03:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/builds/MusicScience37Projects/utility-libraries/cpp-msgpack-rpc/src/msgpack_rpc/config/toml/parse_toml_logging.h
Line
Count
Source
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
 */
16
/*!
17
 * \file
18
 * \brief Definition of parse_toml functions for logging.
19
 */
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
36
/*!
37
 * \brief Parse a log level from a string.
38
 *
39
 * \param[in] str String.
40
 * \param[in] source Location in TOML. (For errors.)
41
 * \param[in] config_key Key of the configuration.
42
 * \return Log level.
43
 */
44
[[nodiscard]] inline logging::LogLevel parse_log_level(std::string_view str,
45
18
    const ::toml::source_region& source, std::string_view config_key) {
46
18
    if (str == "trace") {
47
5
        return logging::LogLevel::TRACE;
48
5
    }
49
13
    if (str == "debug") {
50
2
        return logging::LogLevel::DEBUG;
51
2
    }
52
11
    if (str == "info") {
53
5
        return logging::LogLevel::INFO;
54
5
    }
55
6
    if (str == "warn") {
56
1
        return logging::LogLevel::WARN;
57
1
    }
58
5
    if (str == "error") {
59
2
        return logging::LogLevel::ERROR;
60
2
    }
61
3
    if (str == "critical") {
62
1
        return logging::LogLevel::CRITICAL;
63
1
    }
64
2
    throw_error(source, config_key);
65
2
}
66
67
/*!
68
 * \brief Parse a configuration of logging from TOML.
69
 *
70
 * \param[in] table Table in TOML.
71
 * \param[out] config Configuration.
72
 */
73
28
inline void parse_toml(const ::toml::table& table, LoggingConfig& config) {
74
33
    for (const auto& [key, value] : table) {
75
33
        const auto key_str = key.str();
76
33
        if (key_str == "file_path") {
77
13
            MSGPACK_RPC_PARSE_TOML_VALUE("file_path", file_path, std::string);
78
20
        } else if (key_str == "max_file_size") {
79
4
            MSGPACK_RPC_PARSE_TOML_VALUE(
80
4
                "max_file_size", max_file_size, std::size_t);
81
16
        } else if (key_str == "max_files") {
82
4
            MSGPACK_RPC_PARSE_TOML_VALUE("max_files", max_files, std::size_t);
83
12
        } else if (key_str == "output_log_level") {
84
12
            const auto config_value = value.value<std::string>();
85
12
            if (!config_value) {
86
1
                throw_error(value.source(), "output_log_level");
87
1
            }
88
12
            config.output_log_level(parse_log_level(
89
12
                *config_value, value.source(), "output_log_level"));
90
12
        }
91
33
    }
92
28
}
93
94
/*!
95
 * \brief Parse configurations of logging from TOML.
96
 *
97
 * \param[in] table Table in TOML.
98
 * \param[out] configs Configurations.
99
 */
100
inline void parse_toml(const ::toml::table& table,
101
12
    std::unordered_map<std::string, LoggingConfig>& configs) {
102
17
    for (const auto& [key, value] : table) {
103
17
        const auto* table_ptr = value.as_table();
104
17
        if (table_ptr == nullptr) {
105
1
            throw_error(value.source(), "logging",
106
1
                "\"logging\" must be a table of tables.");
107
1
        }
108
17
        LoggingConfig config;
109
17
        parse_toml(*table_ptr, config);
110
17
        configs.try_emplace(std::string(key.str()), std::move(config));
111
17
    }
112
12
}
113
114
}  // namespace msgpack_rpc::config::toml::impl