From 891c1cfc9a355171215821fc91b694273503f139 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Thu, 29 Mar 2018 20:38:32 +0200 Subject: C Logging: use instead of printf for messages - See `--log` help message for usage. - Supports enabling categories. - Color severity. - Optionally logs to a file. - Currently use to replace printf calls in wm module. See D3120 for details. --- source/creator/CMakeLists.txt | 1 + source/creator/creator.c | 12 ++++++ source/creator/creator_args.c | 93 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+) (limited to 'source/creator') diff --git a/source/creator/CMakeLists.txt b/source/creator/CMakeLists.txt index a155f060335..a71c1bb1984 100644 --- a/source/creator/CMakeLists.txt +++ b/source/creator/CMakeLists.txt @@ -26,6 +26,7 @@ setup_libdirs() blender_include_dirs( + ../../intern/clog ../../intern/guardedalloc ../../intern/glew-mx ../blender/blenlib diff --git a/source/creator/creator.c b/source/creator/creator.c index a59a45f885c..962d6720760 100644 --- a/source/creator/creator.c +++ b/source/creator/creator.c @@ -42,6 +42,8 @@ #include "MEM_guardedalloc.h" +#include "CLG_log.h" + #include "DNA_genfile.h" #include "BLI_args.h" @@ -49,6 +51,7 @@ #include "BLI_utildefines.h" #include "BLI_callbacks.h" #include "BLI_string.h" +#include "BLI_system.h" /* mostly init functions */ #include "BKE_appdir.h" @@ -180,6 +183,11 @@ static void callback_main_atexit(void *user_data) #endif } +static void callback_clg_fatal(void *fp) +{ + BLI_system_backtrace(fp); +} + /** \} */ @@ -304,6 +312,10 @@ int main( sdlewInit(); #endif + /* Initialize logging */ + CLG_init(); + CLG_fatal_fn_set(callback_clg_fatal); + C = CTX_create(); #ifdef WITH_PYTHON_MODULE diff --git a/source/creator/creator_args.c b/source/creator/creator_args.c index 25f8d732c58..17fa18916fd 100644 --- a/source/creator/creator_args.c +++ b/source/creator/creator_args.c @@ -30,6 +30,8 @@ #include "MEM_guardedalloc.h" +#include "CLG_log.h" + #ifdef WIN32 # include "BLI_winstuff.h" #endif @@ -529,6 +531,11 @@ static int arg_handle_print_help(int UNUSED(argc), const char **UNUSED(argv), vo BLI_argsPrintArgDoc(ba, "--python-exit-code"); BLI_argsPrintArgDoc(ba, "--addons"); + printf("\n"); + printf("Logging Options:\n"); + BLI_argsPrintArgDoc(ba, "--log"); + BLI_argsPrintArgDoc(ba, "--log-level"); + BLI_argsPrintArgDoc(ba, "--log-file"); printf("\n"); printf("Debug Options:\n"); @@ -704,6 +711,88 @@ static int arg_handle_background_mode_set(int UNUSED(argc), const char **UNUSED( return 0; } +static const char arg_handle_log_level_set_doc[] = +"\n\tSet the logging verbosity level (higher for more details) defaults to 1." +; +static int arg_handle_log_level_set(int argc, const char **argv, void *UNUSED(data)) +{ + const char *arg_id = "--log-level"; + if (argc > 1) { + const char *err_msg = NULL; + if (!parse_int_clamp(argv[1], NULL, 0, INT_MAX, &G.log.level, &err_msg)) { + printf("\nError: %s '%s %s'.\n", err_msg, arg_id, argv[1]); + } + return 1; + } + else { + printf("\nError: '%s' no args given.\n", arg_id); + return 0; + } +} + +static const char arg_handle_log_file_set_doc[] = +"\n\tSet a file to output the log to." +; +static int arg_handle_log_file_set(int argc, const char **argv, void *UNUSED(data)) +{ + const char *arg_id = "--log-file"; + if (argc > 1) { + errno = 0; + FILE *fp = BLI_fopen(argv[1], "w"); + if (fp == NULL) { + const char *err_msg = errno ? strerror(errno) : "unknown"; + printf("\nError: %s '%s %s'.\n", err_msg, arg_id, argv[1]); + } + else { + if (UNLIKELY(G.log.file != NULL)) { + fclose(G.log.file); + } + G.log.file = fp; + CLG_output_set(G.log.file); + } + return 1; + } + else { + printf("\nError: '%s' no args given.\n", arg_id); + return 0; + } +} + +static const char arg_handle_log_set_doc[] = +"\n\tEnable logging categories, taking a single comma separated argument.\n" +"\tMultiple categories can be matched using a '.*' suffix, so '--log \"wm.*\"' logs every kind of window-manager message.\n" +"\tUse \"*\" to log everything." +; +static int arg_handle_log_set(int argc, const char **argv, void *UNUSED(data)) +{ + const char *arg_id = "--log"; + if (argc > 1) { + const char *str_step = argv[1]; + while (*str_step) { + const char *str_step_end = strchr(str_step, ','); + int str_step_len = str_step_end ? (str_step_end - str_step) : strlen(str_step); + + CLG_type_filter(str_step, str_step_len); + + if (str_step_end) { + /* typically only be one, but don't fail on multiple.*/ + while (*str_step_end == ',') { + str_step_end++; + } + str_step = str_step_end; + } + else { + break; + } + } + return 1; + } + else { + printf("\nError: '%s' no args given.\n", arg_id); + return 0; + } +} + static const char arg_handle_debug_mode_set_doc[] = "\n" "\tTurn debugging on.\n" @@ -1827,6 +1916,10 @@ void main_args_setup(bContext *C, bArgs *ba, SYS_SystemHandle *syshandle) BLI_argsAdd(ba, 1, "-a", NULL, CB(arg_handle_playback_mode), NULL); + BLI_argsAdd(ba, 1, NULL, "--log-level", CB(arg_handle_log_level_set), ba); + BLI_argsAdd(ba, 1, NULL, "--log-file", CB(arg_handle_log_file_set), ba); + BLI_argsAdd(ba, 1, NULL, "--log", CB(arg_handle_log_set), ba); + BLI_argsAdd(ba, 1, "-d", "--debug", CB(arg_handle_debug_mode_set), ba); #ifdef WITH_FFMPEG -- cgit v1.2.3