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