From b201fd2db979bb42eb0d2ed033748f3a0e4bdb1e Mon Sep 17 00:00:00 2001 From: Carter Li Date: Sat, 3 Oct 2026 10:21:08 +0800 Subject: [PATCH] Common: improves reliability of FF_DEBUG --- CMakeLists.txt | 2 +- src/common/debug.h | 40 +++++---------- src/common/impl/{debug_windows.c => debug.c} | 53 ++++++++++++++++++-- src/detection/battery/battery_windows.c | 4 +- src/detection/brightness/brightness_linux.c | 9 ++-- 5 files changed, 67 insertions(+), 41 deletions(-) rename src/common/impl/{debug_windows.c => debug.c} (54%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 586eb8fae..1cf37819a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -474,6 +474,7 @@ file(GENERATE OUTPUT logo_builtin.h CONTENT "${LOGO_BUILTIN_H}") set(LIBFASTFETCH_SRC src/common/impl/commandoption.c + src/common/impl/debug.c src/common/impl/duration.c src/common/impl/font.c src/common/impl/format.c @@ -1106,7 +1107,6 @@ elseif(WIN32) src/common/impl/processing_windows.c src/common/impl/FFPlatform_windows.c src/common/impl/binary_windows.c - src/common/impl/debug_windows.c src/common/impl/kmod_windows.c src/common/windows/getline.c src/common/windows/com.c diff --git a/src/common/debug.h b/src/common/debug.h index ea7c27f4c..cb978cbc4 100644 --- a/src/common/debug.h +++ b/src/common/debug.h @@ -1,38 +1,22 @@ #pragma once -#include "fastfetch.h" -#include "common/time.h" - -[[gnu::nonnull(1), gnu::pure, nodiscard]] static inline const char* ffFindFileName(const char* file) { - const char* lastSlash = __builtin_strrchr(file, '/'); -#ifdef _WIN32 - if (lastSlash == nullptr) { - lastSlash = __builtin_strrchr(file, '\\'); - } -#endif - if (lastSlash != nullptr) { - return lastSlash + 1; - } - return file; -} - #ifndef NDEBUG - #define FF_DEBUG_PRINT(file_, line_, format_, ...) \ - do { \ - if (instance.config.display.debugMode) \ - fprintf(stderr, "[%s%4d, %s] " format_ "\n", ffFindFileName(file_), line_, ffTimeToTimeStr(ffTimeGetNow()), ##__VA_ARGS__); \ - } while (0) -#else - #define FF_DEBUG_PRINT(file_, line_, format_, ...) \ - do { \ - } while (0) -#endif + #include "fastfetch.h" + #include "common/time.h" -#define FF_DEBUG(format, ...) FF_DEBUG_PRINT(__FILE__, __LINE__, format, ##__VA_ARGS__) +[[gnu::nonnull(1, 3), gnu::format(printf, 3, 4)]] +void ffDebugPrint(const char* file, int line, const char* format, ...); -#if _WIN32 + #define FF_DEBUG(format, ...) ffDebugPrint(__FILE__, __LINE__, format, ##__VA_ARGS__) + + #if _WIN32 const char* ffDebugWin32Error(DWORD errorCode); const char* ffDebugNtStatus(NTSTATUS status); const char* ffDebugConfigRet(unsigned long /*CONFIGRET*/ ret); const char* ffDebugHResult(HRESULT hr); + #endif + +#else + + #define FF_DEBUG(format, ...) ((void) 0) #endif diff --git a/src/common/impl/debug_windows.c b/src/common/impl/debug.c similarity index 54% rename from src/common/impl/debug_windows.c rename to src/common/impl/debug.c index 6247ec437..86e76383f 100644 --- a/src/common/impl/debug_windows.c +++ b/src/common/impl/debug.c @@ -1,8 +1,50 @@ -#include "common/debug.h" -#include "common/windows/nt.h" +#include -#include -#include +#ifndef NDEBUG + + #include "common/debug.h" + #include "common/thread.h" + +[[gnu::nonnull(1), gnu::pure, gnu::always_inline, nodiscard]] +static inline const char* ffFindFileName(const char* file) { + const char* lastSlash = __builtin_strrchr(file, '/'); + #ifdef _WIN32 + if (lastSlash == nullptr) { + lastSlash = __builtin_strrchr(file, '\\'); + } + #endif + if (lastSlash != nullptr) { + return lastSlash + 1; + } + return file; +} + +#if __GNUC__ && !__clang__ +[[gnu::optimize("O2")]] +#endif +void ffDebugPrint(const char* file, int line, const char* format, ...) { + if (!instance.config.display.debugMode) { + return; + } + + static FFThreadMutex debugMutex = FF_THREAD_MUTEX_INITIALIZER; + ffThreadMutexLock(&debugMutex); + int errno_ = errno; + static char errmsg_[1024]; + va_list args; + va_start(args, format); + vsnprintf(errmsg_, sizeof(errmsg_), format, args); + va_end(args); + fprintf(stderr, "[%s%4d, %s] %s\n", ffFindFileName(file), line, ffTimeToTimeStr(ffTimeGetNow()), errmsg_); + errno = errno_; + ffThreadMutexUnlock(&debugMutex); +} + + #if _WIN32 + #include "common/windows/nt.h" + + #include + #include const char* ffDebugWin32Error(DWORD errorCode) { static char buffer[512]; @@ -58,3 +100,6 @@ static inline DWORD HRESULTToWin32Error(HRESULT hr) { const char* ffDebugHResult(HRESULT hr) { return ffDebugWin32Error(HRESULTToWin32Error(hr)); } + #endif + +#endif diff --git a/src/detection/battery/battery_windows.c b/src/detection/battery/battery_windows.c index f8280aac1..4733b141d 100644 --- a/src/detection/battery/battery_windows.c +++ b/src/detection/battery/battery_windows.c @@ -1,10 +1,10 @@ #define INITGUID #include "battery.h" -#include "common/endian.h" - #include "common/debug.h" +#include "common/endian.h" #include "common/mallocHelper.h" +#include "common/windows/nt.h" #include "common/windows/unicode.h" #include "common/windows/wmi.h" diff --git a/src/detection/brightness/brightness_linux.c b/src/detection/brightness/brightness_linux.c index 4317e6b46..46769a6a9 100644 --- a/src/detection/brightness/brightness_linux.c +++ b/src/detection/brightness/brightness_linux.c @@ -60,8 +60,7 @@ snprintf(path, sizeof(path), "/dev/i2c-%d", cache->devices[i]); FF_AUTO_CLOSE_FD int fd = open(path, O_RDWR | O_CLOEXEC); if (fd < 0) { - int error = errno; - FF_DEBUG("Brightness DDC/CI: cached bus %d is unavailable (errno=%d: %s)", cache->devices[i], error, strerror(error)); + FF_DEBUG("Brightness DDC/CI: cached bus %d is unavailable (errno=%d: %s)", cache->devices[i], errno, strerror(errno)); valid = false; break; } @@ -119,8 +118,7 @@ int ioctlResult = ioctl(fd, I2C_RDWR, &data); if (ioctlResult != 1) { - int error = errno; - FF_DEBUG("Brightness DDC/CI: write request failed (fd=%d, result=%d, errno=%d: %s)", fd, ioctlResult, error, strerror(error)); + FF_DEBUG("Brightness DDC/CI: write request failed (fd=%d, result=%d, errno=%d: %s)", fd, ioctlResult, errno, strerror(errno)); return false; } @@ -131,8 +129,7 @@ msg.buf = response; ioctlResult = ioctl(fd, I2C_RDWR, &data); if (ioctlResult != 1) { - int error = errno; - FF_DEBUG("Brightness DDC/CI: read response failed (fd=%d, result=%d, errno=%d: %s)", fd, ioctlResult, error, strerror(error)); + FF_DEBUG("Brightness DDC/CI: read response failed (fd=%d, result=%d, errno=%d: %s)", fd, ioctlResult, errno, strerror(errno)); return false; }