ZeroErr
dbg.h
Go to the documentation of this file.
1 #pragma once
3 
4 #include "zeroerr/color.h"
5 #include "zeroerr/print.h"
6 
7 #include <iostream>
8 #include <tuple> // for std::get and std::tie
9 
11 
12 #ifndef ZEROERR_DISABLE_DBG_MARCO
13 #define dbg(...) zeroerr::DebugExpr(__FILE__, __LINE__, __func__, #__VA_ARGS__, __VA_ARGS__)
14 #else
15 #define dbg(...) (__VA_ARGS__)
16 #endif
17 
18 namespace zeroerr {
19 
20 template <class T1, class... T>
21 struct last {
22  using type = typename last<T...>::type;
23 };
24 
25 template <class T1>
26 struct last<T1> {
27  using type = T1;
28 };
29 
34 template <typename... Args>
35 auto get_last(Args&&... args) -> typename last<Args...>::type {
36  return std::get<sizeof...(Args) - 1>(std::tie(args...));
37 }
38 
39 
44 template <typename... T>
45 auto DebugExpr(const char* file, unsigned line, const char* func, const char* exprs, T... t) ->
46  typename last<T...>::type {
47  std::string fileName(file);
48  auto p = fileName.find_last_of('/');
49  if (p != std::string::npos) fileName = fileName.substr(p + 1);
50 
51  std::cerr << Dim << "[" << fileName << ":" << line << " " << func << "] " << Reset;
52  std::cerr << FgCyan << exprs << Reset << " = ";
53  Printer print(std::cerr);
54  print.line_break = "";
55  print(t...);
56  std::cerr << " (" << FgGreen;
57  std::string typenames[] = {print.type(t)...};
58  for (unsigned i = 0; i < sizeof...(T); ++i) {
59  if (i != 0) std::cerr << ", ";
60  std::cerr << typenames[i];
61  }
62  std::cerr << Reset << ")" << std::endl;
63  return get_last(t...);
64 }
65 
66 
67 } // namespace zeroerr
68 
#define ZEROERR_SUPPRESS_COMMON_WARNINGS_POP
Definition: config.h:265
#define ZEROERR_SUPPRESS_COMMON_WARNINGS_PUSH
Definition: config.h:218
Definition: benchmark.cpp:17
const char * FgCyan
Definition: color.cpp:48
auto DebugExpr(const char *file, unsigned line, const char *func, const char *exprs, T... t) -> typename last< T... >::type
DebugExpr is a function to print any type of variable with its type name. It is used by dbg macro.
Definition: dbg.h:45
const char * Dim
Definition: color.cpp:36
const char * FgGreen
Definition: color.cpp:44
auto get_last(Args &&... args) -> typename last< Args... >::type
get_last is a function to get the last argument of a variadic template. It is used by DebugExpr.
Definition: dbg.h:35
const char * Reset
Definition: color.cpp:34
A functor class Printer for printing a value of any type.
Definition: print.h:80
static std::string type(const T &t)
Definition: print.h:131
const char * line_break
Definition: print.h:125
T1 type
Definition: dbg.h:27
Definition: dbg.h:21
typename last< T... >::type type
Definition: dbg.h:22