Coverage Report

Created: 2024-10-11 06:23

/builds/MusicScience37Projects/numerical-analysis/numerical-collection-cpp/include/num_collect/util/format_errno.h
Line
Count
Source
1
/*
2
 * Copyright 2022 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 format_errno function.
19
 */
20
#pragma once
21
22
#include <cerrno>
23
#include <iterator>
24
#include <string>
25
#include <system_error>
26
#include <utility>
27
28
#include <fmt/base.h>
29
#include <fmt/format.h>
30
31
namespace num_collect::util {
32
33
/*!
34
 * \brief Format a message with error message determined by errno.
35
 *
36
 * \tparam Args Types of arguments.
37
 * \param[in] format Format.
38
 * \param[in] args Arguments.
39
 * \return Formatted message.
40
 */
41
template <typename... Args>
42
[[nodiscard]] inline auto format_errno(
43
3
    fmt::format_string<Args...> format, Args&&... args) -> std::string {
44
3
    const int current_errno = errno;
45
3
    fmt::memory_buffer buffer;
46
3
    fmt::format_to(
47
3
        std::back_inserter(buffer), format, std::forward<Args>(args)...);
48
3
    if (current_errno != 0) {
49
2
        const auto error_code =
50
2
            std::error_code(current_errno, std::generic_category());
51
2
        fmt::format_to(
52
2
            std::back_inserter(buffer), ": {}", error_code.message());
53
2
    }
54
3
    return std::string(buffer.data(), buffer.size());
55
3
}
_ZN11num_collect4util12format_errnoIJEEENSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEN3fmt3v1119basic_format_stringIcJDpNSA_13type_identityIT_E4typeEEEEDpOSD_
Line
Count
Source
43
1
    fmt::format_string<Args...> format, Args&&... args) -> std::string {
44
1
    const int current_errno = errno;
45
1
    fmt::memory_buffer buffer;
46
1
    fmt::format_to(
47
1
        std::back_inserter(buffer), format, std::forward<Args>(args)...);
48
1
    if (current_errno != 0) {
49
1
        const auto error_code =
50
1
            std::error_code(current_errno, std::generic_category());
51
1
        fmt::format_to(
52
1
            std::back_inserter(buffer), ": {}", error_code.message());
53
1
    }
54
1
    return std::string(buffer.data(), buffer.size());
55
1
}
Unexecuted instantiation: _ZN11num_collect4util12format_errnoIJRKNSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEERPKcEEES8_N3fmt3v1119basic_format_stringIcJDpNSF_13type_identityIT_E4typeEEEEDpOSI_
_ZN11num_collect4util12format_errnoIJRA14_KcEEENSt3__112basic_stringIcNS5_11char_traitsIcEENS5_9allocatorIcEEEEN3fmt3v1119basic_format_stringIcJDpNSD_13type_identityIT_E4typeEEEEDpOSG_
Line
Count
Source
43
2
    fmt::format_string<Args...> format, Args&&... args) -> std::string {
44
2
    const int current_errno = errno;
45
2
    fmt::memory_buffer buffer;
46
2
    fmt::format_to(
47
2
        std::back_inserter(buffer), format, std::forward<Args>(args)...);
48
2
    if (current_errno != 0) {
49
1
        const auto error_code =
50
1
            std::error_code(current_errno, std::generic_category());
51
1
        fmt::format_to(
52
1
            std::back_inserter(buffer), ": {}", error_code.message());
53
1
    }
54
2
    return std::string(buffer.data(), buffer.size());
55
2
}
56
57
}  // namespace num_collect::util