EEPROM-Storage Library
The EEPROM Storage library provides the ability to access variables stored in EEPROM just as if they were stored in RAM.
 
Loading...
Searching...
No Matches
EEPROM-Debug.cpp
Go to the documentation of this file.
1/*
2 This file is part of EEPROMDebug. Modified by
3 Daniel M Porrey to work on the Particle platform.
4
5 Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
6
7 This software is released under the GNU General Public License version 3,
8 which covers the main part of arduino-cli.
9 The terms of this license can be found at:
10 https://www.gnu.org/licenses/gpl-3.0.en.html
11
12 You can be released from the requirements of the above licenses by purchasing
13 a commercial license. Buying such a license is mandatory if you want to modify or
14 otherwise use the software for commercial activities involving the Arduino
15 software without disclosing the source code of your own applications. To purchase
16 a commercial license, send an email to license@arduino.cc.
17*/
18
19/******************************************************************************
20 INCLUDE
21 ******************************************************************************/
22
23#include "EEPROM-Debug.h"
24
25/******************************************************************************
26 CONSTANTS
27 ******************************************************************************/
28
29static int const DEFAULT_DEBUG_LEVEL = DBG_INFO;
30static Stream * DEFAULT_OUTPUT_STREAM = &Serial;
31
32/******************************************************************************
33 CTOR/DTOR
34 ******************************************************************************/
35
38 newlineOn();
41 setDebugLevel(DEFAULT_DEBUG_LEVEL);
42 setDebugOutputStream(DEFAULT_OUTPUT_STREAM);
43}
44
45/******************************************************************************
46 PUBLIC MEMBER FUNCTIONS
47 ******************************************************************************/
48
49void EEPROMDebug::setDebugLevel(int const debug_level) {
50 _debug_level = debug_level;
51}
52
54 return _debug_level;
55}
56
57void EEPROMDebug::setDebugOutputStream(Stream * stream) {
58 _debug_output_stream = stream;
59}
60
62 _newline_on = true;
63}
64
66 _newline_on = false;
67}
68
70 _print_debug_label = true;
71}
72
74 _print_debug_label = false;
75}
76
78 _format_timestamp_on = true;
79}
80
82 _format_timestamp_on = false;
83}
84
86 _timestamp_on = true;
87}
88
90 _timestamp_on = false;
91}
92
93void EEPROMDebug::print(int const debug_level, const char * fmt, ...)
94{
95 if (!shouldPrint(debug_level))
96 return;
97
98 if (_print_debug_label)
99 printDebugLabel(debug_level);
100
101 if (_timestamp_on)
102 printTimestamp();
103
104 va_list args;
105 va_start(args, fmt);
106 vPrint(fmt, args);
107 va_end(args);
108}
109
110void EEPROMDebug::print(int const debug_level, const __FlashStringHelper * fmt, ...)
111{
112 if (!shouldPrint(debug_level))
113 return;
114
115 if (_print_debug_label)
116 printDebugLabel(debug_level);
117
118 if (_timestamp_on)
119 printTimestamp();
120
121 String fmt_str(fmt);
122
123 va_list args;
124 va_start(args, fmt);
125 vPrint(fmt_str.c_str(), args);
126 va_end(args);
127}
128
129/******************************************************************************
130 PRIVATE MEMBER FUNCTIONS
131 ******************************************************************************/
132
133void EEPROMDebug::vPrint(char const * fmt, va_list args) {
134 // calculate required buffer length
135 int msg_buf_size = vsnprintf(nullptr, 0, fmt, args) + 1; // add one for null terminator
136#if defined(ARDUINO) && ARDUINO >= 100
137 #if __STDC_NO_VLA__ == 1
138 // in the rare case where VLA is not allowed by compiler, fall back on heap-allocated memory
139 char * msg_buf = new char[msg_buf_size];
140 #else
141 char msg_buf[msg_buf_size];
142 #endif
143#elif defined(PARTICLE)
144 char msg_buf[msg_buf_size];
145#endif
146
147 vsnprintf(msg_buf, msg_buf_size, fmt, args);
148
149 if (_newline_on) {
150 _debug_output_stream->println(msg_buf);
151 } else {
152 _debug_output_stream->print(msg_buf);
153 }
154
155#if defined(ARDUINO) && ARDUINO >= 100
156 #if __STDC_NO_VLA__ == 1
157 // remember to clean up memory
158 delete[] msg_buf;
159 #endif
160#endif
161}
162
163void EEPROMDebug::printTimestamp()
164{
165 char timestamp[32];
166
167 if (_format_timestamp_on)
168 {
169 auto const msCount = millis();
170
171 uint16_t const milliseconds = msCount % 1000; // ms remaining when converted to seconds
172 uint16_t const allSeconds = msCount / 1000; // total number of seconds to calculate remaining values
173
174 uint16_t const hours = allSeconds / 3600; // convert seconds to hours
175 uint16_t const secondsRemaining = allSeconds % 3600; // seconds left over
176
177 uint16_t const minutes = secondsRemaining / 60 ; // convert seconds left over to minutes
178 uint16_t const seconds = secondsRemaining % 60; // seconds left over
179
180 snprintf(timestamp, sizeof(timestamp), // "prints" formatted output to a char array (string)
181 "[ "
182 "%02d:" //HH:
183 "%02d:" //MM:
184 "%02d." //SS.
185 "%03d" //MMM
186 " ] ",
187 hours,
188 minutes,
189 seconds,
190 milliseconds
191 );
192 }
193 else
194 {
195 snprintf(timestamp, sizeof(timestamp), "[ %lu ] ", millis());
196 }
197
198 _debug_output_stream->print(timestamp);
199}
200
201void EEPROMDebug::printDebugLabel(int const debug_level)
202{
203 static char const * DEBUG_MODE_STRING[5] =
204 {
205 "[DBG_ERROR ] ",
206 "[DBG_WARNING] ",
207 "[DBG_INFO ] ",
208 "[DBG_DEBUG ] ",
209 "[DBG_VERBOSE] ",
210 };
211
212 bool is_valid_debug_level = (debug_level >= DBG_ERROR) && (debug_level <= DBG_VERBOSE);
213 if (!is_valid_debug_level)
214 return;
215
216 _debug_output_stream->print(DEBUG_MODE_STRING[debug_level]);
217}
218
219bool EEPROMDebug::shouldPrint(int const debug_level) const
220{
221 return ((debug_level >= DBG_ERROR) && (debug_level <= DBG_VERBOSE) && (debug_level <= _debug_level));
222}
223
224/******************************************************************************
225 CLASS INSTANTIATION
226 ******************************************************************************/
227
229
230void setDebugMessageLevel(int const debug_level) {
231 Debug.setDebugLevel(debug_level);
232}
233
235 return Debug.getDebugLevel();
236}
EEPROMDebug Debug
int getDebugMessageLevel()
void setDebugMessageLevel(int const debug_level)
void formatTimestampOn()
void formatTimestampOff()
void setDebugLevel(int const debug_level)
void setDebugOutputStream(Stream *stream)
void print(int const debug_level, const char *fmt,...)
void debugLabelOn()
void newlineOff()
void debugLabelOff()
void timestampOn()
void timestampOff()
int getDebugLevel() const