From aac36d9b0cf524d4983e6eab27b45be81323fe2b Mon Sep 17 00:00:00 2001 From: Max Prokhorov Date: Mon, 24 Jun 2019 00:30:16 +0300 Subject: [PATCH] Update debugSend / debugSend_P (#1788) - chain debugSend_P into basic _debugSend instead of duplicating code, pass va_list between them - check if new[] call was successful - when possible, use stack buffer and fallback to heap buffer only when it is more that 64 chars --- code/espurna/config/prototypes.h | 6 ++-- code/espurna/debug.ino | 40 ++------------------------- code/espurna/libs/DebugSend.h | 47 ++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 39 deletions(-) create mode 100644 code/espurna/libs/DebugSend.h diff --git a/code/espurna/config/prototypes.h b/code/espurna/config/prototypes.h index fa8760bc..9c7dc20e 100644 --- a/code/espurna/config/prototypes.h +++ b/code/espurna/config/prototypes.h @@ -41,8 +41,10 @@ void systemStabilityCounter(uint8_t); // ----------------------------------------------------------------------------- // Debug // ----------------------------------------------------------------------------- -void debugSend(const char * format, ...); -void debugSend_P(PGM_P format, ...); + +#include "../libs/DebugSend.h" + +void debugSendImpl(const char*); extern "C" { void custom_crash_callback(struct rst_info*, uint32_t, uint32_t); } diff --git a/code/espurna/debug.ino b/code/espurna/debug.ino index ebe7a0a0..9d361522 100644 --- a/code/espurna/debug.ino +++ b/code/espurna/debug.ino @@ -8,6 +8,8 @@ Copyright (C) 2016-2019 by Xose PĂ©rez #if DEBUG_SUPPORT +#include "libs/DebugSend.h" + #if DEBUG_UDP_SUPPORT #include WiFiUDP _udp_debug; @@ -36,7 +38,7 @@ char _udp_syslog_header[40] = {0}; } #endif -void _debugSend(const char * message) { +void debugSendImpl(const char * message) { const size_t msg_len = strlen(message); @@ -85,42 +87,6 @@ void _debugSend(const char * message) { } -// ----------------------------------------------------------------------------- - -void debugSend(const char * format, ...) { - - va_list args; - va_start(args, format); - char test[1]; - int len = ets_vsnprintf(test, 1, format, args) + 1; - char * buffer = new char[len]; - ets_vsnprintf(buffer, len, format, args); - va_end(args); - - _debugSend(buffer); - - delete[] buffer; - -} - -void debugSend_P(PGM_P format_P, ...) { - - char format[strlen_P(format_P)+1]; - memcpy_P(format, format_P, sizeof(format)); - - va_list args; - va_start(args, format_P); - char test[1]; - int len = ets_vsnprintf(test, 1, format, args) + 1; - char * buffer = new char[len]; - ets_vsnprintf(buffer, len, format, args); - va_end(args); - - _debugSend(buffer); - - delete[] buffer; - -} #if DEBUG_WEB_SUPPORT diff --git a/code/espurna/libs/DebugSend.h b/code/espurna/libs/DebugSend.h new file mode 100644 index 00000000..11cc5281 --- /dev/null +++ b/code/espurna/libs/DebugSend.h @@ -0,0 +1,47 @@ +// ----------------------------------------------------------------------------- +// printf-like debug methods +// ----------------------------------------------------------------------------- + +#pragma once + +void debugSendImpl(const char*); + +void _debugSend(const char * format, va_list args) { + + char temp[64]; + int len = ets_vsnprintf(temp, sizeof(temp), format, args); + if (len < 64) { debugSendImpl(temp); return; } + + auto buffer = new char[len + 1]; + ets_vsnprintf(buffer, len + 1, format, args); + + debugSendImpl(buffer); + + delete[] buffer; + +} + +void debugSend(const char* format, ...) { + + va_list args; + va_start(args, format); + + _debugSend(format, args); + + va_end(args); + +} + +void debugSend_P(PGM_P format_P, ...) { + + char format[strlen_P(format_P) + 1]; + memcpy_P(format, format_P, sizeof(format)); + + va_list args; + va_start(args, format_P); + + _debugSend(format, args); + + va_end(args); + +}