logging.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. #pragma once
  7. #include <string>
  8. #include <c10/macros/Macros.h>
  9. #include <c10/util/Logging.h>
  10. #include <fmt/format.h>
  11. namespace c10d {
  12. namespace detail {
  13. enum class LogLevel {
  14. Trace,
  15. Debug,
  16. Info,
  17. Warning,
  18. Error
  19. };
  20. TORCH_API bool isLogLevelEnabled(LogLevel level) noexcept;
  21. template <typename... T>
  22. std::string formatLogMessage(fmt::string_view fmt, T&&... args) {
  23. return fmt::vformat(fmt, fmt::make_format_args(args...));
  24. }
  25. } // namespace detail
  26. } // namespace c10d
  27. #define C10D_ERROR(...)\
  28. LOG_IF(ERROR, c10d::detail::isLogLevelEnabled(c10d::detail::LogLevel::Error))\
  29. << "[c10d] " << c10d::detail::formatLogMessage(__VA_ARGS__)
  30. #define C10D_WARNING(...)\
  31. LOG_IF(WARNING, c10d::detail::isLogLevelEnabled(c10d::detail::LogLevel::Warning))\
  32. << "[c10d] " << c10d::detail::formatLogMessage(__VA_ARGS__)
  33. #define C10D_INFO(...)\
  34. LOG_IF(INFO, c10d::detail::isLogLevelEnabled(c10d::detail::LogLevel::Info))\
  35. << "[c10d] " << c10d::detail::formatLogMessage(__VA_ARGS__)
  36. #define C10D_DEBUG(...)\
  37. LOG_IF(INFO, c10d::detail::isLogLevelEnabled(c10d::detail::LogLevel::Debug))\
  38. << "[c10d - debug] " << c10d::detail::formatLogMessage(__VA_ARGS__)
  39. #define C10D_TRACE(...)\
  40. LOG_IF(INFO, c10d::detail::isLogLevelEnabled(c10d::detail::LogLevel::Trace))\
  41. << "[c10d - trace] " << c10d::detail::formatLogMessage(__VA_ARGS__)