All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
logger.hpp
1 /*
2  * Copyright (c) 2020-2024, NVIDIA CORPORATION.
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  */
16 
17 #pragma once
18 
19 #include <rmm/cuda_stream_view.hpp>
20 #include <rmm/detail/export.hpp>
21 #include <rmm/detail/format.hpp>
22 
23 #include <spdlog/sinks/basic_file_sink.h>
24 #include <spdlog/spdlog.h>
25 
26 #include <string>
27 
28 namespace RMM_NAMESPACE {
29 
30 namespace detail {
31 
40 inline std::string default_log_filename()
41 {
42  auto* filename = std::getenv("RMM_DEBUG_LOG_FILE");
43  return (filename == nullptr) ? std::string{"rmm_log.txt"} : std::string{filename};
44 }
45 
51 
53  : logger_{"RMM",
54  std::make_shared<spdlog::sinks::basic_file_sink_mt>(
55  default_log_filename(), true // truncate file
56  )}
57  {
58  logger_.set_pattern("[%6t][%H:%M:%S:%f][%-6l] %v");
59  logger_.flush_on(spdlog::level::warn);
60 #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_INFO
61 #ifdef CUDA_API_PER_THREAD_DEFAULT_STREAM
62  logger_.info("----- RMM LOG BEGIN [PTDS ENABLED] -----");
63 #else
64  logger_.info("----- RMM LOG BEGIN [PTDS DISABLED] -----");
65 #endif
66  logger_.flush();
67 #endif
68  }
69 };
70 
71 inline spdlog::logger& logger()
72 {
73  static detail::logger_wrapper wrapped{};
74  return wrapped.logger_;
75 }
76 } // namespace detail
77 
87 [[deprecated(
88  "Support for direct access to spdlog loggers in rmm is planned for "
89  "removal")]] RMM_EXPORT inline spdlog::logger&
91 {
92  return detail::logger();
93 }
94 
96 //
97 // The default is INFO, but it should be used sparingly, so that by default a log file is only
98 // output if there is important information, warnings, errors, and critical failures
99 // Log messages that require computation should only be used at level TRACE and DEBUG
100 #define RMM_LOG_TRACE(...) \
101  SPDLOG_LOGGER_TRACE(&rmm::detail::logger(), rmm::detail::formatted_log(__VA_ARGS__))
102 #define RMM_LOG_DEBUG(...) \
103  SPDLOG_LOGGER_DEBUG(&rmm::detail::logger(), rmm::detail::formatted_log(__VA_ARGS__))
104 #define RMM_LOG_INFO(...) \
105  SPDLOG_LOGGER_INFO(&rmm::detail::logger(), rmm::detail::formatted_log(__VA_ARGS__))
106 #define RMM_LOG_WARN(...) \
107  SPDLOG_LOGGER_WARN(&rmm::detail::logger(), rmm::detail::formatted_log(__VA_ARGS__))
108 #define RMM_LOG_ERROR(...) \
109  SPDLOG_LOGGER_ERROR(&rmm::detail::logger(), rmm::detail::formatted_log(__VA_ARGS__))
110 #define RMM_LOG_CRITICAL(...) \
111  SPDLOG_LOGGER_CRITICAL(&rmm::detail::logger(), rmm::detail::formatted_log(__VA_ARGS__))
112 
114 
115 } // namespace RMM_NAMESPACE
116 
spdlog::logger & logger()
Returns the global RMM logger.
Definition: logger.hpp:90
Simple wrapper around a spdlog::logger that performs RMM-specific initialization.
Definition: logger.hpp:49
spdlog::logger logger_
The underlying logger.
Definition: logger.hpp:50