|
| 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 | +} |
0 commit comments