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