Skip to content

Commit 7490f89

Browse files
committed
Add simple level based debug infrastructure: yDebug
1 parent 29558a1 commit 7490f89

File tree

5 files changed

+85
-5
lines changed

5 files changed

+85
-5
lines changed

common_macros.hh

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
// src: avr-include/avr/interrupt.h
1111
#ifndef __STRINGIFY
12-
# define __STRINGIFY(x) #x
12+
# define __STRINGIFY2(x) #x
13+
# define __STRINGIFY(x) __STRINGIFY2(x)
1314
#endif
1415

1516
#endif

debug.hh

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#ifndef __YAAL_DEBUG__
2+
#define __YAAL_DEBUG__ 1
3+
4+
#include "common_macros.hh"
5+
6+
// Some ideas of yDebug come from QDebug and KDebug
7+
8+
/* FIXME: defining _T here brakes functionality on real AVR hardware
9+
* Currently this setup works ok on test programs (run on development computer)
10+
* i.e. yDebug is not ready for debugging on AVR, but works nicely for developing tests
11+
*/
12+
#ifndef _T
13+
# define _T(s) (s)
14+
#endif
15+
16+
17+
namespace yaal {
18+
struct YaalNoDebug {
19+
template<typename T>
20+
YaalNoDebug& operator << (const T&) {
21+
return *this;
22+
}
23+
};
24+
}
25+
#define yDebugImplPipeNull ::yaal::YaalNoDebug()
26+
27+
#ifndef yDebugImplPipe
28+
# define yDebugImplPipe yDebugImplPipeNull
29+
#endif
30+
31+
#ifndef yDebugImplEndl
32+
# define yDebugImplEndl '\n'
33+
#endif
34+
35+
#ifndef yDebugImpl
36+
namespace yaal {
37+
struct YaalDebug {
38+
YaalDebug() {}
39+
~YaalDebug() {
40+
yDebugImplPipe << yDebugImplEndl;
41+
}
42+
43+
template<typename T>
44+
YaalDebug& operator << (const T& o) {
45+
yDebugImplPipe << o;
46+
return *this;
47+
}
48+
};
49+
}
50+
# define yDebugImpl ::yaal::YaalDebug()
51+
#endif
52+
53+
#define yDebugIf(level, level_name) if (level >= yDebugLevel) \
54+
yDebugImpl << _T(level_name " " __FILE__ ":" __STRINGIFY(__LINE__) " ")
55+
56+
#define yDebug() yDebugIf(1, "DEBUG")
57+
#define yInfo() yDebugIf(2, "INFO")
58+
#define yWarning() yDebugIf(3, "WARNING")
59+
#define yError() yDebugIf(4, "ERROR")
60+
#define yCritical() yDebugIf(5, "CRITICAL")
61+
#define yFatal() yDebugIf(6, "FATAL")
62+
63+
#ifndef yDebugLevel
64+
# define yDebugLevel 0
65+
#endif
66+
67+
#endif

requirements.hh

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
# include <stdint.h>
2222
# include "common_macros.hh"
2323
# include "qualifiers.hh"
24+
# include "debug.hh"
2425

2526
// Fixes for avr gcc library
2627
// Allow debrecated macros (Some debrecated macros are used inside #ifdef in io/registers)

tests/testing.hh

+10-1
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ typedef unsigned long long reg_size_t;
2828
#define EQ(left, right) ASSERT(left, ==, right)
2929

3030
#define EQ_F2(left, right, mult, accuracy) do { \
31-
cout << left << " =?= " << right << endl; \
31+
std::cout << left << " <> " << right << " -> "; \
3232
signed long a_ = (signed long)(left * mult); \
3333
signed long b_ = (signed long)(right * mult); \
34+
std::cout << std::dec << a_ << " <> " << std::dec << b_ << std::endl; \
3435
signed long d_ = a_ - b_; \
3536
signed long ad_ = d_ < 0 ? -d_ : d_; \
3637
ASSERT(ad_, < , accuracy); \
@@ -104,4 +105,12 @@ void print_hex<float>(std::ostream& out, const float& value) {
104105
out << "0x" << std::hex << *(int*)&value << " (" << value << ")";
105106
};
106107

108+
109+
// define yDebugImpl so debug messages are outputted
110+
#ifndef NO_YDEBUG
111+
# include <yaal/common_macros.hh>
112+
# define yDebugImplPipe std::cout
113+
# define yDebugImplEndl std::endl
114+
#endif
115+
107116
#endif

util/formating.hh

+5-3
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,19 @@ namespace yaal {
9999
}
100100

101101

102-
/* ProgramMemory String */
102+
/* Program Memory String */
103103

104104
# ifdef __AVR__
105+
// _T is defined in debug.hh as pass-through macro, so undefine it first
106+
# ifdef _T
107+
# undef _T
108+
# endif
105109
# define _T(s) (__extension__({ \
106110
static const char __attribute__ ((__progmem__,used)) __CONCAT(c_,__LINE__)[] = (s); \
107111
::yaal::internal::StreamPgmString __CONCAT(p_,__LINE__); \
108112
__CONCAT(p_,__LINE__).string = __CONCAT(c_,__LINE__); \
109113
__CONCAT(p_,__LINE__); \
110114
}))
111-
# else
112-
# define _T(s) (s)
113115
# endif
114116

115117

0 commit comments

Comments
 (0)