Skip to content

Commit

Permalink
C++ API to configure logging
Browse files Browse the repository at this point in the history
  • Loading branch information
pschatzmann committed Oct 31, 2024
1 parent 83288b3 commit ea53094
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 16 deletions.
2 changes: 2 additions & 0 deletions src/DriverCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
32 changes: 32 additions & 0 deletions src/Utils/AudioDriverLogger.h
Original file line number Diff line number Diff line change
@@ -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;
19 changes: 9 additions & 10 deletions src/Utils/Logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,53 @@
#include <stdarg.h>

// 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);
}

}

4 changes: 1 addition & 3 deletions src/Utils/Logger.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// logging methods that can be called from C and C++
#pragma once

// maximum size of log string
Expand All @@ -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;

Expand Down
10 changes: 8 additions & 2 deletions src/Utils/etc.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
3 changes: 2 additions & 1 deletion src/Utils/etc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit ea53094

Please sign in to comment.