debug_progmem.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * debug_progmem.h
8  *
9  * Contains debug functions that facilitate using strings stored in flash(irom).
10  * This frees up RAM of all const char* debug strings
11  *
12  * Created on: 27.01.2017
13  * Author: (github.com/)ADiea
14  *
15  ****/
16 
17 #pragma once
18 
19 #include "FakePgmSpace.h"
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 //This enables or disables logging
26 //Can be overridden in Makefile
27 #ifndef DEBUG_BUILD
28  #ifdef SMING_RELEASE
29  #define DEBUG_BUILD 0
30  #else
31  #define DEBUG_BUILD 1
32  #endif
33 #endif
34 
35 //This enables or disables file and number printing for each log line
36 //Can be overridden in Makefile
37 #ifndef DEBUG_PRINT_FILENAME_AND_LINE
38 #define DEBUG_PRINT_FILENAME_AND_LINE 0
39 #endif
40 
41 #define ERR 0
42 #define WARN 1
43 #define INFO 2
44 #define DBG 3
45 
46 //This sets debug verbose level
47 //Define in Makefile, default is INFO
48 #ifndef DEBUG_VERBOSE_LEVEL
49 #define DEBUG_VERBOSE_LEVEL INFO
50 #endif
51 
52 // Dummy to omit debug information safely
53 #define debug_none(fmt, ...) \
54  do { \
55  } while(0)
56 
57 extern uint32_t system_get_time();
58 
59 #if DEBUG_BUILD
60 
61 // http://stackoverflow.com/a/35441900
62 #define MACROCAT2(x,y,z) x##y##z
63 #define MACROCONCAT(x,y,z) MACROCAT2(x,y,z)
64 #define MACROQUOT(x) #x
65 #define MACROQUOTE(x) MACROQUOT(x)
66 
67 //A static const char[] is defined having a unique name (log_ prefix, filename and line number)
68 //This will be stored in the irom section(on flash) freeing up the RAM
69 //Next special version of printf from FakePgmSpace is called to fetch and print the message
70 #if DEBUG_PRINT_FILENAME_AND_LINE
71 #define debug_e(fmt, ...) \
72  (__extension__({ \
73  PSTR_ARRAY(fmtbuf, "[" MACROQUOTE(CUST_FILE_BASE) ":%d] " fmt "\r\n"); \
74  m_printf(fmtbuf, __LINE__, ##__VA_ARGS__); \
75  }))
76 #else
77 #define debug_e(fmt, ...) \
78  (__extension__({ \
79  PSTR_ARRAY(fmtbuf, "%u " fmt "\r\n"); \
80  m_printf(fmtbuf, system_get_time(), ##__VA_ARGS__); \
81  }))
82 #endif
83 
84 /*
85  * Print a block of data but only at or above given debug level
86  */
87 #define debug_hex(_level, _tag, _data, _len, ...) \
88  { \
89  if(DEBUG_VERBOSE_LEVEL >= _level) \
90  m_printHex(_F(_tag), _data, _len, ##__VA_ARGS__); \
91  }
92 
93  #if DEBUG_VERBOSE_LEVEL == DBG
94  #define debug_w debug_e
95  #define debug_i debug_e
96  #define debug_d debug_e
97  #elif DEBUG_VERBOSE_LEVEL == INFO
98  #define debug_w debug_e
99  #define debug_i debug_e
100  #define debug_d debug_none
101  #elif DEBUG_VERBOSE_LEVEL == WARN
102  #define debug_w debug_e
103  #define debug_i debug_none
104  #define debug_d debug_none
105  #else
106  #define debug_w debug_none
107  #define debug_i debug_none
108  #define debug_d debug_none
109  #endif
110 
111 #else /*DEBUG_BUILD*/
112 
113 #define debug_e debug_none
114 #define debug_w debug_none
115 #define debug_i debug_none
116 #define debug_d debug_none
117 
118 #define debug_hex(_level, _tag, _data, _len, ...) \
119  do { \
120  } while(0)
121 
122 #endif /*DEBUG_BUILD*/
123 
124 #ifdef SMING_RELEASE
125 #define debugf(fmt, ...)
126 #else
127 #define debugf debug_i
128 #endif
129 
130 #ifdef __cplusplus
131 }
132 #endif
uint32_t system_get_time()