#include "AlexaLog.h" #include // 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); }