|
20 | 20 | #include <rmm/detail/error.hpp> |
21 | 21 |
|
22 | 22 | #include <spdlog/sinks/basic_file_sink.h> |
| 23 | +#include <spdlog/sinks/stdout_sinks.h> |
23 | 24 | #include <spdlog/spdlog.h> |
24 | 25 | #include <sstream> |
25 | 26 | #include <memory> |
26 | 27 |
|
27 | 28 | namespace rmm { |
28 | 29 | namespace mr { |
| 30 | + |
| 31 | +/** |
| 32 | + * @brief Output location of the log. |
| 33 | + */ |
| 34 | +enum class LogLocation { |
| 35 | + FILE = 1, |
| 36 | + STDOUT = 2, |
| 37 | + STDERR = 3 |
| 38 | +}; |
| 39 | + |
29 | 40 | /** |
30 | 41 | * @brief Resource that uses `Upstream` to allocate memory and logs information |
31 | 42 | * about the requested allocation/deallocations. |
32 | 43 | * |
33 | | - * An instance of this resource can be constructured with an existing, upstream |
| 44 | + * An instance of this resource can be constructed with an existing, upstream |
34 | 45 | * resource in order to satisfy allocation requests and log |
35 | 46 | * allocation/deallocation activity. |
36 | 47 | * |
@@ -59,15 +70,29 @@ class logging_resource_adaptor final : public device_memory_resource { |
59 | 70 | * @param filename Name of file to write log info. If not specified, retrieves |
60 | 71 | * the file name from the environment variable "RMM_LOG_FILE". |
61 | 72 | */ |
62 | | - logging_resource_adaptor(Upstream* upstream, std::string const& filename = |
63 | | - get_default_filename()) |
64 | | - : upstream_{upstream}, |
65 | | - logger_{std::make_shared<spdlog::logger>( |
66 | | - "RMM", std::make_shared<spdlog::sinks::basic_file_sink_mt>( |
67 | | - filename, true /*truncate file*/))} { |
| 73 | + logging_resource_adaptor(Upstream* upstream, |
| 74 | + LogLocation location = LogLocation::FILE, |
| 75 | + std::string const& filename = {}) |
| 76 | + : upstream_{upstream} { |
68 | 77 | RMM_EXPECTS(nullptr != upstream, |
69 | 78 | "Unexpected null upstream resource pointer."); |
70 | 79 |
|
| 80 | + switch (location) { |
| 81 | + case LogLocation::FILE: |
| 82 | + logger_ = std::make_shared<spdlog::logger>( |
| 83 | + "RMM", std::make_shared<spdlog::sinks::basic_file_sink_mt>( |
| 84 | + filename.empty() ? get_default_filename() : filename, true /*truncate file*/)); |
| 85 | + break; |
| 86 | + case LogLocation::STDOUT: |
| 87 | + logger_ = std::make_shared<spdlog::logger>( |
| 88 | + "RMM", std::make_shared<spdlog::sinks::stdout_sink_mt>()); |
| 89 | + break; |
| 90 | + case LogLocation::STDERR: |
| 91 | + logger_ = std::make_shared<spdlog::logger>( |
| 92 | + "RMM", std::make_shared<spdlog::sinks::stderr_sink_mt>()); |
| 93 | + break; |
| 94 | + } |
| 95 | + |
71 | 96 | auto const csv_header{"Time,Action,Pointer,Size,Stream"}; |
72 | 97 | logger_->set_pattern("%v"); |
73 | 98 | logger_->info(csv_header); |
@@ -99,11 +124,6 @@ class logging_resource_adaptor final : public device_memory_resource { |
99 | 124 | bool supports_get_mem_info() const noexcept override { return upstream_->supports_streams(); } |
100 | 125 |
|
101 | 126 | private: |
102 | | - // make_logging_adaptor needs access to private get_default_filename |
103 | | - template <typename T> |
104 | | - friend logging_resource_adaptor<T> make_logging_adaptor( |
105 | | - T* upstream, std::string const& filename); |
106 | | - |
107 | 127 | /** |
108 | 128 | * @brief Return the value of the environment variable RMM_LOG_FILE. |
109 | 129 | * |
@@ -231,9 +251,9 @@ class logging_resource_adaptor final : public device_memory_resource { |
231 | 251 | template <typename Upstream> |
232 | 252 | logging_resource_adaptor<Upstream> make_logging_adaptor( |
233 | 253 | Upstream* upstream, |
234 | | - std::string const& filename = |
235 | | - logging_resource_adaptor<Upstream>::get_default_filename()) { |
236 | | - return logging_resource_adaptor<Upstream>{upstream, filename}; |
| 254 | + LogLocation location = LogLocation::FILE, |
| 255 | + std::string const& filename = {}) { |
| 256 | + return logging_resource_adaptor<Upstream>{upstream, location, filename}; |
237 | 257 | } |
238 | 258 |
|
239 | 259 | } // namespace mr |
|
0 commit comments