@@ -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,29 @@ void d_pass() noexcept {}
119140static inline
120141void 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+ else
159+ {
160+ std::fprintf (output, " [dpf] " );
161+ std::vfprintf (output, fmt, args);
162+ std::fprintf (output, " \n " );
163+ }
164+
165+ std::fflush (output);
132166 va_end (args);
133167 } catch (...) {}
134168}
@@ -140,11 +174,18 @@ void d_debug(const char* const fmt, ...) noexcept
140174static inline
141175void d_stdout (const char * const fmt, ...) noexcept
142176{
177+ static FILE* const output = __d_fopen (" /tmp/dpf.stdout.log" , stdout);
178+
143179 try {
144180 va_list args;
145181 va_start (args, fmt);
146- std::vfprintf (stdout, fmt, args);
147- std::fprintf (stdout, " \n " );
182+ std::fprintf (output, " [dpf] " );
183+ std::vfprintf (output, fmt, args);
184+ std::fprintf (output, " \n " );
185+ #ifndef DEBUG
186+ if (output != stdout)
187+ #endif
188+ std::fflush (output);
148189 va_end (args);
149190 } catch (...) {}
150191}
@@ -155,11 +196,18 @@ void d_stdout(const char* const fmt, ...) noexcept
155196static inline
156197void d_stderr (const char * const fmt, ...) noexcept
157198{
199+ static FILE* const output = __d_fopen (" /tmp/dpf.stderr.log" , stderr);
200+
158201 try {
159202 va_list args;
160203 va_start (args, fmt);
161- std::vfprintf (stderr, fmt, args);
162- std::fprintf (stderr, " \n " );
204+ std::fprintf (output, " [dpf] " );
205+ std::vfprintf (output, fmt, args);
206+ std::fprintf (output, " \n " );
207+ #ifndef DEBUG
208+ if (output != stderr)
209+ #endif
210+ std::fflush (output);
163211 va_end (args);
164212 } catch (...) {}
165213}
@@ -170,12 +218,26 @@ void d_stderr(const char* const fmt, ...) noexcept
170218static inline
171219void d_stderr2 (const char * const fmt, ...) noexcept
172220{
221+ static FILE* const output = __d_fopen (" /tmp/dpf.stderr2.log" , stderr);
222+
173223 try {
174224 va_list args;
175225 va_start (args, fmt);
176- std::fprintf (stderr, " \x1b [31m" );
177- std::vfprintf (stderr, fmt, args);
178- std::fprintf (stderr, " \x1b [0m\n " );
226+
227+ if (output == stdout)
228+ {
229+ std::fprintf (output, " \x1b [31m[dpf] " );
230+ std::vfprintf (output, fmt, args);
231+ std::fprintf (output, " \x1b [0m\n " );
232+ }
233+ else
234+ {
235+ std::fprintf (output, " [dpf] " );
236+ std::vfprintf (output, fmt, args);
237+ std::fprintf (output, " \n " );
238+ }
239+
240+ std::fflush (output);
179241 va_end (args);
180242 } catch (...) {}
181243}
0 commit comments