numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
format_errno.h
Go to the documentation of this file.
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 */
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
31namespace num_collect::util {
32
41template <typename... Args>
42[[nodiscard]] inline auto format_errno(
43 fmt::format_string<Args...> format, Args&&... args) -> std::string {
44 const int current_errno = errno;
45 fmt::memory_buffer buffer;
46 fmt::format_to(
47 std::back_inserter(buffer), format, std::forward<Args>(args)...);
48 if (current_errno != 0) {
49 const auto error_code =
50 std::error_code(current_errno, std::generic_category());
51 fmt::format_to(
52 std::back_inserter(buffer), ": {}", error_code.message());
53 }
54 return std::string(buffer.data(), buffer.size());
55}
56
57} // namespace num_collect::util
Namespace of utilities.
Definition assert.h:30
auto format_errno(fmt::format_string< Args... > format, Args &&... args) -> std::string
Format a message with error message determined by errno.