Skip to content

Commit 7b6820b

Browse files
committed
Cleanup Logging
1 parent 3de88bc commit 7b6820b

File tree

10 files changed

+120
-128
lines changed

10 files changed

+120
-128
lines changed

src/AudioA2DP.h

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
namespace audio_tools {
2020

21-
AudioLogger &log_a2dp = AudioLogger::instance();
22-
2321
/**
2422
* @brief Covnerts the data from T src[][2] to a Channels array
2523
* @author Phil Schatzmann
@@ -62,7 +60,7 @@ int32_t a2dp_stream_source_sound_data(Channels* data, int32_t len) {
6260
size_t result_len_bytes = a2dp_buffer.readArray((uint8_t*)data, len*sizeof(Channels));
6361
// result is in number of frames
6462
result_len = result_len_bytes / sizeof(Channels);
65-
ESP_LOGI(AUDIO_TAG, "a2dp_stream_source_sound_data %d -> %d", len ,result_len);
63+
ESP_LOGI( "a2dp_stream_source_sound_data %d -> %d", len ,result_len);
6664
// allow some other task
6765
yield();
6866
return result_len;
@@ -72,7 +70,7 @@ int32_t a2dp_stream_source_sound_data(Channels* data, int32_t len) {
7270
void a2dp_stream_sink_sound_data(const uint8_t* data, uint32_t len) {
7371
if (is_a2dp_setup){
7472
uint32_t result_len = a2dp_buffer.writeArray(data, len);
75-
ESP_LOGI(AUDIO_TAG, "a2dp_stream_sink_sound_data %d -> %d", len, result_len);
73+
ESP_LOGI( "a2dp_stream_sink_sound_data %d -> %d", len, result_len);
7674
// allow some other task
7775
yield();
7876
}
@@ -125,26 +123,26 @@ class A2DPStream : public Stream {
125123

126124
switch (mode){
127125
case TX_MODE:
128-
log_a2dp.info("Starting a2dp_source...");
126+
LOGI("Starting a2dp_source...");
129127
source(); // allocate object
130128
a2dp_source->start(name, a2dp_stream_source_sound_data);
131129
while(!a2dp_source->isConnected()){
132130
yield();
133131
}
134-
log_a2dp.info("a2dp_source is connected...");
132+
LOGI("a2dp_source is connected...");
135133
//is_a2dp_setup done in callback
136134
//is_a2dp_setup = true;
137135
break;
138136

139137
case RX_MODE:
140-
log_a2dp.info("Starting a2dp_sink...");
138+
LOGI("Starting a2dp_sink...");
141139
sink(); // allocate object
142140
a2dp_sink->set_stream_reader(&a2dp_stream_sink_sound_data, false);
143141
a2dp_sink->start(name);
144142
while(!a2dp_sink->isConnected()){
145143
yield();
146144
}
147-
log_a2dp.info("a2dp_sink is connected...");
145+
LOGI("a2dp_sink is connected...");
148146
is_a2dp_setup = true;
149147
break;
150148
}
@@ -172,9 +170,9 @@ class A2DPStream : public Stream {
172170
size_t result = 0;
173171
if (is_a2dp_setup){
174172
result = a2dp_buffer.writeArray(data,len);
175-
ESP_LOGI(AUDIO_TAG, "write %d->%d", len,result);
173+
ESP_LOGI( "write %d->%d", len,result);
176174
} else {
177-
ESP_LOGW(AUDIO_TAG, "write failed because !is_a2dp_setup");
175+
ESP_LOGW( "write failed because !is_a2dp_setup");
178176
}
179177
return result;
180178
}
@@ -195,21 +193,21 @@ class A2DPStream : public Stream {
195193
size_t result = 0;
196194
if (is_a2dp_setup){
197195
result = a2dp_buffer.readArray(data, len);
198-
ESP_LOGI(AUDIO_TAG, "read %d->%d", len,result);
196+
ESP_LOGI( "read %d->%d", len,result);
199197
} else {
200-
ESP_LOGW(AUDIO_TAG, "readBytes failed because !is_a2dp_setup");
198+
ESP_LOGW( "readBytes failed because !is_a2dp_setup");
201199
}
202200
return result;
203201
}
204202

205203
// not supported
206204
virtual int read() {
207-
ESP_LOGE(AUDIO_TAG, "read() not supported");
205+
ESP_LOGE( "read() not supported");
208206
return -1;
209207
}
210208
// not supported
211209
virtual int peek() {
212-
ESP_LOGE(AUDIO_TAG, "peek() not supported");
210+
ESP_LOGE( "peek() not supported");
213211
return -1;
214212
}
215213

@@ -228,7 +226,7 @@ class A2DPStream : public Stream {
228226
char* name = nullptr;
229227

230228
A2DPStream() {
231-
log_a2dp.info("A2DPStream");
229+
LOGI("A2DPStream");
232230
}
233231

234232
};

src/AudioConfig.h

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
*
66
*/
77
#pragma once
8+
#include <string.h>
89

910
/**
11+
* -------------------------------------------------------------------------
1012
* @brief Optional Functionality - comment out if not wanted
1113
*/
1214

@@ -19,6 +21,7 @@
1921
#endif
2022

2123
/**
24+
* -------------------------------------------------------------------------
2225
* @brief Common Default Settings that can usually be changed in the API
2326
*/
2427
#ifndef LED_BUILTIN
@@ -34,15 +37,14 @@
3437
#define A2DP_BUFFER_SIZE 4096
3538
#define A2DP_BUFFER_COUNT 8
3639
#define DEFAUT_ADC_PIN 34
37-
#define I2S_TAG "I2S"
38-
#define AUDIO_TAG "AUDIO"
40+
3941
/**
42+
* -------------------------------------------------------------------------
4043
* @brief Platform specific Settings
4144
*
4245
*/
4346
#ifdef ESP32
4447
#include "esp32-hal-log.h"
45-
#define USE_ESP32_LOGGER
4648
#define PIN_I2S_BCK 14
4749
#define PIN_I2S_WS 15
4850
#define PIN_I2S_DATA_IN 32
@@ -51,20 +53,38 @@
5153
// Default Setting: The mute pin can be switched off by setting it to -1. Or you could drive the LED by assigning LED_BUILTIN
5254
#define PIN_I2S_MUTE 23
5355
#define SOFT_MUTE_VALUE LOW
54-
55-
56-
5756
#endif
5857

5958
#ifdef ESP8266
60-
6159
#define PIN_I2S_BCK -1
6260
#define PIN_I2S_WS -1
6361
#define PIN_I2S_DATA_IN -1
6462
#define PIN_I2S_DATA_OUT -1
6563
#define I2S_USE_APLL false
6664
#define PIN_I2S_MUTE 23
6765
#define SOFT_MUTE_VALUE LOW
68-
6966
#endif
7067

68+
/**
69+
* -------------------------------------------------------------------------
70+
* @brief Logging
71+
*/
72+
73+
// Logging Configuration
74+
#define USE_LOGGING
75+
#define LOG_LEVEL AudioLogger::Info
76+
#define LOG_STREAM Serial
77+
78+
// Logging Implementation
79+
#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
80+
#ifdef USE_LOGGING
81+
#define LOGD(...) AudioLogger::instance().printLog(__FILENAME__,__LINE__, AudioLogger::Debug, __VA_ARGS__)
82+
#define LOGI(...) AudioLogger::instance().printLog(__FILENAME__,__LINE__, AudioLogger::Info, __VA_ARGS__)
83+
#define LOGW(...) AudioLogger::instance().printLog(__FILENAME__,__LINE__, AudioLogger::Warn, __VA_ARGS__)
84+
#define LOGE(...) AudioLogger::instance().printLog(__FILENAME__,__LINE__, AudioLogger::Error, __VA_ARGS__)
85+
#else
86+
#define LOGD(...)
87+
#define LOGI(...)
88+
#define LOGW(...)
89+
#define LOGE(...)
90+
#endif

src/AudioTools/AnalogAudio.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,15 +218,15 @@ class AnalogAudio {
218218
size_t writeBytes(const void *src, size_t size_bytes){
219219
size_t result = 0;
220220
if (i2s_write(i2s_num, src, size_bytes, &result, portMAX_DELAY)!=ESP_OK){
221-
ESP_LOGE(I2S_TAG, "%s", __func__);
221+
ESP_LOGE("%s", __func__);
222222
}
223223
return result;
224224
}
225225

226226
size_t readBytes(void *dest, size_t size_bytes){
227227
size_t result = 0;
228228
if (i2s_read(i2s_num, dest, size_bytes, &result, portMAX_DELAY)!=ESP_OK){
229-
ESP_LOGE(I2S_TAG, "%s", __func__);
229+
ESP_LOGE("%s", __func__);
230230
}
231231
ESP_LOGD(ADC_TAG, "%s - len: %d -> %d", __func__, size_bytes, result);
232232
//vTaskDelay(1);

src/AudioTools/AudioCopy.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
namespace audio_tools {
1111

12-
AudioLogger stream_log = AudioLogger::instance();
13-
1412
/**
1513
* @brief Typed Stream Copy which supports the conversion from channel to 2 channels. We make sure that we
1614
* allways copy full samples
@@ -25,7 +23,7 @@ class StreamCopyT {
2523
this->buffer_size = buffer_size;
2624
buffer = new uint8_t[buffer_size];
2725
if (buffer==nullptr){
28-
ESP_LOGE(AUDIO_TAG, "Could not allocate enough memory for StreamCopy: %d bytes", buffer_size);
26+
ESP_LOGE("Could not allocate enough memory for StreamCopy: %d bytes", buffer_size);
2927
}
3028
}
3129

@@ -39,7 +37,7 @@ class StreamCopyT {
3937
size_t delayCount = 0;
4038
size_t len = available();
4139
size_t bytes_to_read;
42-
ESP_LOGI(AUDIO_TAG, "StreamCopyT::copy - begin");
40+
ESP_LOGI("StreamCopyT::copy - begin");
4341

4442
if (len>0){
4543
bytes_to_read = min(len, static_cast<size_t>(buffer_size));
@@ -48,7 +46,7 @@ class StreamCopyT {
4846
size_t bytes_read = from->readBytes(buffer, bytes_to_read);
4947
result = write(bytes_read,delayCount);
5048
}
51-
ESP_LOGI(AUDIO_TAG, "StreamCopy::copy %u -> %u bytes - in %d hops\n", bytes_to_read, result, delayCount);
49+
ESP_LOGI("StreamCopy::copy %u -> %u bytes - in %d hops\n", bytes_to_read, result, delayCount);
5250
return result;
5351
}
5452

@@ -60,7 +58,7 @@ class StreamCopyT {
6058
size_t bytes_read;
6159
size_t len = available();
6260
size_t bytes_to_read;
63-
ESP_LOGI(AUDIO_TAG, "StreamCopyT::copy2 - begin");
61+
ESP_LOGI("StreamCopyT::copy2 - begin");
6462

6563
if (len>0){
6664
bytes_to_read = min(len, static_cast<size_t>(buffer_size / 2));
@@ -79,7 +77,7 @@ class StreamCopyT {
7977
}
8078
result = write(samples * sizeof(T)*2, delayCount);
8179
}
82-
ESP_LOGI(AUDIO_TAG, "StreamCopy::copy %u -> %u bytes - in %d hops", bytes_to_read, result, delayCount);
80+
ESP_LOGI("StreamCopy::copy %u -> %u bytes - in %d hops", bytes_to_read, result, delayCount);
8381
return result;
8482
}
8583

@@ -132,7 +130,7 @@ class StreamCopy : public StreamCopyT<uint8_t> {
132130
write(result, delayCount);
133131
}
134132

135-
ESP_LOGI(AUDIO_TAG, "StreamCopy::copy %d bytes - in %d hops\n", result, delayCount);
133+
ESP_LOGI("StreamCopy::copy %d bytes - in %d hops\n", result, delayCount);
136134
return result;
137135
}
138136

src/AudioTools/AudioLogger.h

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
#include "AudioConfig.h"
44
#include "Stream.h"
55

6-
#ifndef SOUND_LOG_LEVEL
7-
#define SOUND_LOG_LEVEL Error
8-
#endif
9-
106
#ifndef PRINTF_BUFFER_SIZE
117
#define PRINTF_BUFFER_SIZE 160
128
#endif
@@ -35,7 +31,7 @@ class AudioLogger {
3531

3632

3733
/// activate the logging
38-
void begin(Stream& out, LogLevel level=SOUND_LOG_LEVEL) {
34+
void begin(Stream& out, LogLevel level=LOG_LEVEL) {
3935
this->log_stream_ptr = &out;
4036
this->log_level = level;
4137
this->active = true;
@@ -77,41 +73,33 @@ class AudioLogger {
7773
int len = 0;
7874
va_list args;
7975

80-
#ifdef USE_ESP32_LOGGER
81-
va_start(args,fmt);
82-
//Serial.println(serial_printf_buffer);
83-
// TODO why is the following not working
84-
const char* msg = serial_printf_buffer;
85-
esp_log_level_t level;
86-
switch(current_level){
87-
case Error:
88-
level = ESP_LOG_ERROR;
89-
break;
90-
case Info:
91-
level = ESP_LOG_INFO;
92-
break;
93-
case Warning:
94-
level = ESP_LOG_WARN;
95-
break;
96-
case Debug:
97-
level = ESP_LOG_DEBUG;
98-
break;
99-
default:
100-
level = ESP_LOG_DEBUG;
101-
break;
76+
if (this->active && log_stream_ptr!=nullptr && current_level >= log_level){
77+
char serial_printf_buffer[PRINTF_BUFFER_SIZE] = {0};
78+
va_start(args,fmt);
79+
len = vsnprintf(serial_printf_buffer,PRINTF_BUFFER_SIZE, fmt, args);
80+
log_stream_ptr->print(serial_printf_buffer);
81+
va_end(args);
10282
}
103-
esp_log_writev(level, TAG, fmt, args);
104-
va_end(args);
83+
return len;
84+
}
10585

106-
#else
86+
int printLog(const char* file, int line, LogLevel current_level, const char* fmt, ...) const {
87+
char serial_printf_buffer[PRINTF_BUFFER_SIZE] = {0};
88+
int len = 0;
89+
va_list args;
90+
len+=log_stream_ptr->print(file);
91+
len+=log_stream_ptr->print(" ");
92+
len+=log_stream_ptr->print(line);
93+
len+=log_stream_ptr->print(": ");
94+
10795
if (this->active && log_stream_ptr!=nullptr && current_level >= log_level){
10896
char serial_printf_buffer[PRINTF_BUFFER_SIZE] = {0};
10997
va_start(args,fmt);
110-
len = vsnprintf(serial_printf_buffer,PRINTF_BUFFER_SIZE, fmt, args);
98+
len += vsnprintf(serial_printf_buffer,PRINTF_BUFFER_SIZE, fmt, args);
11199
log_stream_ptr->print(serial_printf_buffer);
112100
va_end(args);
113101
}
114-
#endif
102+
len += log_stream_ptr->println();
115103
return len;
116104
}
117105

@@ -126,10 +114,10 @@ class AudioLogger {
126114

127115

128116
protected:
129-
Stream *log_stream_ptr = nullptr;
117+
Stream *log_stream_ptr = &LOG_STREAM;
130118
const char* TAG = "AudioTools";
131-
LogLevel log_level = Error;
132-
bool active = false;
119+
LogLevel log_level = LOG_LEVEL;
120+
bool active = true;
133121

134122
AudioLogger(){
135123
}

0 commit comments

Comments
 (0)