Browse Source

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
master
Max Prokhorov 5 years ago
committed by GitHub
parent
commit
aac36d9b0c
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 39 deletions
  1. +4
    -2
      code/espurna/config/prototypes.h
  2. +3
    -37
      code/espurna/debug.ino
  3. +47
    -0
      code/espurna/libs/DebugSend.h

+ 4
- 2
code/espurna/config/prototypes.h View File

@ -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);
}


+ 3
- 37
code/espurna/debug.ino View File

@ -8,6 +8,8 @@ Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
#if DEBUG_SUPPORT
#include "libs/DebugSend.h"
#if DEBUG_UDP_SUPPORT
#include <WiFiUdp.h>
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


+ 47
- 0
code/espurna/libs/DebugSend.h View File

@ -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);
}

Loading…
Cancel
Save