logging: allow to log only the basename of each source

In the C API, add another enum log_file_type value, and when set print only the
basename of the source file path.

Rationale: especially when not building directly in the source dir, the paths
to the source files can become rather long. Usually, just the basename of the
file is sufficient to identify the source line.

Change-Id: If3e4d5fb2066f8bf86e59c82d1752b1a843cf58e
This commit is contained in:
Neels Hofmeyr 2018-01-16 02:49:48 +01:00 committed by Neels Hofmeyr
parent bd7bd3947d
commit 0e2a94326e
2 changed files with 15 additions and 0 deletions

View File

@ -224,6 +224,7 @@ enum log_target_type {
enum log_filename_type {
LOG_FILENAME_NONE,
LOG_FILENAME_PATH,
LOG_FILENAME_BASENAME,
};
/*! structure representing a logging target */

View File

@ -323,6 +323,14 @@ const char* log_category_name(int subsys)
return NULL;
}
static const char *const_basename(const char *path)
{
const char *bn = strrchr(path, '/');
if (!bn || !bn[1])
return path;
return bn + 1;
}
static void _output(struct log_target *target, unsigned int subsys,
unsigned int level, const char *file, int line, int cont,
const char *format, va_list ap)
@ -400,6 +408,12 @@ static void _output(struct log_target *target, unsigned int subsys,
goto err;
OSMO_SNPRINTF_RET(ret, rem, offset, len);
break;
case LOG_FILENAME_BASENAME:
ret = snprintf(buf + offset, rem, "%s:%d ", const_basename(file), line);
if (ret < 0)
goto err;
OSMO_SNPRINTF_RET(ret, rem, offset, len);
break;
}
}
ret = vsnprintf(buf + offset, rem, format, ap);