commit 8a085b3245fd81e73ab52de9d28d55e489922d97
parent 045b943199393053c606b530fc882661502d53d7
Author: falkTX <falktx@falktx.com>
Date: Sat, 23 Mar 2024 21:59:13 +0100
Allow to redirect logging to file, add "[dpf] " string prefix
Signed-off-by: falkTX <falktx@falktx.com>
Diffstat:
3 files changed, 80 insertions(+), 17 deletions(-)
diff --git a/distrho/DistrhoUtils.hpp b/distrho/DistrhoUtils.hpp
@@ -109,6 +109,27 @@ void d_pass() noexcept {}
@{
*/
+/*
+ * Internal noexcept-safe fopen function.
+ */
+static inline
+FILE* __d_fopen(const char* const filename, FILE* const fallback) noexcept
+{
+ if (std::getenv("DPF_CAPTURE_CONSOLE_OUTPUT") == nullptr)
+ return fallback;
+
+ FILE* ret = nullptr;
+
+ try {
+ ret = std::fopen(filename, "a+");
+ } catch (...) {}
+
+ if (ret == nullptr)
+ ret = fallback;
+
+ return ret;
+}
+
/**
Print a string to stdout with newline (gray color).
Does nothing if DEBUG is not defined.
@@ -119,16 +140,30 @@ void d_pass() noexcept {}
static inline
void d_debug(const char* const fmt, ...) noexcept
{
+ static FILE* const output = __d_fopen("/tmp/dpf.debug.log", stdout);
+
try {
va_list args;
va_start(args, fmt);
- #ifdef DISTRHO_OS_MAC
- std::fprintf(stdout, "\x1b[37;1m");
- #else
- std::fprintf(stdout, "\x1b[30;1m");
- #endif
- std::vfprintf(stdout, fmt, args);
- std::fprintf(stdout, "\x1b[0m\n");
+
+ if (output == stdout)
+ {
+ #ifdef DISTRHO_OS_MAC
+ std::fprintf(output, "\x1b[37;1m[dpf] ");
+ #else
+ std::fprintf(output, "\x1b[30;1m[dpf] ");
+ #endif
+ std::vfprintf(output, fmt, args);
+ std::fprintf(output, "\x1b[0m\n");
+ }
+ else
+ {
+ std::fprintf(output, "[dpf] ");
+ std::vfprintf(output, fmt, args);
+ std::fprintf(output, "\n");
+ }
+
+ std::fflush(output);
va_end(args);
} catch (...) {}
}
@@ -140,11 +175,18 @@ void d_debug(const char* const fmt, ...) noexcept
static inline
void d_stdout(const char* const fmt, ...) noexcept
{
+ static FILE* const output = __d_fopen("/tmp/dpf.stdout.log", stdout);
+
try {
va_list args;
va_start(args, fmt);
- std::vfprintf(stdout, fmt, args);
- std::fprintf(stdout, "\n");
+ std::fprintf(output, "[dpf] ");
+ std::vfprintf(output, fmt, args);
+ std::fprintf(output, "\n");
+ #ifndef DEBUG
+ if (output != stdout)
+ #endif
+ std::fflush(output);
va_end(args);
} catch (...) {}
}
@@ -155,11 +197,18 @@ void d_stdout(const char* const fmt, ...) noexcept
static inline
void d_stderr(const char* const fmt, ...) noexcept
{
+ static FILE* const output = __d_fopen("/tmp/dpf.stderr.log", stderr);
+
try {
va_list args;
va_start(args, fmt);
- std::vfprintf(stderr, fmt, args);
- std::fprintf(stderr, "\n");
+ std::fprintf(output, "[dpf] ");
+ std::vfprintf(output, fmt, args);
+ std::fprintf(output, "\n");
+ #ifndef DEBUG
+ if (output != stderr)
+ #endif
+ std::fflush(output);
va_end(args);
} catch (...) {}
}
@@ -170,12 +219,26 @@ void d_stderr(const char* const fmt, ...) noexcept
static inline
void d_stderr2(const char* const fmt, ...) noexcept
{
+ static FILE* const output = __d_fopen("/tmp/dpf.stderr2.log", stderr);
+
try {
va_list args;
va_start(args, fmt);
- std::fprintf(stderr, "\x1b[31m");
- std::vfprintf(stderr, fmt, args);
- std::fprintf(stderr, "\x1b[0m\n");
+
+ if (output == stdout)
+ {
+ std::fprintf(output, "\x1b[31m[dpf] ");
+ std::vfprintf(output, fmt, args);
+ std::fprintf(output, "\x1b[0m\n");
+ }
+ else
+ {
+ std::fprintf(output, "[dpf] ");
+ std::vfprintf(output, fmt, args);
+ std::fprintf(output, "\n");
+ }
+
+ std::fflush(output);
va_end(args);
} catch (...) {}
}
diff --git a/distrho/src/DistrhoUIDSSI.cpp b/distrho/src/DistrhoUIDSSI.cpp
@@ -413,7 +413,7 @@ int main(int argc, char* argv[])
if (argc != 5)
{
- fprintf(stderr, "Usage: %s <osc-url> <plugin-dll> <plugin-label> <instance-name>\n", argv[0]);
+ d_stderr("Usage: %s <osc-url> <plugin-dll> <plugin-label> <instance-name>", argv[0]);
return 1;
}
diff --git a/distrho/src/jackbridge/JackBridge.cpp b/distrho/src/jackbridge/JackBridge.cpp
@@ -475,12 +475,12 @@ struct JackBridge {
if (lib == nullptr)
{
- fprintf(stderr, "Failed to load JACK DLL, reason:\n%s\n", lib_error(filename));
+ d_stderr("Failed to load JACK DLL, reason:\n%s", lib_error(filename));
return;
}
else
{
- fprintf(stdout, "%s loaded successfully!\n", filename);
+ d_stdout("%s loaded successfully!", filename);
}
#define JOIN(a, b) a ## b