Logging: add --show-log-backtrace

Useful in debug builds to see a functions callers.
This commit is contained in:
Campbell Barton 2018-05-18 11:00:47 +02:00
parent 96fba1e101
commit 278e3f7d5f
3 changed files with 46 additions and 5 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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[] =
"<filename>\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);