diff --git a/src/AudioConfig.h b/src/AudioConfig.h index 915cbcc333..cff17b6ede 100644 --- a/src/AudioConfig.h +++ b/src/AudioConfig.h @@ -76,12 +76,11 @@ #define LOG_STREAM Serial // Logging Implementation -#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) #ifdef USE_LOGGING -#define LOGD(...) AudioLogger::instance().printLog(__FILENAME__,__LINE__, AudioLogger::Debug, __VA_ARGS__) -#define LOGI(...) AudioLogger::instance().printLog(__FILENAME__,__LINE__, AudioLogger::Info, __VA_ARGS__) -#define LOGW(...) AudioLogger::instance().printLog(__FILENAME__,__LINE__, AudioLogger::Warning, __VA_ARGS__) -#define LOGE(...) AudioLogger::instance().printLog(__FILENAME__,__LINE__, AudioLogger::Error, __VA_ARGS__) +#define LOGD(...) AudioLogger::instance().printLog(__FILE__,__LINE__, AudioLogger::Debug, __VA_ARGS__) +#define LOGI(...) AudioLogger::instance().printLog(__FILE__,__LINE__, AudioLogger::Info, __VA_ARGS__) +#define LOGW(...) AudioLogger::instance().printLog(__FILE__,__LINE__, AudioLogger::Warning, __VA_ARGS__) +#define LOGE(...) AudioLogger::instance().printLog(__FILE__,__LINE__, AudioLogger::Error, __VA_ARGS__) #else #define LOGD(...) #define LOGI(...) diff --git a/src/AudioTools/AudioLogger.h b/src/AudioTools/AudioLogger.h index 572fad3050..bbebce6974 100644 --- a/src/AudioTools/AudioLogger.h +++ b/src/AudioTools/AudioLogger.h @@ -86,21 +86,19 @@ class AudioLogger { int printLog(const char* file, int line, LogLevel current_level, const char* fmt, ...) const { char serial_printf_buffer[PRINTF_BUFFER_SIZE] = {0}; int len = 0; - va_list args; if (this->active && log_stream_ptr!=nullptr && current_level >= log_level){ - - len+=log_stream_ptr->print(file); - len+=log_stream_ptr->print(" "); - len+=log_stream_ptr->print(line); - len+=log_stream_ptr->print(": "); - + // content char serial_printf_buffer[PRINTF_BUFFER_SIZE] = {0}; + // prefix + len += printPrefix(file, line, current_level); + + va_list args; va_start(args,fmt); len += vsnprintf(serial_printf_buffer,PRINTF_BUFFER_SIZE, fmt, args); log_stream_ptr->print(serial_printf_buffer); va_end(args); - + // newline len += log_stream_ptr->println(); } return len; @@ -125,6 +123,33 @@ class AudioLogger { AudioLogger(){ } + const char* levelName(LogLevel level) const { + switch(level){ + case Debug: + return "D"; + case Info: + return "I"; + case Warning: + return "W"; + case Error: + return "E"; + } + return ""; + } + + int printPrefix(const char* file, int line, LogLevel current_level) const { + const char* file_name = strrchr(file, '/') ? strrchr(file, '/') + 1 : file; + const char* level_code = levelName(current_level); + int len = log_stream_ptr->print("["); + len += log_stream_ptr->print(level_code); + len += log_stream_ptr->print("] "); + len += log_stream_ptr->print(file_name); + len += log_stream_ptr->print(" : "); + len += log_stream_ptr->print(line); + len += log_stream_ptr->print(" - "); + return len; + } + }; }