aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorChun-Min Chang <[email protected]>2020-04-09 11:27:02 -0700
committerGitHub <[email protected]>2020-04-09 11:27:02 -0700
commit9caa5b113a2a4faef8bd31894fc2d762b884a5cf (patch)
tree18c88923a408497ff8720a3dddae517ca4e98d7f
parent8a4170a8631dfdb3e5b5de7e5062a1e0b00be730 (diff)
downloadcubeb-9caa5b113a2a4faef8bd31894fc2d762b884a5cf.tar.gz
cubeb-9caa5b113a2a4faef8bd31894fc2d762b884a5cf.zip
Only print the filename when logging (#581)
* Only print the filename when logging This keeps the logging message consistent after https://github.com/djg/cubeb-rs/pull/50 is merged, which fixes the BMO 162132. Instead of printing the full path to the file that is currently running, we should print the filename only. As long as we have the filename and the line, we know where the log comes from. * Reformat `LOG_INTERNAL` Align the `\` used for `LOG_INTERNAL` macro. * Correct position of `__FILENAME__` * Define `__FILENAME__` by `__FILE_NAME__` if possible clang 11 provide a `__FILE_NAME__` [1] macro. We should use it if it exists. [1] https://clang.llvm.org/docs/LanguageExtensions.html#builtin-macros * Use `strrchr` in `string.h` when building by MSVC There is no `__builtin_strrchr` in MSVC, so we fullback to use the `strrchr` in `string.h` to trim the path above the current file name.
-rw-r--r--src/cubeb_log.h15
1 files changed, 11 insertions, 4 deletions
diff --git a/src/cubeb_log.h b/src/cubeb_log.h
index a79976b..446e29a 100644
--- a/src/cubeb_log.h
+++ b/src/cubeb_log.h
@@ -16,8 +16,15 @@ extern "C" {
#if defined(__GNUC__) || defined(__clang__)
#define PRINTF_FORMAT(fmt, args) __attribute__((format(printf, fmt, args)))
+#if defined(__FILE_NAME__)
+#define __FILENAME__ __FILE_NAME__
+#else
+#define __FILENAME__ (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__)
+#endif
#else
#define PRINTF_FORMAT(fmt, args)
+#include <string.h>
+#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
#endif
extern cubeb_log_level g_cubeb_log_level;
@@ -32,10 +39,10 @@ void cubeb_async_log_reset_threads();
#define LOGV(msg, ...) LOG_INTERNAL(CUBEB_LOG_VERBOSE, msg, ##__VA_ARGS__)
#define LOG(msg, ...) LOG_INTERNAL(CUBEB_LOG_NORMAL, msg, ##__VA_ARGS__)
-#define LOG_INTERNAL(level, fmt, ...) do { \
- if (g_cubeb_log_callback && level <= g_cubeb_log_level) { \
- g_cubeb_log_callback("%s:%d: " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__); \
- } \
+#define LOG_INTERNAL(level, fmt, ...) do { \
+ if (g_cubeb_log_callback && level <= g_cubeb_log_level) { \
+ g_cubeb_log_callback("%s:%d: " fmt "\n", __FILENAME__, __LINE__, ##__VA_ARGS__); \
+ } \
} while(0)
/* Asynchronous verbose logging, to log in real-time callbacks. */