VTK-m  2.1
Logging.h
Go to the documentation of this file.
1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //============================================================================
10 #ifndef vtk_m_cont_Logging_h
11 #define vtk_m_cont_Logging_h
12 
15 
16 #include <vtkm/Types.h>
17 
19 
20 #include <iostream>
21 #include <memory>
22 #include <sstream>
23 #include <string>
24 #include <typeindex>
25 #include <typeinfo>
26 
101 
113 
126 
129 
132 
145 
148 
152 
157 
162 
170 
178 
190 
191 #define VTKM_CONCAT_IMPL(s1, s2) s1##s2
192 #define VTKM_CONCAT(s1, s2) VTKM_CONCAT_IMPL(s1, s2)
193 
194 #ifdef __COUNTER__
195 #define VTKM_ANONYMOUS_VARIABLE VTKM_CONCAT(vtk_m_anonymous_, __COUNTER__)
196 #else
197 #define VTKM_ANONYMOUS_VARIABLE VTKM_CONCAT(vtk_m_anonymous_, __LINE__)
198 #endif
199 
200 #if defined(VTKM_ENABLE_LOGGING)
201 
202 #define VTKM_LOG_IF_S(level, cond, ...) \
203  vtkm::cont::LogCondStream(level, cond, __FILE__, __LINE__) << __VA_ARGS__
204 
205 #define VTKM_LOG_IF_F(level, cond, ...) \
206  vtkm::cont::LogCond(level, cond, __FILE__, __LINE__, __VA_ARGS__)
207 
208 #define VTKM_LOG_S(level, ...) VTKM_LOG_IF_S(level, true, __VA_ARGS__)
209 #define VTKM_LOG_F(level, ...) VTKM_LOG_IF_F(level, true, __VA_ARGS__)
210 
211 #define VTKM_LOG_SCOPE(level, ...) \
212  vtkm::cont::detail::LogScope VTKM_ANONYMOUS_VARIABLE { level, __FILE__, __LINE__, __VA_ARGS__ }
213 
214 #define VTKM_LOG_SCOPE_FUNCTION(level) VTKM_LOG_SCOPE(level, __func__)
215 #define VTKM_LOG_ALWAYS_S(level, ...) VTKM_LOG_S(level, __VA_ARGS__)
216 
217 
218 // Convenience macros:
219 
220 // Cast success:
221 #define VTKM_LOG_CAST_SUCC(inObj, outObj) \
222  VTKM_LOG_F(vtkm::cont::LogLevel::Cast, \
223  "Cast succeeded: %s (%p) --> %s (%p)", \
224  vtkm::cont::TypeToString(inObj).c_str(), \
225  &inObj, \
226  vtkm::cont::TypeToString(outObj).c_str(), \
227  &outObj)
228 
229 // Cast failure:
230 #define VTKM_LOG_CAST_FAIL(inObj, outType) \
231  VTKM_LOG_F(vtkm::cont::LogLevel::Cast, \
232  "Cast failed: %s (%p) --> %s", \
233  vtkm::cont::TypeToString(inObj).c_str(), \
234  &inObj, \
235  vtkm::cont::TypeToString<outType>().c_str())
236 
237 // TryExecute failure
238 #define VTKM_LOG_TRYEXECUTE_FAIL(errorMessage, functorName, deviceId) \
239  VTKM_LOG_S(vtkm::cont::LogLevel::Error, "TryExecute encountered an error: " << errorMessage); \
240  VTKM_LOG_S(vtkm::cont::LogLevel::Error, "Failing functor: " << functorName); \
241  VTKM_LOG_S(vtkm::cont::LogLevel::Error, "Failing device: " << deviceId.GetName())
242 
243 // Same, but disabling device:
244 #define VTKM_LOG_TRYEXECUTE_DISABLE(errorMessage, functorName, deviceId) \
245  VTKM_LOG_S(vtkm::cont::LogLevel::Error, "TryExecute encountered an error: " << errorMessage); \
246  VTKM_LOG_S(vtkm::cont::LogLevel::Error, "Failing functor: " << functorName); \
247  VTKM_LOG_S(vtkm::cont::LogLevel::Error, "Failing device: " << deviceId.GetName()); \
248  VTKM_LOG_S(vtkm::cont::LogLevel::Error, "The failing device has been disabled.")
249 
250 // Custom log level
251 #define VTKM_DEFINE_USER_LOG_LEVEL(name, offset) \
252  static constexpr vtkm::cont::LogLevel name = static_cast<vtkm::cont::LogLevel>( \
253  static_cast<typename std::underlying_type<vtkm::cont::LogLevel>::type>( \
254  vtkm::cont::LogLevel::UserFirst) + \
255  offset % \
256  static_cast<typename std::underlying_type<vtkm::cont::LogLevel>::type>( \
257  vtkm::cont::LogLevel::UserLast))
258 
259 #else // VTKM_ENABLE_LOGGING
260 
261 #define VTKM_LOG_S(level, ...)
262 #define VTKM_LOG_F(level, ...)
263 #define VTKM_LOG_IF_S(level, cond, ...)
264 #define VTKM_LOG_IF_F(level, cond, ...)
265 #define VTKM_LOG_SCOPE(level, ...)
266 #define VTKM_LOG_SCOPE_FUNCTION(level)
267 #define VTKM_LOG_ERROR_CONTEXT(desc, data)
268 #define VTKM_LOG_CAST_SUCC(inObj, outObj)
269 #define VTKM_LOG_CAST_FAIL(inObj, outType)
270 #define VTKM_DEFINE_USER_LOG_LEVEL(name, offset)
271 
272 // Always emitted. When logging is disabled, std::cerr is used.
273 
274 #define VTKM_LOG_ALWAYS_S(level, ...) \
275  (static_cast<int>(level) < 0 ? std::cerr : std::cout) \
276  << vtkm::cont::GetLogLevelName(level) << ": " << __VA_ARGS__ << "\n"
277 
278 // TryExecute failures are still important enough to log, but we just write to
279 // std::cerr when logging is disabled.
280 #define VTKM_LOG_TRYEXECUTE_FAIL(errorMessage, functorName, deviceId) \
281  std::cerr << "Error: TryExecute encountered an error: " << errorMessage << "\n" \
282  << "\t- Failing functor: " << functorName << "\n" \
283  << "\t- Failing device: " << deviceId.GetName() << "\n\n"
284 #define VTKM_LOG_TRYEXECUTE_DISABLE(errorMessage, functorName, deviceId) \
285  std::cerr << "Error: TryExecute encountered an error: " << errorMessage << "\n" \
286  << "\t- Failing functor: " << functorName << "\n" \
287  << "\t- Failing device: " << deviceId.GetName() << "\n" \
288  << "The failing device has been disabled.\n\n"
289 
290 #endif // VTKM_ENABLE_LOGGING
291 
292 namespace vtkm
293 {
294 namespace cont
295 {
296 
298 enum class LogLevel
299 {
302  Off = -9, //loguru::Verbosity_OFF,
303 
305  Fatal = -3, // loguru::Verbosity_FATAL,
306 
308  Error = -2, // loguru::Verbosity_ERROR,
309 
311  Warn = -1, // loguru::Verbosity_WARNING,
312 
315  Info = 0, //loguru::Verbosity_INFO,
316 
319  UserFirst = 1,
321  UserLast = 255,
322 
325 
328  Perf,
329 
331  MemCont,
332 
334  MemExec,
335 
337  MemTransfer,
338 
341 
344  Cast,
345 
350  UserVerboseFirst = 1024,
352  UserVerboseLast = 2047
353 };
354 
355 
373 VTKM_CONT
374 void InitLogging(int& argc, char* argv[], const std::string& loggingFlag = "--vtkm-log-level");
376 VTKM_CONT
377 void InitLogging();
386 VTKM_CONT
387 void SetStderrLogLevel(const char* verbosity);
388 
390 VTKM_CONT
398 VTKM_CONT
400 
411 VTKM_CONT
412 void SetLogLevelName(vtkm::cont::LogLevel level, const std::string& name);
413 
420 VTKM_CONT
421 std::string GetLogLevelName(vtkm::cont::LogLevel level);
422 
428 VTKM_CONT
429 void SetLogThreadName(const std::string& name);
431 VTKM_CONT
432 std::string GetLogThreadName();
435 // Per-thread error context, not currently used, undocumented....
437 VTKM_CONT
438 std::string GetLogErrorContext();
439 
446 VTKM_CONT
447 std::string GetStackTrace(vtkm::Int32 skip = 0);
448 
454 VTKM_CONT
455 std::string GetHumanReadableSize(vtkm::UInt64 bytes, int prec = 2);
456 
457 template <typename T>
458 VTKM_CONT inline std::string GetHumanReadableSize(T&& bytes, int prec = 2)
459 {
460  return GetHumanReadableSize(static_cast<vtkm::UInt64>(std::forward<T>(bytes)), prec);
461 }
463 
468 VTKM_CONT
469 std::string GetSizeString(vtkm::UInt64 bytes, int prec = 2);
470 
471 template <typename T>
472 VTKM_CONT inline std::string GetSizeString(T&& bytes, int prec = 2)
473 {
474  return GetSizeString(static_cast<vtkm::UInt64>(std::forward<T>(bytes)), prec);
475 }
477 
483 VTKM_CONT_EXPORT VTKM_CONT std::string TypeToString(const std::type_info& t);
484 VTKM_CONT_EXPORT VTKM_CONT std::string TypeToString(const std::type_index& t);
485 template <typename T>
486 inline VTKM_CONT std::string TypeToString()
487 {
488  return TypeToString(typeid(T));
489 }
490 template <typename T>
491 inline VTKM_CONT std::string TypeToString(const T&)
492 {
493  return TypeToString(typeid(T));
494 }
497 #ifdef VTKM_ENABLE_LOGGING
498 
509 VTKM_CONT
510 void LogCond(LogLevel level, bool cond, const char* file, unsigned line, const char* format...);
511 
512 namespace detail
513 {
514 
522 class VTKM_CONT_EXPORT LogScope
523 {
524  struct InternalStruct;
525  std::unique_ptr<InternalStruct> Internals;
526 
527 public:
528  /*
529  * \param level Desired LogLevel value for the log message.
530  * \param cond When false this function is no-op.
531  * \param format Printf like format string.
532  */
533  VTKM_CONT
534  LogScope(LogLevel level, const char* file, unsigned line, const char* format...);
535 
536  VTKM_CONT ~LogScope();
537 };
538 
539 
540 } // namespace detail
541 
548 {
549  VTKM_CONT
550  LogCondStream(LogLevel level, bool cond, const char* file, int line)
551  : Level(level)
552  , Condition(cond)
553  , File(file)
554  , Line(line)
555  {
556  }
557 
558  VTKM_CONT
559  ~LogCondStream() noexcept(false);
560 
561  template <typename T>
562  VTKM_CONT LogCondStream& operator<<(const T& in)
563  {
564  SStream << in;
565  return *this;
566  }
567 
568  VTKM_CONT
569  LogCondStream& operator<<(std::ostream& (*f)(std::ostream&))
570  {
571  f(SStream);
572  return *this;
573  }
574 
575 private:
577  bool Condition;
578  const char* File;
579  int Line;
580  std::ostringstream SStream;
581 };
582 #endif // VTKM_ENABLE_LOGGING
583 
584 }
585 } // end namespace vtkm::cont
586 
587 #endif // vtk_m_cont_Logging_h
vtkm::Ray
Represent an infinite or semi-infinite line segment with a point and a direction.
Definition: Geometry.h:21
vtkm::cont::TypeToString
std::string TypeToString(const std::type_info &t)
Use RTTI information to retrieve the name of the type T.
vtkm::cont::LogLevel::UserVerboseFirst
@ UserVerboseFirst
The first in a range of logging levels reserved for code that uses VTK-m.
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::cont::LogLevel::Warn
@ Warn
Less important user errors, such as out-of-bounds parameters.
Types.h
vtkm::cont::GetStackTrace
std::string GetStackTrace(vtkm::Int32 skip=0)
Returns a stacktrace on supported platforms.
vtkm::cont::LogLevel::DevicesEnabled
@ DevicesEnabled
Information about which devices are enabled/disabled.
vtkm::cont::LogCondStream::Level
LogLevel Level
Definition: Logging.h:576
vtkm::cont::LogLevel::MemTransfer
@ MemTransfer
Transferring of data between a host and device.
vtkm::cont::LogLevel::UserLast
@ UserLast
The last in a range of logging levels reserved for code that uses VTK-m.
vtkm::cont::LogLevel
LogLevel
Log levels for use with the logging macros.
Definition: Logging.h:298
vtkm::cont::Error
The superclass of all exceptions thrown by any VTKm function or method.
Definition: Error.h:33
vtkm::cont::LogCondStream::Condition
bool Condition
Definition: Logging.h:577
vtkm::cont::LogLevel::UserVerboseLast
@ UserVerboseLast
The last in a range of logging levels reserved for code that uses VTK-m.
vtkm::cont::LogLevel::Cast
@ Cast
Reports when a dynamic object is (or is not) resolved via a CastAndCall or other casting method.
vtkm::cont::GetLogThreadName
std::string GetLogThreadName()
Specifies a humman-readable name to identify the current thread in the log output.
vtkm::cont::LogCond
void LogCond(LogLevel level, bool cond, const char *file, unsigned line, const char *format...)
Conditionally logs a message with a printf-like format.
vtkm::cont::LogCondStream
Conditionally logs a message with a stream-like interface.
Definition: Logging.h:547
vtkm::cont::GetSizeString
std::string GetSizeString(vtkm::UInt64 bytes, int prec=2)
Returns "%1 (%2 bytes)" where %1 is the result from GetHumanReadableSize and %2 is the exact number o...
ExportMacros.h
vtkm::cont::SetLogLevelName
void SetLogLevelName(vtkm::cont::LogLevel level, const std::string &name)
Register a custom name to identify a log level.
vtkm::cont::LogLevel::Info
@ Info
Information messages (detected hardware, etc) and temporary debugging output.
vtkm::cont::SetStderrLogLevel
void SetStderrLogLevel(const char *verbosity)
Set the range of log levels that will be printed to stderr.
vtkm::cont::LogLevel::KernelLaunches
@ KernelLaunches
Details on device-side kernel launches.
vtkm::cont::LogCondStream::SStream
std::ostringstream SStream
Definition: Logging.h:580
VTKM_CONT_EXPORT
#define VTKM_CONT_EXPORT
Definition: vtkm_cont_export.h:44
vtkm::cont::LogCondStream::Line
int Line
Definition: Logging.h:579
Configure.h
vtkm::cont::LogCondStream::operator<<
LogCondStream & operator<<(std::ostream &(*f)(std::ostream &))
Definition: Logging.h:569
vtkm_cont_export.h
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::cont::LogLevel::MemCont
@ MemCont
Host-side resource allocations/frees (e.g. ArrayHandle control buffers).
vtkm::cont::LogLevel::Fatal
@ Fatal
Fatal errors that should abort execution.
vtkm::cont::GetLogLevelName
std::string GetLogLevelName(vtkm::cont::LogLevel level)
Get a human readable name for the log level.
vtkm::cont::GetStderrLogLevel
vtkm::cont::LogLevel GetStderrLogLevel()
Get the active highest log level that will be printed to stderr.
vtkm::cont::SetLogThreadName
void SetLogThreadName(const std::string &name)
Specifies a humman-readable name to identify the current thread in the log output.
vtkm::cont::LogLevel::Off
@ Off
A placeholder used to silence all logging.
vtkm::cont::LogLevel::MemExec
@ MemExec
Device-side resource allocations/frees (e.g ArrayHandle device buffers).
vtkm::cont::InitLogging
void InitLogging(int &argc, char *argv[], const std::string &loggingFlag="--vtkm-log-level")
This shouldn't be called directly – prefer calling vtkm::cont::Initialize, which takes care of loggin...
vtkm::UInt64
unsigned long long UInt64
Base type to use for 64-bit signed integer numbers.
Definition: Types.h:207
vtkm::Int32
int32_t Int32
Base type to use for 32-bit signed integer numbers.
Definition: Types.h:181
vtkm::cont::GetLogErrorContext
std::string GetLogErrorContext()
vtkm::cont::LogCondStream::File
const char * File
Definition: Logging.h:578
vtkm::cont::LogCondStream::LogCondStream
LogCondStream(LogLevel level, bool cond, const char *file, int line)
Definition: Logging.h:550
vtkm::cont::GetHumanReadableSize
std::string GetHumanReadableSize(vtkm::UInt64 bytes, int prec=2)
Convert a size in bytes to a human readable string (such as "64 bytes", "1.44 MiB",...
vtkm::cont::LogLevel::UserFirst
@ UserFirst
The first in a range of logging levels reserved for code that uses VTK-m.
vtkm::cont::LogLevel::Perf
@ Perf
General timing data and algorithm flow information, such as filter execution, worklet dispatches,...