xref: /aosp_15_r20/external/pytorch/torch/csrc/distributed/c10d/error.h (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
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 
14 namespace fmt {
15 
16 template <>
17 struct formatter<std::error_category> {
18   constexpr decltype(auto) parse(format_parse_context& ctx) const {
19     return ctx.begin();
20   }
21 
22   template <typename FormatContext>
23   decltype(auto) format(const std::error_category& cat, FormatContext& ctx)
24       const {
25     if (std::strcmp(cat.name(), "generic") == 0) {
26       return fmt::format_to(ctx.out(), "errno");
27     } else {
28       return fmt::format_to(ctx.out(), "{} error", cat.name());
29     }
30   }
31 };
32 
33 template <>
34 struct formatter<std::error_code> {
35   constexpr decltype(auto) parse(format_parse_context& ctx) const {
36     return ctx.begin();
37   }
38 
39   template <typename FormatContext>
40   decltype(auto) format(const std::error_code& err, FormatContext& ctx) const {
41     return fmt::format_to(
42         ctx.out(), "({}: {} - {})", err.category(), err.value(), err.message());
43   }
44 };
45 
46 } // namespace fmt
47 
48 namespace c10d {
49 namespace detail {
50 
51 inline std::error_code lastError() noexcept {
52   return std::error_code{errno, std::generic_category()};
53 }
54 
55 } // namespace detail
56 } // namespace c10d
57