Skip to content

Commit 1ecc7f7

Browse files
committedMay 28, 2019
Initial commit of ArduinoDebugUtils
1 parent ca37ca0 commit 1ecc7f7

File tree

2 files changed

+166
-0
lines changed

2 files changed

+166
-0
lines changed
 

‎src/ArduinoDebugUtils.cpp

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
This file is part of ArduinoDebugUtils.
3+
4+
Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
5+
6+
This software is released under the GNU General Public License version 3,
7+
which covers the main part of arduino-cli.
8+
The terms of this license can be found at:
9+
https://www.gnu.org/licenses/gpl-3.0.en.html
10+
11+
You can be released from the requirements of the above licenses by purchasing
12+
a commercial license. Buying such a license is mandatory if you want to modify or
13+
otherwise use the software for commercial activities involving the Arduino
14+
software without disclosing the source code of your own applications. To purchase
15+
a commercial license, send an email to license@arduino.cc.
16+
*/
17+
18+
/******************************************************************************
19+
INCLUDE
20+
******************************************************************************/
21+
22+
#include "ArduinoDebugUtils.h"
23+
24+
/******************************************************************************
25+
CTOR/DTOR
26+
******************************************************************************/
27+
28+
ArduinoDebugUtils::ArduinoDebugUtils()
29+
{
30+
timestampOff();
31+
setDebugLevel(DEFAULT_DEBUG_LEVEL);
32+
setDebugOutputStream(DEFAULT_OUTPUT_STREAM);
33+
}
34+
35+
/******************************************************************************
36+
PUBLIC MEMBER FUNCTIONS
37+
******************************************************************************/
38+
39+
void ArduinoDebugUtils::setDebugLevel(DebugLevel const debug_level)
40+
{
41+
_debug_level = debug_level;
42+
}
43+
44+
void ArduinoDebugUtils::setDebugOutputStream(Stream * stream)
45+
{
46+
_debug_output_stream = stream;
47+
}
48+
49+
void ArduinoDebugUtils::timestampOn()
50+
{
51+
_timestamp_on = true;
52+
}
53+
54+
void ArduinoDebugUtils::timestampOff()
55+
{
56+
_timestamp_on = false;
57+
}
58+
59+
void ArduinoDebugUtils::debugPrint(DebugLevel const debug_level, const char * fmt, ...)
60+
{
61+
if (debug_level >= DebugLevel::Error &&
62+
debug_level <= DebugLevel::Verbose &&
63+
debug_level <= _debug_level)
64+
{
65+
if(_timestamp_on)
66+
{
67+
char timestamp[20];
68+
snprintf(timestamp, 20, "[ %lu ] ", millis());
69+
debug_output_stream->print(timestamp);
70+
}
71+
72+
va_list args;
73+
va_start(args, fmt);
74+
vDebugPrint(fmt, args);
75+
va_end(args);
76+
}
77+
}
78+
79+
/******************************************************************************
80+
PRIVATE MEMBER FUNCTIONS
81+
******************************************************************************/
82+
83+
void ArduinoDebugUtils::vDebugPrint(char const * fmt, va_list args)
84+
{
85+
static size_t const MSG_BUF_SIZE = 120;
86+
char msg_buf[MSG_BUF_SIZE] = {0};
87+
88+
vsnprintf(msg_buf, MSG_BUF_SIZE, fmt, args);
89+
90+
_debug_output_stream->println(msg_buf);
91+
}

‎src/ArduinoDebugUtils.h

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
This file is part of ArduinoDebugUtils.
3+
4+
Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
5+
6+
This software is released under the GNU General Public License version 3,
7+
which covers the main part of arduino-cli.
8+
The terms of this license can be found at:
9+
https://www.gnu.org/licenses/gpl-3.0.en.html
10+
11+
You can be released from the requirements of the above licenses by purchasing
12+
a commercial license. Buying such a license is mandatory if you want to modify or
13+
otherwise use the software for commercial activities involving the Arduino
14+
software without disclosing the source code of your own applications. To purchase
15+
a commercial license, send an email to license@arduino.cc.
16+
*/
17+
18+
#ifndef ARDUINO_DEBUG_UTILS_H_
19+
#define ARDUINO_DEBUG_UTILS_H_
20+
21+
/******************************************************************************
22+
INCLUDE
23+
******************************************************************************/
24+
25+
#include <Arduino.h>
26+
27+
/******************************************************************************
28+
TYPEDEF
29+
******************************************************************************/
30+
31+
enum class DebugLevel : int {
32+
None = -1,
33+
Error = 0,
34+
Warning = 1,
35+
Info = 2,
36+
Debug = 3,
37+
Verbose = 4
38+
};
39+
40+
/******************************************************************************
41+
CLASS DECLARATION
42+
******************************************************************************/
43+
44+
class ArduinoDebugUtils
45+
{
46+
47+
public:
48+
49+
ArduinoDebugUtils();
50+
51+
void setDebugLevel(DebugLevel const debug_level);
52+
void setDebugOutputStream(Stream * stream);
53+
54+
void timestampOn();
55+
void timestampOff();
56+
57+
void debugPrint(DebugLevel const debug_level, const char * fmt, ...);
58+
59+
60+
private:
61+
62+
bool _timestamp_on;
63+
DebugLevel _debug_level;
64+
Stream * _debug_output_stream = &Serial;
65+
66+
67+
static DebugLevel const DEFAULT_DEBUG_LEVEL = DebugLevel::Info;
68+
static Stream * DEFAULT_OUTPUT_STREAM = &Serial;
69+
70+
71+
void vDebugPrint(char const * fmt, va_list args);
72+
73+
};
74+
75+
#endif /* ARDUINO_DEBUG_UTILS_H_ */

0 commit comments

Comments
 (0)
Please sign in to comment.