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.
pull/66/head
nirenjan 2026-04-03 23:12:01 -07:00
parent 836206d93f
commit 583d4fd646
5 changed files with 33 additions and 8 deletions

View File

@ -101,11 +101,14 @@ dep_math = compiler.find_library('m', required: false)
####################################################################### #######################################################################
# pinelog # 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 dep_systemd.found() and not get_option('systemd-logs').disabled()
# If systemd logs is enabled or auto, and systemd is found, then hide # If systemd logs is enabled or auto, and systemd is found, then hide
# the timestamps in log messages # the timestamps in log messages
pinelog_options = ['show-date=false'] pinelog_options += ['show-date=false']
endif endif
sub_pinelog = subproject('pinelog', required: true, sub_pinelog = subproject('pinelog', required: true,

View File

@ -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 * `PINELOG_SHOW_BACKTRACE` - Display the file and line where the message is
logged. 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. Set these flags by using the `-D` compiler argument, .e.g.
`-DPINELOG_SHOW_LEVEL=1 -DPINELOG_SHOW_DATE=1` `-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 ### Level strings
The application can control the level strings displayed by means of preprocessor The application can control the level strings displayed by means of preprocessor

View File

@ -1,10 +1,13 @@
project('pinelog', 'C', project('pinelog', 'C',
default_options: ['show-date=true', 'show-level=true', 'show-backtrace=true']) 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 = [ pinelog_cflags = [
'-DPINELOG_SHOW_DATE=@0@'.format(get_option('show-date').to_int()), '-DPINELOG_SHOW_DATE=@0@'.format(get_option('show-date').to_int()),
'-DPINELOG_SHOW_LEVEL=@0@'.format(get_option('show-level').to_int()), '-DPINELOG_SHOW_LEVEL=@0@'.format(get_option('show-level').to_int()),
'-DPINELOG_SHOW_BACKTRACE=@0@'.format(get_option('show-backtrace').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) 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_inc = include_directories('.')
libpinelog_dep = declare_dependency( libpinelog_dep = declare_dependency(
include_directories: libpinelog_inc, include_directories: libpinelog_inc,
link_with: libpinelog link_with: libpinelog,
compile_args: [pinelog_path_arg],
) )
prog_class = ['bench', 'test'] prog_class = ['bench', 'test']
@ -44,7 +48,8 @@ foreach date_arg: [0, 1]
'-DPINELOG_DEFAULT_LEVEL=PINELOG_LVL_TRACE', '-DPINELOG_DEFAULT_LEVEL=PINELOG_LVL_TRACE',
'-DPINELOG_DEFAULT_STREAM=stderr', '-DPINELOG_DEFAULT_STREAM=stderr',
'-DPINELOG_TEST', '-DPINELOG_TEST',
date_def, level_def, backtrace_def date_def, level_def, backtrace_def,
pinelog_path_arg,
] ]
test_name = test_name_template.format( test_name = test_name_template.format(
date_name, level_name, backtrace_name) date_name, level_name, backtrace_name)

View File

@ -6,3 +6,6 @@ option('show-level', type: 'boolean',
option('show-backtrace', type: 'boolean', option('show-backtrace', type: 'boolean',
description: 'Show backtrace information in log message') 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)')

View File

@ -180,10 +180,16 @@ void pinelog_log_message(int module, int level, const char *file, int line, cons
#define pinelog_exit exit #define pinelog_exit exit
#endif #endif
// Base filename /* When 1 (default), only the basename is shown (e.g. core.c). Set to 0 to use the
#if defined __has_builtin * compiler's __FILE__ (often a relative path like libx52/core.c vs libx52io/core.c). */
# if __has_builtin(__builtin_strrchr) #ifndef PINELOG_STRIP_FILE_PATH
# define PINELOG_FILE __builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__ # 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
#endif #endif
#ifndef PINELOG_FILE #ifndef PINELOG_FILE