Skip to content

Commit 8a085b3

Browse files
committed
Allow to redirect logging to file, add "[dpf] " string prefix
Signed-off-by: falkTX <[email protected]>
1 parent 045b943 commit 8a085b3

File tree

3 files changed

+80
-17
lines changed

3 files changed

+80
-17
lines changed

distrho/DistrhoUtils.hpp

Lines changed: 77 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,27 @@ void d_pass() noexcept {}
109109
@{
110110
*/
111111

112+
/*
113+
* Internal noexcept-safe fopen function.
114+
*/
115+
static inline
116+
FILE* __d_fopen(const char* const filename, FILE* const fallback) noexcept
117+
{
118+
if (std::getenv("DPF_CAPTURE_CONSOLE_OUTPUT") == nullptr)
119+
return fallback;
120+
121+
FILE* ret = nullptr;
122+
123+
try {
124+
ret = std::fopen(filename, "a+");
125+
} catch (...) {}
126+
127+
if (ret == nullptr)
128+
ret = fallback;
129+
130+
return ret;
131+
}
132+
112133
/**
113134
Print a string to stdout with newline (gray color).
114135
Does nothing if DEBUG is not defined.
@@ -119,16 +140,30 @@ void d_pass() noexcept {}
119140
static inline
120141
void d_debug(const char* const fmt, ...) noexcept
121142
{
143+
static FILE* const output = __d_fopen("/tmp/dpf.debug.log", stdout);
144+
122145
try {
123146
va_list args;
124147
va_start(args, fmt);
125-
#ifdef DISTRHO_OS_MAC
126-
std::fprintf(stdout, "\x1b[37;1m");
127-
#else
128-
std::fprintf(stdout, "\x1b[30;1m");
129-
#endif
130-
std::vfprintf(stdout, fmt, args);
131-
std::fprintf(stdout, "\x1b[0m\n");
148+
149+
if (output == stdout)
150+
{
151+
#ifdef DISTRHO_OS_MAC
152+
std::fprintf(output, "\x1b[37;1m[dpf] ");
153+
#else
154+
std::fprintf(output, "\x1b[30;1m[dpf] ");
155+
#endif
156+
std::vfprintf(output, fmt, args);
157+
std::fprintf(output, "\x1b[0m\n");
158+
}
159+
else
160+
{
161+
std::fprintf(output, "[dpf] ");
162+
std::vfprintf(output, fmt, args);
163+
std::fprintf(output, "\n");
164+
}
165+
166+
std::fflush(output);
132167
va_end(args);
133168
} catch (...) {}
134169
}
@@ -140,11 +175,18 @@ void d_debug(const char* const fmt, ...) noexcept
140175
static inline
141176
void d_stdout(const char* const fmt, ...) noexcept
142177
{
178+
static FILE* const output = __d_fopen("/tmp/dpf.stdout.log", stdout);
179+
143180
try {
144181
va_list args;
145182
va_start(args, fmt);
146-
std::vfprintf(stdout, fmt, args);
147-
std::fprintf(stdout, "\n");
183+
std::fprintf(output, "[dpf] ");
184+
std::vfprintf(output, fmt, args);
185+
std::fprintf(output, "\n");
186+
#ifndef DEBUG
187+
if (output != stdout)
188+
#endif
189+
std::fflush(output);
148190
va_end(args);
149191
} catch (...) {}
150192
}
@@ -155,11 +197,18 @@ void d_stdout(const char* const fmt, ...) noexcept
155197
static inline
156198
void d_stderr(const char* const fmt, ...) noexcept
157199
{
200+
static FILE* const output = __d_fopen("/tmp/dpf.stderr.log", stderr);
201+
158202
try {
159203
va_list args;
160204
va_start(args, fmt);
161-
std::vfprintf(stderr, fmt, args);
162-
std::fprintf(stderr, "\n");
205+
std::fprintf(output, "[dpf] ");
206+
std::vfprintf(output, fmt, args);
207+
std::fprintf(output, "\n");
208+
#ifndef DEBUG
209+
if (output != stderr)
210+
#endif
211+
std::fflush(output);
163212
va_end(args);
164213
} catch (...) {}
165214
}
@@ -170,12 +219,26 @@ void d_stderr(const char* const fmt, ...) noexcept
170219
static inline
171220
void d_stderr2(const char* const fmt, ...) noexcept
172221
{
222+
static FILE* const output = __d_fopen("/tmp/dpf.stderr2.log", stderr);
223+
173224
try {
174225
va_list args;
175226
va_start(args, fmt);
176-
std::fprintf(stderr, "\x1b[31m");
177-
std::vfprintf(stderr, fmt, args);
178-
std::fprintf(stderr, "\x1b[0m\n");
227+
228+
if (output == stdout)
229+
{
230+
std::fprintf(output, "\x1b[31m[dpf] ");
231+
std::vfprintf(output, fmt, args);
232+
std::fprintf(output, "\x1b[0m\n");
233+
}
234+
else
235+
{
236+
std::fprintf(output, "[dpf] ");
237+
std::vfprintf(output, fmt, args);
238+
std::fprintf(output, "\n");
239+
}
240+
241+
std::fflush(output);
179242
va_end(args);
180243
} catch (...) {}
181244
}

distrho/src/DistrhoUIDSSI.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ int main(int argc, char* argv[])
413413

414414
if (argc != 5)
415415
{
416-
fprintf(stderr, "Usage: %s <osc-url> <plugin-dll> <plugin-label> <instance-name>\n", argv[0]);
416+
d_stderr("Usage: %s <osc-url> <plugin-dll> <plugin-label> <instance-name>", argv[0]);
417417
return 1;
418418
}
419419

distrho/src/jackbridge/JackBridge.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,12 +475,12 @@ struct JackBridge {
475475

476476
if (lib == nullptr)
477477
{
478-
fprintf(stderr, "Failed to load JACK DLL, reason:\n%s\n", lib_error(filename));
478+
d_stderr("Failed to load JACK DLL, reason:\n%s", lib_error(filename));
479479
return;
480480
}
481481
else
482482
{
483-
fprintf(stdout, "%s loaded successfully!\n", filename);
483+
d_stdout("%s loaded successfully!", filename);
484484
}
485485

486486
#define JOIN(a, b) a ## b

0 commit comments

Comments
 (0)