From 583d4fd6469141797a53abf0769928bea638cdca Mon Sep 17 00:00:00 2001 From: nirenjan Date: Fri, 3 Apr 2026 23:12:01 -0700 Subject: [PATCH] feat: Make prefix stripping optional in pinelog Prior to this change, pinelog would always strip the directory prefix from __FILE__, displaying only the basename. This change adds the ability to check the full path passed to the compiler, which may still be only a basename, but that's still fine. --- meson.build | 7 +++++-- subprojects/pinelog/README.md | 8 ++++++++ subprojects/pinelog/meson.build | 9 +++++++-- subprojects/pinelog/meson_options.txt | 3 +++ subprojects/pinelog/pinelog.h | 14 ++++++++++---- 5 files changed, 33 insertions(+), 8 deletions(-) diff --git a/meson.build b/meson.build index c38ba5b..386e006 100644 --- a/meson.build +++ b/meson.build @@ -101,11 +101,14 @@ dep_math = compiler.find_library('m', required: false) ####################################################################### # pinelog -pinelog_options = [] +pinelog_options = [ + # Keep directory prefix in __FILE__ for logs (e.g. libx52/core.c vs libx52io/core.c). + 'strip-file-path=false', +] if dep_systemd.found() and not get_option('systemd-logs').disabled() # If systemd logs is enabled or auto, and systemd is found, then hide # the timestamps in log messages - pinelog_options = ['show-date=false'] + pinelog_options += ['show-date=false'] endif sub_pinelog = subproject('pinelog', required: true, diff --git a/subprojects/pinelog/README.md b/subprojects/pinelog/README.md index 1f7e26f..0e8112e 100644 --- a/subprojects/pinelog/README.md +++ b/subprojects/pinelog/README.md @@ -97,9 +97,17 @@ default to `0` (disabled). Set the flag to `1` to enable it. * `PINELOG_SHOW_BACKTRACE` - Display the file and line where the message is logged. +* `PINELOG_STRIP_FILE_PATH` - If `1` (default), the file in the log line is only + the basename. If `0`, the compiler’s `__FILE__` is used instead, which usually + keeps a path prefix (for example `libx52/core.c` vs `libx52io/core.c`). + Set these flags by using the `-D` compiler argument, .e.g. `-DPINELOG_SHOW_LEVEL=1 -DPINELOG_SHOW_DATE=1` +When using Meson, the pinelog option `strip-file-path` sets +`-DPINELOG_STRIP_FILE_PATH` for the library and for dependents via +`declare_dependency(compile_args: ...)`. + ### Level strings The application can control the level strings displayed by means of preprocessor diff --git a/subprojects/pinelog/meson.build b/subprojects/pinelog/meson.build index 5ff0142..c31bb59 100644 --- a/subprojects/pinelog/meson.build +++ b/subprojects/pinelog/meson.build @@ -1,10 +1,13 @@ project('pinelog', 'C', default_options: ['show-date=true', 'show-level=true', 'show-backtrace=true']) +pinelog_path_arg = '-DPINELOG_STRIP_FILE_PATH=@0@'.format(get_option('strip-file-path').to_int()) + pinelog_cflags = [ '-DPINELOG_SHOW_DATE=@0@'.format(get_option('show-date').to_int()), '-DPINELOG_SHOW_LEVEL=@0@'.format(get_option('show-level').to_int()), '-DPINELOG_SHOW_BACKTRACE=@0@'.format(get_option('show-backtrace').to_int()), + pinelog_path_arg, ] libpinelog = static_library('pinelog', 'pinelog.c', c_args: pinelog_cflags) @@ -12,7 +15,8 @@ libpinelog = static_library('pinelog', 'pinelog.c', c_args: pinelog_cflags) libpinelog_inc = include_directories('.') libpinelog_dep = declare_dependency( include_directories: libpinelog_inc, - link_with: libpinelog + link_with: libpinelog, + compile_args: [pinelog_path_arg], ) prog_class = ['bench', 'test'] @@ -44,7 +48,8 @@ foreach date_arg: [0, 1] '-DPINELOG_DEFAULT_LEVEL=PINELOG_LVL_TRACE', '-DPINELOG_DEFAULT_STREAM=stderr', '-DPINELOG_TEST', - date_def, level_def, backtrace_def + date_def, level_def, backtrace_def, + pinelog_path_arg, ] test_name = test_name_template.format( date_name, level_name, backtrace_name) diff --git a/subprojects/pinelog/meson_options.txt b/subprojects/pinelog/meson_options.txt index 20273c6..381de8d 100644 --- a/subprojects/pinelog/meson_options.txt +++ b/subprojects/pinelog/meson_options.txt @@ -6,3 +6,6 @@ option('show-level', type: 'boolean', option('show-backtrace', type: 'boolean', description: 'Show backtrace information in log message') + +option('strip-file-path', type: 'boolean', value: true, + description: 'If true, log location shows only the source basename; if false, use the compiler __FILE__ (path prefix helps when many units share a name, e.g. core.c)') diff --git a/subprojects/pinelog/pinelog.h b/subprojects/pinelog/pinelog.h index 61de8e0..06bb6fe 100644 --- a/subprojects/pinelog/pinelog.h +++ b/subprojects/pinelog/pinelog.h @@ -180,10 +180,16 @@ void pinelog_log_message(int module, int level, const char *file, int line, cons #define pinelog_exit exit #endif -// Base filename -#if defined __has_builtin -# if __has_builtin(__builtin_strrchr) -# define PINELOG_FILE __builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__ +/* When 1 (default), only the basename is shown (e.g. core.c). Set to 0 to use the + * compiler's __FILE__ (often a relative path like libx52/core.c vs libx52io/core.c). */ +#ifndef PINELOG_STRIP_FILE_PATH +# define PINELOG_STRIP_FILE_PATH 1 +#endif +#if PINELOG_STRIP_FILE_PATH +# if defined __has_builtin +# if __has_builtin(__builtin_strrchr) +# define PINELOG_FILE __builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__ +# endif # endif #endif #ifndef PINELOG_FILE