Logging: add argument --log-show-basename

Optionally strips leading path from filenames when logging.
This commit is contained in:
Campbell Barton
2018-03-31 15:27:06 +02:00
parent 5cb440492e
commit db8e7f9780
3 changed files with 48 additions and 5 deletions

View File

@@ -144,6 +144,7 @@ void CLG_init(void);
void CLG_exit(void);
void CLG_output_set(void *file_handle);
void CLG_output_use_basename_set(bool value);
void CLG_fatal_fn_set(void (*fatal_fn)(void *file_handle));
void CLG_type_filter_include(const char *type_filter, int type_filter_len);

View File

@@ -43,6 +43,12 @@
#define STREQ(a, b) (strcmp(a, b) == 0)
#define STREQLEN(a, b, n) (strncmp(a, b, n) == 0)
#ifdef _WIN32
# define PATHSEP_CHAR '\\'
#else
# define PATHSEP_CHAR '/'
#endif
/* -------------------------------------------------------------------- */
/** \name Internal Types
* \{ */
@@ -59,6 +65,7 @@ typedef struct CLogContext {
/* exclude, include filters. */
CLG_IDFilter *filters[2];
bool use_color;
bool use_basename;
/** Borrowed, not owned. */
FILE *output;
@@ -353,9 +360,23 @@ static void write_type(CLogStringBuf *cstr, CLG_LogType *lg)
clg_str_append(cstr, "): ");
}
static void write_file_line_fn(CLogStringBuf *cstr, const char *file_line, const char *fn)
static void write_file_line_fn(CLogStringBuf *cstr, const char *file_line, const char *fn, const bool use_basename)
{
clg_str_append(cstr, file_line);
uint file_line_len = strlen(file_line);
if (use_basename) {
uint file_line_offset = file_line_len;
while (file_line_offset-- > 0) {
if (file_line[file_line_offset] == PATHSEP_CHAR) {
file_line_offset++;
break;
}
}
file_line += file_line_offset;
file_line_len -= file_line_offset;
}
clg_str_append_with_len(cstr, file_line, file_line_len);
clg_str_append(cstr, " ");
clg_str_append(cstr, fn);
clg_str_append(cstr, ": ");
@@ -373,7 +394,7 @@ void CLG_log_str(
write_type(&cstr, lg);
{
write_file_line_fn(&cstr, file_line, fn);
write_file_line_fn(&cstr, file_line, fn, lg->ctx->use_basename);
clg_str_append(&cstr, message);
}
clg_str_append(&cstr, "\n");
@@ -397,12 +418,11 @@ void CLG_logf(
char cstr_stack_buf[CLOG_BUF_LEN_INIT];
clg_str_init(&cstr, cstr_stack_buf, sizeof(cstr_stack_buf));
// FILE *fh = lg->ctx->output;
write_severity(&cstr, severity, lg->ctx->use_color);
write_type(&cstr, lg);
{
write_file_line_fn(&cstr, file_line, fn);
write_file_line_fn(&cstr, file_line, fn, lg->ctx->use_basename);
va_list ap;
va_start(ap, fmt);
@@ -436,6 +456,11 @@ static void CLG_ctx_output_set(CLogContext *ctx, void *file_handle)
#endif
}
static void CLG_ctx_output_use_basename_set(CLogContext *ctx, bool value)
{
ctx->use_basename = value;
}
/** Action on fatal severity. */
static void CLG_ctx_fatal_fn_set(CLogContext *ctx, void (*fatal_fn)(void *file_handle))
{
@@ -520,6 +545,12 @@ void CLG_output_set(void *file_handle)
CLG_ctx_output_set(g_ctx, file_handle);
}
void CLG_output_use_basename_set(bool value)
{
CLG_ctx_output_use_basename_set(g_ctx, value);
}
void CLG_fatal_fn_set(void (*fatal_fn)(void *file_handle))
{
CLG_ctx_fatal_fn_set(g_ctx, fatal_fn);

View File

@@ -535,6 +535,7 @@ static int arg_handle_print_help(int UNUSED(argc), const char **UNUSED(argv), vo
printf("Logging Options:\n");
BLI_argsPrintArgDoc(ba, "--log");
BLI_argsPrintArgDoc(ba, "--log-level");
BLI_argsPrintArgDoc(ba, "--log-show-basename");
BLI_argsPrintArgDoc(ba, "--log-file");
printf("\n");
@@ -732,6 +733,15 @@ static int arg_handle_log_level_set(int argc, const char **argv, void *UNUSED(da
}
}
static const char arg_handle_log_show_basename_set_doc[] =
"\n\tOnly show file name in output (not the leading path)."
;
static int arg_handle_log_show_basename_set(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
{
CLG_output_use_basename_set(true);
return 0;
}
static const char arg_handle_log_file_set_doc[] =
"<filename>\n"
"\n"
@@ -1931,6 +1941,7 @@ void main_args_setup(bContext *C, bArgs *ba, SYS_SystemHandle *syshandle)
BLI_argsAdd(ba, 1, NULL, "--log", CB(arg_handle_log_set), ba);
BLI_argsAdd(ba, 1, NULL, "--log-level", CB(arg_handle_log_level_set), ba);
BLI_argsAdd(ba, 1, NULL, "--log-show-basename", CB(arg_handle_log_show_basename_set), ba);
BLI_argsAdd(ba, 1, NULL, "--log-file", CB(arg_handle_log_file_set), ba);
BLI_argsAdd(ba, 1, "-d", "--debug", CB(arg_handle_debug_mode_set), ba);