From 6ef0bd0f90eb059a05e5b109a8de8579b3add07e Mon Sep 17 00:00:00 2001 From: Thilo Graf Date: Fri, 5 May 2023 13:19:52 +0200 Subject: [PATCH] system/debug.h: try for more thread save implementation Replacing printf() with fprintf() in this code section aims to prevent unpredictable behavior in multi-threaded environments that could be caused by printf()'s non-atomic nature. The change is intended to enhance output consistency and prevent potential race conditions. Additionally, it is important to note that many other plain calls to printf() in the Neutrino code could also cause issues and require further investigation. Whether this change will have a positive impact remains to be seen. --- src/system/debug.h | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/system/debug.h b/src/system/debug.h index a05ff1895..ba26705b7 100644 --- a/src/system/debug.h +++ b/src/system/debug.h @@ -39,12 +39,29 @@ enum void setDebugLevel( int level ); +#if 0 +#define dprintf(debuglevel, fmt, args...) \ + do + { \ + if (debug >= debuglevel) \ + printf( "[neutrino] " fmt, ## args); \ + } + while(0) +#define dperror(str) {perror("[neutrino] " str);} +#else +// more thread save implementation #define dprintf(debuglevel, fmt, args...) \ do { \ if (debug >= debuglevel) \ - printf( "[neutrino] " fmt, ## args); \ + fprintf(stderr, "[neutrino] " fmt, ## args); \ } while(0) -#define dperror(str) {perror("[neutrino] " str);} +#define dperror(str) \ + do { \ + char errbuf[256]; \ + strerror_r(errno, errbuf, sizeof(errbuf)); \ + fprintf(stderr, "[neutrino] %s: %s\n", str, errbuf); \ + } while(0) +#endif #endif