1// Copyright (c) Facebook, Inc. and its affiliates.
2// All rights reserved.
3//
4// This source code is licensed under the BSD-style license found in the
5// LICENSE file in the root directory of this source tree.
6
7#pragma once
8
9#include <cstring>
10#include <system_error>
11
12#include <fmt/format.h>
13
14namespace fmt {
15
16template <>
17struct formatter<std::error_category> {
18 constexpr decltype(auto) parse(format_parse_context& ctx) {
19 return ctx.begin();
20 }
21
22 template <typename FormatContext>
23 decltype(auto) format(const std::error_category& cat, FormatContext& ctx) {
24 if (std::strcmp(cat.name(), "generic") == 0) {
25 return format_to(ctx.out(), "errno");
26 } else {
27 return format_to(ctx.out(), "{} error", cat.name());
28 }
29 }
30};
31
32template <>
33struct formatter<std::error_code> {
34 constexpr decltype(auto) parse(format_parse_context& ctx) {
35 return ctx.begin();
36 }
37
38 template <typename FormatContext>
39 decltype(auto) format(const std::error_code& err, FormatContext& ctx) {
40 return format_to(
41 ctx.out(), "({}: {} - {})", err.category(), err.value(), err.message());
42 }
43};
44
45} // namespace fmt
46
47namespace c10d {
48namespace detail {
49
50inline std::error_code lastError() noexcept {
51 return std::error_code{errno, std::generic_category()};
52}
53
54} // namespace detail
55} // namespace c10d
56