]> rtime.felk.cvut.cz Git - hercules2020/kcf.git/blob - src/debug.h
Remove unnecessary calls to cudaStreamSynchronize()
[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             IOSave s(std::cerr);
87             std::cerr << std::setprecision(precision);
88             std::cerr << indent() << name /*<< " @" << line */ << " " << print(obj) << std::endl;
89         }
90     }
91
92     template <typename T> struct Printer {
93         const T &obj;
94         Printer(const T &_obj) : obj(_obj) {}
95     };
96
97     template <typename T> Printer<T> print(const T& obj) { return Printer<T>(obj); }
98     Printer<cv::Mat> print(const MatScales& obj) { return Printer<cv::Mat>(obj); }
99     Printer<cv::Mat> print(const MatFeats& obj) { return Printer<cv::Mat>(obj); }
100     Printer<cv::Mat> print(const MatScaleFeats& obj) { return Printer<cv::Mat>(obj); }
101 };
102
103 template <typename T>
104 std::ostream &operator<<(std::ostream &os, const DbgTracer::Printer<T> &p)
105 {
106     os << p.obj;
107     return os;
108 }
109
110 #if CV_MAJOR_VERSION == 3 && CV_MINOR_VERSION < 3
111 static inline std::ostream &operator<<(std::ostream &out, const cv::MatSize &msize)
112 {
113     int i, dims = msize.p[-1];
114     for (i = 0; i < dims; i++) {
115         out << msize.p[i];
116         if (i < dims - 1)
117             out << " x ";
118     }
119     return out;
120 }
121 #endif
122
123 std::ostream &operator<<(std::ostream &os, const DbgTracer::Printer<cv::Mat> &p);
124
125 #if defined(CUFFT)
126 static inline std::ostream &operator<<(std::ostream &os, const cufftComplex &p)
127 {
128     (void)p; // TODO
129     return os;
130 }
131 #endif
132
133 std::ostream &operator<<(std::ostream &os, const DbgTracer::Printer<ComplexMat> &p);
134
135 extern DbgTracer __dbgTracer;
136
137 #define TRACE(...) const DbgTracer::FTrace __tracer(__dbgTracer, __PRETTY_FUNCTION__, ##__VA_ARGS__)
138
139 #define DEBUG_PRINT(obj) __dbgTracer.traceVal(#obj, (obj), __LINE__)
140 #define DEBUG_PRINTM(obj) DEBUG_PRINT(obj)
141 #define PRINT(obj) __dbgTracer.traceVal(#obj, (obj), __LINE__, true)
142
143 #endif // DEBUG_H