8ea7778introduced AlexaLogLevel::NONE, ERROR, INFO and DEBUG. A sketch that starts with "#define DEBUG 1", or a project built with -DDEBUG, did not compile any more: "expected identifier before numeric constant" at the enumerator, and with the build flag also in the library's own files, because ALEX2ESP_LOGD spelled AlexaLogLevel::DEBUG wherever it expanded. The same sketch builds against 1.1.0. AlexaLog.h declares the enumerators between #pragma push_macro / #undef and #pragma pop_macro of the four names: a macro of the sketch is set aside for the declaration and is back after it. ALEX2ESP_LOG takes the level as its number and casts it, so no call site names an enumerator. AlexaLog.cpp, the one file that names the levels, undefines the four names after its includes. For a sketch: nothing changes unless it defines such a macro. One that does cannot write AlexaLogLevel::DEBUG, because the preprocessor replaces the name; setLogLevel(static_cast<AlexaLogLevel>(3)) selects the same level. NONE is set aside in AlexaLog.h as well, but a project that defines NONE fails for another reason: AsyncMqttClient has an enumerator NONE (ParsingInformation.hpp). Built for d1_mini with empty credentials (PlatformIO 6.2.0, espressif8266 4.2.1), examples/basicLight.cpp, static RAM / flash in bytes,8ea7778-> this commit, no warnings: as committed 34,116 / 336,757 -> 34,116 / 336,757 "#define DEBUG 1" as the first line does not compile -> 34,116 / 336,757 DEBUG, ERROR and INFO defined in front 34,116 / 336,757 build_flags = -DDEBUG does not compile (28 errors) -> 34,116 / 336,757 build_flags = -DDEBUG -DERROR -DINFO 34,116 / 336,757 The other four examples are unchanged as well (34,232 / 340,517, 34,380 / 341,177, 34,024 / 335,457, 34,256 / 338,925). Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
55 lines
1.3 KiB
C++
55 lines
1.3 KiB
C++
#include "AlexaLog.h"
|
|
#include <stdarg.h>
|
|
|
|
// This file names the levels. A build flag such as -DDEBUG defines its macro here as well (see AlexaLog.h).
|
|
#undef NONE
|
|
#undef ERROR
|
|
#undef INFO
|
|
#undef DEBUG
|
|
|
|
AlexaLogLevel AlexaLog::level = AlexaLogLevel::INFO;
|
|
Print *AlexaLog::output = &Serial;
|
|
|
|
void AlexaLog::setLevel(AlexaLogLevel newLevel)
|
|
{
|
|
level = newLevel;
|
|
}
|
|
|
|
AlexaLogLevel AlexaLog::getLevel()
|
|
{
|
|
return level;
|
|
}
|
|
|
|
void AlexaLog::setOutput(Print *newOutput)
|
|
{
|
|
output = newOutput;
|
|
}
|
|
|
|
bool AlexaLog::enabled(AlexaLogLevel lineLevel)
|
|
{
|
|
return output != nullptr && lineLevel != AlexaLogLevel::NONE && lineLevel <= level;
|
|
}
|
|
|
|
void AlexaLog::write(AlexaLogLevel lineLevel, PGM_P format, ...)
|
|
{
|
|
if (!enabled(lineLevel))
|
|
{
|
|
return;
|
|
}
|
|
|
|
// vsnprintf and not vsnprintf_P: on the ESP8266 vsnprintf reads a PROGMEM format itself (vsnprintf_P is a jump
|
|
// to it), and vsnprintf_P comes in one object with printf_P and sprintf_P, which link the FILE-based printf:
|
|
// 5.8 KB of flash.
|
|
char line[160];
|
|
va_list arguments;
|
|
va_start(arguments, format);
|
|
int written = vsnprintf(line, sizeof(line), format, arguments);
|
|
va_end(arguments);
|
|
if (written < 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
output->print(lineLevel == AlexaLogLevel::ERROR ? F("[Alex2ESP] error: ") : F("[Alex2ESP] "));
|
|
output->println(line);
|
|
}
|