-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmbLog.h
117 lines (109 loc) · 4.32 KB
/
mbLog.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#pragma once
// for log output
// - allows simple redirect to another Serial (or whatever)
// - allows quick disable without uncommenting each Seral.print..
// - stream style
#include <Arduino.h>
#ifdef WITH_THREADS
#include <TeensyThreads.h>
#define LK Threads::Scope m(_mutex);
#else
#define LK
#endif
// does not work with typedef :( ???
#ifndef MbLogOut
#define MbLogOut Serial
#endif
class MbLog
{
public:
#ifdef ARDUINO_ARCH_STM32
typedef int how_t; // someone alsways needs spezial treatment :(
#else
typedef uint8_t how_t;
#endif
enum FORMAT {
bin, dec, oct, hex, endl
};
MbLog() {}
inline void begin() {}
inline MbLog& dez(byte val) {_dez = val; return *this;}
inline void print (byte val, how_t how = DEC) { LK MbLogOut.print(val,how); }
inline void println(byte val, how_t how = DEC) { LK MbLogOut.println(val,how); }
inline void print (uint16_t val, how_t how = DEC) { LK MbLogOut.print(val,how); }
inline void println(uint16_t val, how_t how = DEC) { LK MbLogOut.println(val,how); }
inline void print (int16_t val, how_t how = DEC) { LK MbLogOut.print(val,how); }
inline void println(int16_t val, how_t how = DEC) { LK MbLogOut.println(val,how); }
inline void print (uint32_t val, how_t how = DEC) { LK MbLogOut.print(val,how); }
inline void println(uint32_t val, how_t how = DEC) { LK MbLogOut.println(val,how); }
inline void print (int32_t val, how_t how = DEC) { LK MbLogOut.print(val,how); }
inline void println(int32_t val, how_t how = DEC) { LK MbLogOut.println(val,how); }
inline void print (uint64_t val, how_t how = DEC) { LK MbLogOut.println((uint32_t)(val>>32),how); MbLogOut.println((uint32_t)(val&&0xffffffff),how); }
inline void println(uint64_t val, how_t how = DEC) { LK MbLogOut.println((uint32_t)(val>>32),how); MbLogOut.println((uint32_t)(val&&0xffffffff),how); }
inline void print (const char *msg) { LK MbLogOut.print(msg); }
inline void println(const char *msg) { LK MbLogOut.println(msg); }
inline void print (float val) { LK MbLogOut.print(val); }
inline void println(float val) { LK MbLogOut.println(val); }
inline void print (double val) { LK MbLogOut.print(val); }
inline void println(double val) { LK MbLogOut.println(val); }
inline MbLog& operator<<(const char *msg) { LK MbLogOut.print(msg); return *this; };
inline MbLog& operator<<(int16_t val) { LK MbLogOut.print(val,_how); return *this; };
#ifdef TEENSYDUINO
inline MbLog& operator<<(int val) { LK MbLogOut.print(val,_how); return *this; };
inline MbLog& operator<<(unsigned int val) { LK MbLogOut.print(val,_how); return *this; };
#endif
#ifndef ESP32
inline MbLog& operator<<(int32_t val) { LK MbLogOut.print(val,_how); return *this; };
inline MbLog& operator<<(uint32_t val) { LK MbLogOut.print(val,_how); return *this; };
#endif
inline MbLog& operator<<(byte val) {
LK
if(_how == HEX) {
sprintf(buffer, "%02x", val);
MbLogOut.print(buffer);
} else {
MbLogOut.print(val,_how);
}
return *this;
};
inline MbLog& operator<<(float val) { LK MbLogOut.print(val,_dez); return *this; };
inline MbLog& operator<<(double val) { LK MbLogOut.print(val,_dez); return *this; };
inline MbLog& operator<<(bool val) { LK MbLogOut.print(val,DEC); return *this; };
inline MbLog& operator<<(uint16_t val) {
LK
if(_how == HEX) {
sprintf(buffer, "%04x", val);
MbLogOut.print(buffer);
} else {
MbLogOut.print(val,_how);
}
return *this;
};
inline MbLog& operator<<(uint64_t val) {
LK
uint32_t upper = val>>32;
uint32_t lower = val;
sprintf(buffer, "%08lx %08lx", upper, lower);
MbLogOut.print(buffer);
return *this;
};
inline MbLog& operator<<(FORMAT val) {
LK
switch(val) {
case bin: { _how = BIN; return *this; }
case oct: { _how = OCT; return *this; }
case dec: { _how = DEC; return *this; }
case hex: { _how = HEX; return *this; }
case endl: { MbLogOut.print("\n"); return *this; }
}
return *this;
};
private:
how_t _how = DEC;
how_t _dez = 2;
char buffer[40];
#ifdef WITH_THREADS
Threads::Mutex _mutex;
#endif
};
extern MbLog LOG;