Skip to content

Commit 3987e9c

Browse files
committed
feat: reroute printf logging in libjamesdsp to JDSP4Linux's log output
1 parent 0f6101e commit 3987e9c

File tree

13 files changed

+115
-6
lines changed

13 files changed

+115
-6
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include "PrintfStdOutExtension.h"
2+
#include <stdarg.h>
3+
4+
static stdOutHandler _printfStdOutHandlerPtr = 0;
5+
static void* _printfStdOutHandlerUserPtr = 0;
6+
7+
int redirected_printf(const char * format, ...) {
8+
// Make formatted string.
9+
char* outstr = 0;
10+
va_list ap;
11+
va_start(ap, format);
12+
int result = vasprintf(&outstr, format, ap);
13+
va_end(ap);
14+
if(result < 0) // Error occurred and `outstr` undefined if result < 0.
15+
return result;
16+
17+
if(_printfStdOutHandlerPtr != 0)
18+
{
19+
_printfStdOutHandlerPtr(outstr, _printfStdOutHandlerUserPtr);
20+
}
21+
22+
free(outstr);
23+
return result;
24+
}
25+
26+
void setPrintfStdOutHandler(stdOutHandler funcPtr, void* userData)
27+
{
28+
_printfStdOutHandlerPtr = funcPtr;
29+
_printfStdOutHandlerUserPtr = userData;
30+
}
31+
32+
int isPrintfStdOutHandlerSet()
33+
{
34+
return _printfStdOutHandlerPtr != 0;
35+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef EELSTDOUTEXTENSION_H
2+
#define EELSTDOUTEXTENSION_H
3+
4+
typedef void (*stdOutHandler)(const char*, void*);
5+
6+
7+
extern void setPrintfStdOutHandler(stdOutHandler funcPtr, void* userData);
8+
extern int isPrintfStdOutHandlerSet();
9+
10+
#endif // EELSTDOUTEXTENSION_H

libjamesdsp/libjamesdsp.pro

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ QMAKE_CFLAGS += -std=gnu11 -O2
1111
CONFIG += warn_off # Disable warnings for library
1212

1313
DEFINES += CUSTOM_CMD
14+
DEFINES += DEBUG # TODO remove?
15+
DEFINES += printf=redirected_printf
1416

1517
BASEPATH = $$PWD/subtree/Main/libjamesdsp/jni/jamesdsp/jdsp/
1618

@@ -50,6 +52,7 @@ HEADERS += \
5052
$$BASEPATH/generalDSP/spectralInterpolatorFloat.h \
5153
$$BASEPATH/jdsp_header.h \
5254
EELStdOutExtension.h \
55+
PrintfStdOutExtension.h \
5356
JdspImpResToolbox.h
5457

5558
SOURCES += \
@@ -99,6 +102,7 @@ SOURCES += \
99102
$$BASEPATH/generalDSP/spectralInterpolatorFloat.c \
100103
$$BASEPATH/jdspController.c \
101104
EELStdOutExtension.c \
105+
PrintfStdOutExtension.c \
102106
JdspImpResToolbox.c
103107

104108
unix {

src/MainWindow.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ MainWindow::MainWindow(bool statupInTray,
8585
Log::debug("Blocklist mode: " + QString((AppConfig::instance().get<bool>(AppConfig::AudioAppBlocklistInvert) ? "allow" : "block")));
8686
_audioService = new PipewireAudioService();
8787
#endif
88+
connect(_audioService, &IAudioService::logOutputReceived, this, [](const QString& msg){ Log::kernel(msg); });
8889
connect(&DspConfig::instance(), &DspConfig::updated, _audioService, &IAudioService::update);
8990
connect(&DspConfig::instance(), &DspConfig::updatedExternally, _audioService, &IAudioService::update);
9091
}
@@ -146,6 +147,7 @@ MainWindow::MainWindow(bool statupInTray,
146147
});
147148

148149
connect(_audioService, &IAudioService::outputDeviceChanged, &PresetManager::instance(), &PresetManager::onOutputDeviceChanged);
150+
connect(_audioService, &IAudioService::convolverInfoChanged, this, &MainWindow::onConvolverInfoChanged);
149151

150152
// Convolver file info
151153
ConvolverInfoEventArgs ciArgs;

src/audio/base/DspHost.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ class DspHost
2121
EelCompilerStart,
2222
EelCompilerResult,
2323
EelWriteOutputBuffer,
24-
ConvolverInfoChanged
24+
ConvolverInfoChanged,
25+
PrintfWriteOutputBuffer
2526
};
2627

2728
typedef std::function<void(Message,std::any)> MessageHandlerFunc;

src/audio/base/IAudioService.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public slots:
4141
void eelOutputReceived(const QString& output);
4242
void eelVariablesEnumerated(const std::vector<EelVariable>& vars);
4343
void convolverInfoChanged(const ConvolverInfoEventArgs& args);
44+
void logOutputReceived(const QString& output);
4445
void outputDeviceChanged(const QString& deviceName, const QString& deviceId);
4546

4647
};
@@ -76,6 +77,9 @@ inline void IAudioService::handleMessage(DspHost::Message msg, std::any value)
7677
case DspHost::ConvolverInfoChanged:
7778
emit convolverInfoChanged(std::any_cast<ConvolverInfoEventArgs>(value));
7879
break;
80+
case DspHost::PrintfWriteOutputBuffer:
81+
emit logOutputReceived(std::any_cast<QString>(value));
82+
break;
7983
default:
8084
break;
8185
}

src/audio/base/IDspElement.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ class IDspElement
2626
_msgHandler = std::move(extraHandler);
2727
}
2828

29+
void callMessageHandler(DspHost::Message msg, std::any param)
30+
{
31+
_msgHandler(msg, param);
32+
}
33+
2934
protected:
3035
DspHost* _host = nullptr;
3136
DspHost::MessageHandlerFunc _msgHandler;

src/audio/pipewire/PwJamesDspPlugin.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
11
#include "PwJamesDspPlugin.h"
2+
#include <QString>
3+
4+
extern "C" {
5+
#include <PrintfStdOutExtension.h>
6+
}
7+
8+
void receivePrintfStdout(const char* msg, void* userdata) {
9+
auto* plugin = static_cast<PwJamesDspPlugin*>(userdata);
10+
if(plugin != nullptr) {
11+
plugin->callMessageHandler(DspHost::PrintfWriteOutputBuffer, QString(msg));
12+
}
13+
}
214

315
PwJamesDspPlugin::PwJamesDspPlugin(PwPipelineManager* pipe_manager)
416
: PwPluginBase("@PwJamesDspPlugin: ", "JamesDsp", pipe_manager)
517
{
18+
setPrintfStdOutHandler(receivePrintfStdout, this);
19+
620
this->dsp = (JamesDSPLib*) malloc(sizeof(JamesDSPLib));
721
memset(this->dsp, 0, sizeof(JamesDSPLib));
822

@@ -31,6 +45,7 @@ PwJamesDspPlugin::~PwJamesDspPlugin() {
3145
JamesDSPFree(this->dsp);
3246
JamesDSPGlobalMemoryDeallocation();
3347

48+
setPrintfStdOutHandler(nullptr, nullptr);
3449
util::debug(log_tag + name + " destroyed");
3550
}
3651

src/audio/pulseaudio/pipeline/JamesDspElement.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
11
#include "config/DspConfig.h"
22

33
#include "JamesDspElement.h"
4-
54
#include "DspHost.h"
65

6+
extern "C" {
7+
#include <PrintfStdOutExtension.h>
8+
}
9+
10+
void receivePrintfStdout(const char* msg, void* userdata) {
11+
auto* plugin = static_cast<JamesDspElement*>(userdata);
12+
if(plugin != nullptr) {
13+
plugin->callMessageHandler(DspHost::PrintfWriteOutputBuffer, QString(msg));
14+
}
15+
}
16+
17+
718
JamesDspElement::JamesDspElement() : FilterElement("jamesdsp", "jamesdsp")
819
{
20+
setPrintfStdOutHandler(receivePrintfStdout, this);
21+
922
gboolean gEnabled;
1023
gpointer gDspPtr = NULL;
1124
this->getValues("dsp_ptr", &gDspPtr,
@@ -26,7 +39,12 @@ JamesDspElement::JamesDspElement() : FilterElement("jamesdsp", "jamesdsp")
2639
_msgHandler(msg, value);
2740
break;
2841
}
29-
});
42+
});
43+
}
44+
45+
JamesDspElement::~JamesDspElement()
46+
{
47+
setPrintfStdOutHandler(nullptr, nullptr);
3048
}
3149

3250
DspStatus JamesDspElement::status()

src/audio/pulseaudio/pipeline/JamesDspElement.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class JamesDspElement : public FilterElement, public IDspElement
99
{
1010
public:
1111
JamesDspElement();
12+
~JamesDspElement();
1213
DspStatus status() override;
1314

1415
private:

0 commit comments

Comments
 (0)