Skip to content

Commit

Permalink
Add Log Level to printed messages
Browse files Browse the repository at this point in the history
  • Loading branch information
pschatzmann committed May 9, 2021
1 parent 95fab1b commit b450bcf
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
9 changes: 4 additions & 5 deletions src/AudioConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -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(...)
Expand Down
41 changes: 33 additions & 8 deletions src/AudioTools/AudioLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

};

}

0 comments on commit b450bcf

Please sign in to comment.