xref: /aosp_15_r20/external/pytorch/torch/csrc/distributed/c10d/logging.h (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1 // Copyright (c) Meta Platforms, 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 <string>
10 
11 #include <c10/macros/Macros.h>
12 #include <c10/util/Logging.h>
13 #include <fmt/format.h>
14 
15 namespace c10d {
16 namespace detail {
17 
18 enum class LogLevel { Trace, Debug, Info, Warning, Error };
19 
20 TORCH_API bool isLogLevelEnabled(LogLevel level) noexcept;
21 
22 template <typename... T>
formatLogMessage(fmt::string_view fmt,T &&...args)23 std::string formatLogMessage(fmt::string_view fmt, T&&... args) {
24   return fmt::vformat(fmt, fmt::make_format_args(args...));
25 }
26 
27 } // namespace detail
28 } // namespace c10d
29 
30 #define C10D_ERROR(...)                                                      \
31   LOG_IF(                                                                    \
32       ERROR, c10d::detail::isLogLevelEnabled(c10d::detail::LogLevel::Error)) \
33       << "[c10d] " << c10d::detail::formatLogMessage(__VA_ARGS__)
34 
35 #define C10D_WARNING(...)                                               \
36   LOG_IF(                                                               \
37       WARNING,                                                          \
38       c10d::detail::isLogLevelEnabled(c10d::detail::LogLevel::Warning)) \
39       << "[c10d] " << c10d::detail::formatLogMessage(__VA_ARGS__)
40 
41 #define C10D_INFO(...)                                                        \
42   LOG_IF(INFO, c10d::detail::isLogLevelEnabled(c10d::detail::LogLevel::Info)) \
43       << "[c10d] " << c10d::detail::formatLogMessage(__VA_ARGS__)
44 
45 #define C10D_DEBUG(...)                                                        \
46   LOG_IF(INFO, c10d::detail::isLogLevelEnabled(c10d::detail::LogLevel::Debug)) \
47       << "[c10d - debug] " << c10d::detail::formatLogMessage(__VA_ARGS__)
48 
49 #define C10D_TRACE(...)                                                        \
50   LOG_IF(INFO, c10d::detail::isLogLevelEnabled(c10d::detail::LogLevel::Trace)) \
51       << "[c10d - trace] " << c10d::detail::formatLogMessage(__VA_ARGS__)
52