From f648688c45264f19f626b07cb9eaa040590d2b43 Mon Sep 17 00:00:00 2001 From: Horatio Zhang Date: Wed, 9 Oct 2024 14:35:42 +0800 Subject: [PATCH] wsl/hsakmt: Refactor logging function Signed-off-by: Horatio Zhang Reviewed-by: Flora Cui Reviewed-by: Shane Xiao Part-of: --- libhsakmt.h | 36 ++++++++++++++++++++++++++++-------- openclose.cpp | 5 +---- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/libhsakmt.h b/libhsakmt.h index dcece185f5..136180ad98 100644 --- a/libhsakmt.h +++ b/libhsakmt.h @@ -90,15 +90,35 @@ extern int PAGE_SHIFT; /* HSA Thunk logging usage */ extern int hsakmt_debug_level; -#define hsakmt_print(level, fmt, ...) \ - do { if (level <= hsakmt_debug_level) fprintf(stderr, fmt, ##__VA_ARGS__); } while (0) -#define HSAKMT_DEBUG_LEVEL_DEFAULT -1 -#define HSAKMT_DEBUG_LEVEL_ERR 3 -#define HSAKMT_DEBUG_LEVEL_WARNING 4 -#define HSAKMT_DEBUG_LEVEL_INFO 6 -#define HSAKMT_DEBUG_LEVEL_DEBUG 7 +#define get_thread_id() \ + ([]() -> std::string { \ + std::stringstream str_thrd_id; \ + str_thrd_id << std::hex << std::this_thread::get_id(); \ + return str_thrd_id.str(); \ + })() +#define hsakmt_print_common(stream, fmt, ...) \ + do { \ + fprintf(stream, "pid:%d tid:0x%s [%s] " fmt, getpid(), get_thread_id().c_str(), __FUNCTION__, ##__VA_ARGS__); \ + fflush(stream); \ + } while (false) +#ifdef NDEBUG +#define hsakmt_print(level, fmt, ...) \ + do { } while (false) +#else +#define hsakmt_print(level, fmt, ...) \ + do { \ + if (level <= hsakmt_debug_level) { \ + hsakmt_print_common(stdout, fmt, ##__VA_ARGS__); \ + } \ + } while (false) +#endif +#define HSAKMT_DEBUG_LEVEL_ERR -1 +#define HSAKMT_DEBUG_LEVEL_DEFAULT 3 +#define HSAKMT_DEBUG_LEVEL_WARNING 4 +#define HSAKMT_DEBUG_LEVEL_INFO 6 +#define HSAKMT_DEBUG_LEVEL_DEBUG 7 #define pr_err(fmt, ...) \ - hsakmt_print(HSAKMT_DEBUG_LEVEL_ERR, fmt, ##__VA_ARGS__) + hsakmt_print_common(stderr, fmt, ##__VA_ARGS__) #define pr_warn(fmt, ...) \ hsakmt_print(HSAKMT_DEBUG_LEVEL_WARNING, fmt, ##__VA_ARGS__) #define pr_info(fmt, ...) \ diff --git a/openclose.cpp b/openclose.cpp index 22387e4eb9..8ff4abec65 100644 --- a/openclose.cpp +++ b/openclose.cpp @@ -108,10 +108,7 @@ static HSAKMT_STATUS init_vars_from_env(void) { envvar = getenv("HSAKMT_DEBUG_LEVEL"); if (envvar) { - debug_level = atoi(envvar); - if (debug_level >= HSAKMT_DEBUG_LEVEL_ERR && - debug_level <= HSAKMT_DEBUG_LEVEL_DEBUG) - hsakmt_debug_level = debug_level; + hsakmt_debug_level = atoi(envvar); } /* Check whether to support Zero frame buffer */