IoAbstraction
IoLogging.h
Go to the documentation of this file.
1 #ifndef _IO_LOGGING_H_
2 #define _IO_LOGGING_H_
3 
10 #include "PlatformDetermination.h"
11 
12 // START user adjustable section.
13 
14 // the below definition controls logging, enable logging by either defining this build flag
15 // or uncommenting the line below.
16 //#define IO_LOGGING_DEBUG
17 
18 // These are the default levels that will be enabled when logging starts, you can add them at
19 // runtime using serEnableLevel(level, true/false)
20 #ifndef IO_LOGGING_DEFAULT_LEVEL
21 #define IO_LOGGING_DEFAULT_LEVEL (SER_WARNING|SER_ERROR|SER_IOA_INFO|SER_TCMENU_INFO|SER_NETWORK_INFO|SER_DEBUG|SER_USER_1)
22 #endif
23 
24 // END user adjustable section.
25 
31  SER_WARNING = 0x0001,
32  SER_ERROR = 0x0002,
33  SER_DEBUG = 0x0004,
34  SER_TCMENU_INFO = 0x0008,
35  SER_NETWORK_INFO = 0x0010,
36  SER_IOA_INFO = 0x0020,
37  SER_USER_1 = 0x0040,
38  SER_USER_2 = 0x0080,
39  SER_USER_3 = 0x0100,
40  SER_USER_4 = 0x0200,
41  SER_USER_5 = 0x0400,
42  SER_USER_6 = 0x0800,
43  SER_TCMENU_DEBUG = 0x1000,
44  SER_NETWORK_DEBUG = 0x2000,
45  SER_IOA_DEBUG = 0x4000,
46  SER_LOG_EVERYTHING = 0xffff
47 };
48 
49 #ifdef IO_LOGGING_DEBUG
50 
52 extern unsigned int enabledLevels;
53 
54 #ifdef IOA_USE_MBED
55 
56 #include "PrintCompat.h"
57 #include <FileHandle.h>
58 //
59 // On mbed you create an instance of this class called LoggingPort in your main class.
60 // see the mbed example.
61 //
62 class MBedLogger : public Print {
63 private:
64  FileHandle& serial;
65 public:
66  explicit MBedLogger(FileHandle& serialName) : serial(serialName) {}
67 
68  size_t write(uint8_t ch) override {
69  serial.write(&ch, 1);
70  return 1;
71  }
72 
73  size_t write(const char* sz) override {
74  auto len = strlen(sz);
75  serial.write(sz, len);
76  return len;
77  }
78 };
79 extern MBedLogger LoggingPort;
80 // a couple of definitions here to avoid including headers, F() macro not needed on mbed
81 unsigned long millis();
82 #define F(x) x
83 #define IOLOG_MBED_PORT_IF_NEEDED(tx, rx) BufferedSerial serPort(tx, rx);MBedLogger LoggingPort(serPort);
84 #define IOLOG_START_SERIAL serPort.set_baud(115200);
85 #elif defined(BUILD_FOR_PICO_CMAKE)
86 #include "PrintCompat.h"
87 #include <cstring>
88 #include <pico/time.h>
89 #ifdef BUILD_PICO_FORCE_UART
90 class PrintfLogger : public Print {
91 private:
92 public:
93  size_t write(uint8_t ch) override {
94  uart_putc_raw(uart0, ch);
95  return 1;
96  }
97 
98  size_t write(const char* sz) override {
99  auto len = strlen(sz);
100  for(int i=0;i<len;i++) uart_putc_raw(uart0, sz[i]);
101  return len;
102  }
103 };
104 #define IOLOG_START_SERIAL uart_init(uart_get_instance(0), 115200);gpio_set_function(1, GPIO_FUNC_UART);gpio_set_function(0, GPIO_FUNC_UART);
105 #else
106 class PrintfLogger : public Print {
107 public:
108  size_t write(uint8_t ch) override {
109  putchar_raw(ch);
110  return 1;
111  }
112 
113  size_t write(const char* sz) override {
114  auto len = strlen(sz);
115  for(int i=0;i<len;i++) putchar_raw(sz[i]);
116  return len;
117  }
118 };
119 #define IOLOG_START_SERIAL stdio_init_all();
120 #endif
121 extern PrintfLogger LoggingPort;
122 unsigned long millis(); // available from task manager
123 #define F(x) x
124 #define IOLOG_MBED_PORT_IF_NEEDED(tx, rx)
125 #else
126 // Arduino:
127 // You can change the logging serial port by defining LoggingPort to your chosen serial port.
128 #ifndef LoggingPort
129 #define LoggingPort Serial
130 #endif
131 #define IOLOG_START_SERIAL LoggingPort.begin(115200);
132 #define IOLOG_MBED_PORT_IF_NEEDED(tx, rx)
133 #endif
134 
135 
136 const char* prettyLevel(SerLoggingLevel level);
137 #define logTimeAndLevel(title, lvl) LoggingPort.print(millis());LoggingPort.print('-');LoggingPort.print(prettyLevel(lvl));LoggingPort.print(':');LoggingPort.print(title)
138 
144 inline bool serLevelEnabled(SerLoggingLevel level) { return (enabledLevels & level) != 0; }
145 
151 inline void serEnableLevel(SerLoggingLevel level, bool active) {
152  if(active) {
153  enabledLevels |= level;
154  } else {
155  enabledLevels ^= level;
156  }
157 }
158 
159 #define serlogF(lvl, x) if(serLevelEnabled(lvl)) { logTimeAndLevel(F(x), lvl); LoggingPort.println(); }
160 #define serlogF2(lvl, x1, x2) if(serLevelEnabled(lvl)) { logTimeAndLevel(F(x1), lvl); LoggingPort.print(x2);LoggingPort.println(); }
161 #define serlogF3(lvl, x1, x2, x3) if(serLevelEnabled(lvl)) { logTimeAndLevel(F(x1), lvl); LoggingPort.print(x2); LoggingPort.print(' '); LoggingPort.print(x3);LoggingPort.println(); }
162 #define serlogF4(lvl, x1, x2, x3, x4) if(serLevelEnabled(lvl)) { logTimeAndLevel(F(x1), lvl); LoggingPort.print(x2); LoggingPort.print(' '); LoggingPort.print(x3); LoggingPort.print(' '); LoggingPort.print(x4);LoggingPort.println(); }
163 #define serlogFHex(lvl, x1, x2) if(serLevelEnabled(lvl)) { logTimeAndLevel(F(x1), lvl); LoggingPort.print(x2, HEX);LoggingPort.println(); }
164 #define serlogFHex2(lvl, x1, x2, x3) if(serLevelEnabled(lvl)) { logTimeAndLevel(F(x1), lvl); LoggingPort.print(x2, HEX); LoggingPort.print(','); LoggingPort.print(x3, HEX);LoggingPort.println(); }
165 #define serlog(lvl, x) if(serLevelEnabled(lvl)) { logTimeAndLevel(x, lvl);LoggingPort.println(); }
166 #define serlog2(lvl, x1, x2) if(serLevelEnabled(lvl)) { logTimeAndLevel(x1, lvl); LoggingPort.print(x2);LoggingPort.println(); }
167 #define serlog3(lvl, x1, x2, x3) if(serLevelEnabled(lvl)) { logTimeAndLevel(x1, lvl); LoggingPort.print(x2); LoggingPort.print(' '); LoggingPort.print(x3);LoggingPort.println(); }
168 #define serlogHex(lvl, x1, x2) if(serLevelEnabled(lvl)) { logTimeAndLevel(x1, lvl); LoggingPort.print(x2, HEX);LoggingPort.println(); }
169 
170 void serlogHexDump(SerLoggingLevel level, const char *title, const void* data, size_t strlen);
171 inline void serdebugHexDump(const char *title, const void* data, size_t len) { serlogHexDump(SER_DEBUG, title, data, len);}
172 
173 #define serdebugF(x) serlogF(SER_DEBUG, x)
174 #define serdebugF2(x1, x2) serlogF2(SER_DEBUG, x1, x2);
175 #define serdebugF3(x1, x2, x3) serlogF3(SER_DEBUG, x1, x2, x3)
176 #define serdebugF4(x1, x2, x3, x4) serlogF4(SER_DEBUG, x1, x2, x3, x4)
177 #define serdebugFHex(x1, x2) serlogFHex(SER_DEBUG, x1, x2)
178 #define serdebugFHex2(x1, x2, x3) serlogFHex2(SER_DEBUG, x1, x2, x3)
179 #define serdebug(x) serlog(SER_DEBUG, x)
180 #define serdebug2(x1, x2) serlog2(SER_DEBUG, x1, x2)
181 #define serdebug3(x1, x2, x3) serlog3(SER_DEBUG, x1, x2, x3)
182 #define serdebugHex(x1, x2) serlogHex(SER_DEBUG, x1, x2)
183 
184 void startTaskManagerLogDelegate();
185 
186 #else
187 // all loging to no operations (commenting out the above define of IO_LOGGING_DEBUG to remove in production builds).
188 #define serdebugF(x)
189 #define serdebugF2(x, y)
190 #define serdebugF3(x, y, z)
191 #define serdebugF4(a, b, c, d)
192 #define serdebugFHex(x, y)
193 #define serdebugFHex2(x, y, z)
194 #define serdebug(x)
195 #define serdebug2(x, y)
196 #define serdebug3(x, y, z)
197 #define serdebugHex(x, y)
198 #define serdebugHexDump(x, str, strlen)
199 #define serlogHexDump(l, x, str, len)
200 #define serlogF(lvl, x)
201 #define serlogF2(lvl, x1, x2)
202 #define serlogF3(lvl, x1, x2, x3)
203 #define serlogF4(lvl, x1, x2, x3, x4)
204 #define serlogFHex(lvl, x1, x2)
205 #define serlogFHex2(lvl, x1, x2, x3)
206 #define serlog(lvl, x)
207 #define serlog2(lvl, x1, x2)
208 #define serlog3(lvl, x1, x2, x3)
209 #define serlogHex(lvl, x1, x2)
210 
211 #define startTaskManagerLogDelegate()
212 
213 #define IOLOG_START_SERIAL
214 #define IOLOG_MBED_PORT_IF_NEEDED(tx, rx)
215 
216 #define serEnableLevel(l, a)
217 #define serLevelEnabled(l) false
218 #endif // IO_LOGGING_DEBUG
219 
220 #endif // _IO_LOGGING_H_
unsigned int enabledLevels
Definition: IoLogging.cpp:14
SerLoggingLevel
Definition: IoLogging.h:30
void serEnableLevel(SerLoggingLevel level, bool active)
Definition: IoLogging.h:151
bool serLevelEnabled(SerLoggingLevel level)
Definition: IoLogging.h:144
Compatibility with the Arduino Print API for mbed boards, should never be included in an Arduino buil...
Definition: IoLogging.h:62
size_t write(uint8_t ch) override
Definition: IoLogging.h:68
size_t write(const char *sz) override
Definition: IoLogging.h:73
Definition: PrintCompat.h:31
Definition: IoLogging.h:90
size_t write(uint8_t ch) override
Definition: IoLogging.h:93
size_t write(const char *sz) override
Definition: IoLogging.h:98