logger.hpp
Go to the documentation of this file.
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 #pragma once
17 
19 
20 #include <stdarg.h>
21 
22 #include <memory>
23 #include <mutex>
24 #include <sstream>
25 #include <string>
26 
27 namespace spdlog {
28 class logger;
29 namespace sinks {
30 template <class Mutex>
31 class CallbackSink;
32 using callback_sink_mt = CallbackSink<std::mutex>;
33 }; // namespace sinks
34 }; // namespace spdlog
35 
36 namespace ML {
37 
50 std::string format(const char* fmt, va_list& vl);
51 std::string format(const char* fmt, ...);
64 class Logger {
65  public:
71  static Logger& get();
72 
84  void setLevel(int level);
85 
93  void setPattern(const std::string& pattern);
94 
100  void setCallback(void (*callback)(int lvl, const char* msg));
101 
107  void setFlush(void (*flush)());
108 
115  bool shouldLogFor(int level) const;
116 
122  int getLevel() const;
123 
128  std::string getPattern() const { return currPattern; }
129 
136  void log(int level, const char* fmt, ...);
137 
141  void flush();
142 
143  private:
144  Logger();
145  ~Logger() {}
146 
147  std::shared_ptr<spdlog::sinks::callback_sink_mt> sink;
148  std::shared_ptr<spdlog::logger> logger;
149  std::string currPattern;
150  static const std::string DefaultPattern;
151 }; // class Logger
152 
164  public:
169  PatternSetter(const std::string& pattern = "%v");
170 
175  ~PatternSetter();
176 
177  private:
178  std::string prevPattern;
179 }; // class PatternSetter
180 
185 #if (CUML_ACTIVE_LEVEL >= CUML_LEVEL_TRACE)
186 #define CUML_LOG_TRACE(fmt, ...) \
187  do { \
188  std::stringstream ss; \
189  ss << ML::format("%s:%d ", __FILE__, __LINE__); \
190  ss << ML::format(fmt, ##__VA_ARGS__); \
191  ML::Logger::get().log(CUML_LEVEL_TRACE, ss.str().c_str()); \
192  } while (0)
193 #else
194 #define CUML_LOG_TRACE(fmt, ...) void(0)
195 #endif
196 
197 #if (CUML_ACTIVE_LEVEL >= CUML_LEVEL_DEBUG)
198 #define CUML_LOG_DEBUG(fmt, ...) \
199  do { \
200  std::stringstream ss; \
201  ss << ML::format("%s:%d ", __FILE__, __LINE__); \
202  ss << ML::format(fmt, ##__VA_ARGS__); \
203  ML::Logger::get().log(CUML_LEVEL_DEBUG, ss.str().c_str()); \
204  } while (0)
205 #else
206 #define CUML_LOG_DEBUG(fmt, ...) void(0)
207 #endif
208 
209 #if (CUML_ACTIVE_LEVEL >= CUML_LEVEL_INFO)
210 #define CUML_LOG_INFO(fmt, ...) ML::Logger::get().log(CUML_LEVEL_INFO, fmt, ##__VA_ARGS__)
211 #else
212 #define CUML_LOG_INFO(fmt, ...) void(0)
213 #endif
214 
215 #if (CUML_ACTIVE_LEVEL >= CUML_LEVEL_WARN)
216 #define CUML_LOG_WARN(fmt, ...) ML::Logger::get().log(CUML_LEVEL_WARN, fmt, ##__VA_ARGS__)
217 #else
218 #define CUML_LOG_WARN(fmt, ...) void(0)
219 #endif
220 
221 #if (CUML_ACTIVE_LEVEL >= CUML_LEVEL_ERROR)
222 #define CUML_LOG_ERROR(fmt, ...) ML::Logger::get().log(CUML_LEVEL_ERROR, fmt, ##__VA_ARGS__)
223 #else
224 #define CUML_LOG_ERROR(fmt, ...) void(0)
225 #endif
226 
227 #if (CUML_ACTIVE_LEVEL >= CUML_LEVEL_CRITICAL)
228 #define CUML_LOG_CRITICAL(fmt, ...) ML::Logger::get().log(CUML_LEVEL_CRITICAL, fmt, ##__VA_ARGS__)
229 #else
230 #define CUML_LOG_CRITICAL(fmt, ...) void(0)
231 #endif
234 }; // namespace ML
The main Logging class for cuML library.
Definition: logger.hpp:64
bool shouldLogFor(int level) const
Tells whether messages will be logged for the given log level.
Definition: logger.cpp:83
void setLevel(int level)
Set the logging level.
Definition: logger.cpp:67
static Logger & get()
Singleton method to get the underlying logger object.
Definition: logger.cpp:52
void setCallback(void(*callback)(int lvl, const char *msg))
Register a callback function to be run in place of usual log call.
Definition: logger.cpp:79
void setFlush(void(*flush)())
Register a flush function compatible with the registered callback.
Definition: logger.cpp:81
void log(int level, const char *fmt,...)
Main logging method.
Definition: logger.cpp:96
int getLevel() const
Query for the current log level.
Definition: logger.cpp:90
void setPattern(const std::string &pattern)
Set the logging pattern.
Definition: logger.cpp:73
std::string getPattern() const
Get the current logging pattern.
Definition: logger.hpp:128
void flush()
Flush logs by calling flush on underlying logger.
Definition: logger.cpp:110
RAII based pattern setter for Logger class.
Definition: logger.hpp:163
PatternSetter(const std::string &pattern="%v")
Set the pattern for the rest of the log messages.
Definition: logger.cpp:112
~PatternSetter()
This will restore the previous pattern that was active during the moment this object was created.
Definition: logger.cpp:118
std::string format(const char *fmt, va_list &vl)
Definition: logger.cpp:28
Definition: dbscan.hpp:30
CallbackSink< std::mutex > callback_sink_mt
Definition: callbackSink.hpp:69
Definition: callbackSink.hpp:26