diff --git a/src/DriverCommon.h b/src/DriverCommon.h index 10b0ea6..71b3bb9 100644 --- a/src/DriverCommon.h +++ b/src/DriverCommon.h @@ -19,6 +19,8 @@ #define I2C_END true // wether to send a stop bit at the end of the transmission #ifdef __cplusplus +#include "Utils/AudioDriverLogger.h" + namespace audio_driver { #endif diff --git a/src/Utils/AudioDriverLogger.h b/src/Utils/AudioDriverLogger.h new file mode 100644 index 0000000..9485c27 --- /dev/null +++ b/src/Utils/AudioDriverLogger.h @@ -0,0 +1,32 @@ + +#pragma once + +#include "etc.h" +#include "Logger.h" +#include "Print.h" + +// define supported log levels +enum class AudioDriverLogLevel {Debug=0, Info, Warning, Error}; + +/*** + * C++ API to configure logging + */ +class AudioDriverLoggerClass { +public: + void setLogLevel(AudioDriverLogLevel level){ + LOGLEVEL_AUDIODRIVER = (int) level; + } + void setOutput(Print& out) { + setAudioDriverLogOutput(&out); + } + bool begin(Print& out, AudioDriverLogLevel level) { + setLogLevel(level); + setOutput(out); + return true; + } + void end(){ + setAudioDriverLogOutput(nullptr); + } +}; + +static AudioDriverLoggerClass AudioDriverLogger; \ No newline at end of file diff --git a/src/Utils/Logger.c b/src/Utils/Logger.c index 7045884..6ab5af2 100644 --- a/src/Utils/Logger.c +++ b/src/Utils/Logger.c @@ -5,54 +5,53 @@ #include // Default log level is warning -int LOGLEVEL_AUDIODRIVER = AudioDriverWarning; +int LOGLEVEL_AUDIODRIVER = 2; void AD_LOGD(const char* fmr, ...) { - if (LOGLEVEL_AUDIODRIVER <= AudioDriverDebug) { // LOGLEVEL_AUDIODRIVER = Debug + if (LOGLEVEL_AUDIODRIVER <= 0) { // LOGLEVEL_AUDIODRIVER = Debug char log_buffer[AD_LOGLENGTH+1]; strcpy(log_buffer, "Debug: "); va_list arg; va_start(arg, fmr); vsnprintf(log_buffer+9, AD_LOGLENGTH-9, fmr, arg); va_end(arg); - logStr(log_buffer); + audioDriverLogStr(log_buffer); } } void AD_LOGI(const char* fmr, ...) { - if (LOGLEVEL_AUDIODRIVER <= AudioDriverInfo) { // LOGLEVEL_AUDIODRIVER = Info, Debug + if (LOGLEVEL_AUDIODRIVER <= 1) { // LOGLEVEL_AUDIODRIVER = Info, Debug char log_buffer[AD_LOGLENGTH+1]; strcpy(log_buffer, "Info: "); va_list arg; va_start(arg, fmr); vsnprintf(log_buffer+9,AD_LOGLENGTH-9, fmr, arg); va_end(arg); - logStr(log_buffer); + audioDriverLogStr(log_buffer); } } void AD_LOGW(const char* fmr, ...) { - if (LOGLEVEL_AUDIODRIVER <= AudioDriverWarning) { // LOGLEVEL_AUDIODRIVER = Warning, Info, Debug + if (LOGLEVEL_AUDIODRIVER <= 2) { // LOGLEVEL_AUDIODRIVER = Warning, Info, Debug char log_buffer[AD_LOGLENGTH+1]; strcpy(log_buffer, "Warning: "); va_list arg; va_start(arg, fmr); vsnprintf(log_buffer+9,AD_LOGLENGTH-9, fmr, arg); va_end(arg); - logStr(log_buffer); + audioDriverLogStr(log_buffer); } } void AD_LOGE(const char* fmr, ...) { - if (LOGLEVEL_AUDIODRIVER <= AudioDriverError) { // for all levels + if (LOGLEVEL_AUDIODRIVER <= 3) { // for all levels char log_buffer[AD_LOGLENGTH+1]; strcpy(log_buffer, "Error: "); va_list arg; va_start(arg, fmr); vsnprintf(log_buffer+9,AD_LOGLENGTH-9, fmr, arg); va_end(arg); - logStr(log_buffer); + audioDriverLogStr(log_buffer); } - } diff --git a/src/Utils/Logger.h b/src/Utils/Logger.h index 9d5ea23..0b1dfa8 100644 --- a/src/Utils/Logger.h +++ b/src/Utils/Logger.h @@ -1,3 +1,4 @@ +// logging methods that can be called from C and C++ #pragma once // maximum size of log string @@ -10,9 +11,6 @@ extern "C" { #endif -// define supported log levels -enum AudioDriverLogLevels {AudioDriverDebug, AudioDriverInfo, AudioDriverWarning, AudioDriverError}; - // define default log level extern int LOGLEVEL_AUDIODRIVER; diff --git a/src/Utils/etc.cpp b/src/Utils/etc.cpp index a6ff8eb..9594cc2 100644 --- a/src/Utils/etc.cpp +++ b/src/Utils/etc.cpp @@ -1,7 +1,13 @@ #include "Arduino.h" #include "Utils/etc.h" +Print *p_audio_driver_log_output = &Serial; + /// Arcuino c++ println function -void logStr(const char* msg){ - Serial.println(msg); +void audioDriverLogStr(const char* msg){ + if (p_audio_driver_log_output) p_audio_driver_log_output->println(msg); +} + +void setAudioDriverLogOutput(void *out){ + p_audio_driver_log_output = (Print*) out; } diff --git a/src/Utils/etc.h b/src/Utils/etc.h index 0559a35..eaebeb5 100644 --- a/src/Utils/etc.h +++ b/src/Utils/etc.h @@ -24,7 +24,8 @@ #ifdef __cplusplus extern "C" { #endif -void logStr(const char* msg); +void audioDriverLogStr(const char* msg); +void setAudioDriverLogOutput(void *out); #ifndef ARDUINO void pinMode(int, int); void digitalWrite(int, int);