From 278e3f7d5fea2a8b3775e76257dfd96a5e5c2f11 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Fri, 18 May 2018 11:00:47 +0200 Subject: Logging: add --show-log-backtrace Useful in debug builds to see a functions callers. --- intern/clog/CLG_log.h | 1 + intern/clog/clog.c | 37 ++++++++++++++++++++++++++++++++----- source/creator/creator_args.c | 13 +++++++++++++ 3 files changed, 46 insertions(+), 5 deletions(-) diff --git a/intern/clog/CLG_log.h b/intern/clog/CLG_log.h index ff8f983b10c..61fc93be9e7 100644 --- a/intern/clog/CLG_log.h +++ b/intern/clog/CLG_log.h @@ -146,6 +146,7 @@ void CLG_exit(void); void CLG_output_set(void *file_handle); void CLG_output_use_basename_set(int value); void CLG_fatal_fn_set(void (*fatal_fn)(void *file_handle)); +void CLG_backtrace_fn_set(void (*fatal_fn)(void *file_handle)); void CLG_type_filter_include(const char *type_filter, int type_filter_len); void CLG_type_filter_exclude(const char *type_filter, int type_filter_len); diff --git a/intern/clog/clog.c b/intern/clog/clog.c index e93ba9ec2b7..2e5201d63b0 100644 --- a/intern/clog/clog.c +++ b/intern/clog/clog.c @@ -81,6 +81,7 @@ typedef struct CLogContext { struct { void (*fatal_fn)(void *file_handle); + void (*backtrace_fn)(void *file_handle); } callbacks; } CLogContext; @@ -328,15 +329,23 @@ static CLG_LogType *clg_ctx_type_register(CLogContext *ctx, const char *identifi return ty; } -static void clg_ctx_fatal_action(CLogContext *ctx, FILE *file_handle) +static void clg_ctx_fatal_action(CLogContext *ctx) { if (ctx->callbacks.fatal_fn != NULL) { - ctx->callbacks.fatal_fn(file_handle); + ctx->callbacks.fatal_fn(ctx->output_file); } - fflush(file_handle); + fflush(ctx->output_file); abort(); } +static void clg_ctx_backtrace(CLogContext *ctx) +{ + /* Note: we avoid writing fo 'FILE', for backtrace we make an exception, + * if necessary we could have a version of the callback that writes to file descriptor all at once. */ + ctx->callbacks.backtrace_fn(ctx->output_file); + fflush(ctx->output_file); +} + /** \} */ /* -------------------------------------------------------------------- */ @@ -408,8 +417,12 @@ void CLG_log_str( clg_str_free(&cstr); + if (lg->ctx->callbacks.backtrace_fn) { + clg_ctx_backtrace(lg->ctx); + } + if (severity == CLG_SEVERITY_FATAL) { - clg_ctx_fatal_action(lg->ctx, lg->ctx->output_file); + clg_ctx_fatal_action(lg->ctx); } } @@ -439,8 +452,12 @@ void CLG_logf( clg_str_free(&cstr); + if (lg->ctx->callbacks.backtrace_fn) { + clg_ctx_backtrace(lg->ctx); + } + if (severity == CLG_SEVERITY_FATAL) { - clg_ctx_fatal_action(lg->ctx, lg->ctx->output_file); + clg_ctx_fatal_action(lg->ctx); } } @@ -470,6 +487,11 @@ static void CLG_ctx_fatal_fn_set(CLogContext *ctx, void (*fatal_fn)(void *file_h ctx->callbacks.fatal_fn = fatal_fn; } +static void CLG_ctx_backtrace_fn_set(CLogContext *ctx, void (*backtrace_fn)(void *file_handle)) +{ + ctx->callbacks.backtrace_fn = backtrace_fn; +} + static void clg_ctx_type_filter_append(CLG_IDFilter **flt_list, const char *type_match, int type_match_len) { if (type_match_len == 0) { @@ -567,6 +589,11 @@ void CLG_fatal_fn_set(void (*fatal_fn)(void *file_handle)) CLG_ctx_fatal_fn_set(g_ctx, fatal_fn); } +void CLG_backtrace_fn_set(void (*fatal_fn)(void *file_handle)) +{ + CLG_ctx_backtrace_fn_set(g_ctx, fatal_fn); +} + void CLG_type_filter_exclude(const char *type_match, int type_match_len) { CLG_ctx_type_filter_exclude(g_ctx, type_match, type_match_len); diff --git a/source/creator/creator_args.c b/source/creator/creator_args.c index 675f2621942..4b5038d89df 100644 --- a/source/creator/creator_args.c +++ b/source/creator/creator_args.c @@ -536,6 +536,7 @@ static int arg_handle_print_help(int UNUSED(argc), const char **UNUSED(argv), vo BLI_argsPrintArgDoc(ba, "--log"); BLI_argsPrintArgDoc(ba, "--log-level"); BLI_argsPrintArgDoc(ba, "--log-show-basename"); + BLI_argsPrintArgDoc(ba, "--log-show-backtrace"); BLI_argsPrintArgDoc(ba, "--log-file"); printf("\n"); @@ -748,6 +749,17 @@ static int arg_handle_log_show_basename_set(int UNUSED(argc), const char **UNUSE return 0; } +static const char arg_handle_log_show_backtrace_set_doc[] = +"\n\tShow a back trace for each log message (debug builds only)." +; +static int arg_handle_log_show_backtrace_set(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data)) +{ + /* Ensure types don't become incompatible. */ + void (*fn)(FILE *fp) = BLI_system_backtrace; + CLG_backtrace_fn_set((void (*)(void *))fn); + return 0; +} + static const char arg_handle_log_file_set_doc[] = "\n" "\n" @@ -1948,6 +1960,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-show-backtrace", CB(arg_handle_log_show_backtrace_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); -- cgit v1.2.3