]> rtime.felk.cvut.cz Git - hercules2020/kcf.git/blob - src/debug.h
Add NVTX markers to the code
[hercules2020/kcf.git] / src / debug.h
1 #ifndef DEBUG_H
2 #define DEBUG_H
3
4 #include <ios>
5 #include <iomanip>
6 #include <stdarg.h>
7 #include <stdio.h>
8 #include <opencv2/opencv.hpp>
9 #include "dynmem.hpp"
10 #include "complexmat.hpp"
11
12 #ifdef CUFFT
13 #include <cufft.h>
14 #include "nvToolsExt.h"
15 #endif
16
17
18 class IOSave
19 {
20     std::ios&           stream;
21     std::ios::fmtflags  flags;
22     std::streamsize     precision;
23     char                fill;
24 public:
25     IOSave( std::ios& userStream )
26         : stream( userStream )
27         , flags( userStream.flags() )
28         , precision( userStream.precision() )
29         , fill( userStream.fill() )
30     {
31     }
32     ~IOSave()
33     {
34         stream.flags( flags );
35         stream.precision( precision );
36         stream.fill( fill );
37     }
38 };
39
40 class DbgTracer {
41     int indentLvl = 0;
42
43   public:
44     bool debug = false;
45     static constexpr int precision = 2;
46
47     std::string indent() { return std::string(indentLvl * 4, ' '); }
48
49     class FTrace {
50         DbgTracer &t;
51         const char *funcName;
52
53       public:
54         FTrace(DbgTracer &dt, const char *fn, const char *format, ...) : t(dt), funcName(fn)
55         {
56 #ifdef CUFFT
57             nvtxRangePushA(fn);
58 #endif
59             if (!t.debug) return;
60             char *arg;
61             va_list vl;
62             va_start(vl, format);
63             if (-1 == vasprintf(&arg, format, vl))
64                 throw std::runtime_error("vasprintf error");
65             va_end(vl);
66
67             std::cerr << t.indent() << funcName << "(" << arg << ") {" << std::endl;
68             dt.indentLvl++;
69         }
70         ~FTrace()
71         {
72 #ifdef CUFFT
73             nvtxRangePop();
74 #endif
75             if (!t.debug) return;
76             t.indentLvl--;
77             std::cerr << t.indent() << "}" << std::endl;
78         }
79     };
80
81     template <typename T>
82     void traceVal(const char *name, const T& obj, int line, bool always = false)
83     {
84         (void)line;
85         if (debug || always) {
86 #ifdef CUFFT
87             CudaSafeCall(cudaStreamSynchronize(cudaStreamPerThread));
88 #endif
89             IOSave s(std::cerr);
90             std::cerr << std::setprecision(precision);
91             std::cerr << indent() << name /*<< " @" << line */ << " " << print(obj) << std::endl;
92         }
93     }
94
95     template <typename T> struct Printer {
96         const T &obj;
97         Printer(const T &_obj) : obj(_obj) {}
98     };
99
100     template <typename T> Printer<T> print(const T& obj) { return Printer<T>(obj); }
101     Printer<cv::Mat> print(const MatScales& obj) { return Printer<cv::Mat>(obj); }
102     Printer<cv::Mat> print(const MatFeats& obj) { return Printer<cv::Mat>(obj); }
103     Printer<cv::Mat> print(const MatScaleFeats& obj) { return Printer<cv::Mat>(obj); }
104 };
105
106 template <typename T>
107 std::ostream &operator<<(std::ostream &os, const DbgTracer::Printer<T> &p)
108 {
109     os << p.obj;
110     return os;
111 }
112
113 #if CV_MAJOR_VERSION == 3 && CV_MINOR_VERSION < 3
114 static inline std::ostream &operator<<(std::ostream &out, const cv::MatSize &msize)
115 {
116     int i, dims = msize.p[-1];
117     for (i = 0; i < dims; i++) {
118         out << msize.p[i];
119         if (i < dims - 1)
120             out << " x ";
121     }
122     return out;
123 }
124 #endif
125
126 std::ostream &operator<<(std::ostream &os, const DbgTracer::Printer<cv::Mat> &p);
127
128 #if defined(CUFFT)
129 static inline std::ostream &operator<<(std::ostream &os, const cufftComplex &p)
130 {
131     (void)p; // TODO
132     return os;
133 }
134 #endif
135
136 std::ostream &operator<<(std::ostream &os, const DbgTracer::Printer<ComplexMat> &p);
137
138 extern DbgTracer __dbgTracer;
139
140 #define TRACE(...) const DbgTracer::FTrace __tracer(__dbgTracer, __PRETTY_FUNCTION__, ##__VA_ARGS__)
141
142 #define DEBUG_PRINT(obj) __dbgTracer.traceVal(#obj, (obj), __LINE__)
143 #define DEBUG_PRINTM(obj) DEBUG_PRINT(obj)
144 #define PRINT(obj) __dbgTracer.traceVal(#obj, (obj), __LINE__, true)
145
146 #endif // DEBUG_H