From 8cbb57ee0a84023c711a63a86d76597de9bafd7f Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Fri, 8 Mar 2024 13:27:29 +1100 Subject: [PATCH] CLI: suppress noisy prints when running commands Messages such as "Blender quit" get in the way of custom commands, add G.quiet to suppress noisy "info" prints. --- source/blender/blenkernel/BKE_global.hh | 8 ++++++++ source/blender/blenkernel/intern/blendfile.cc | 16 ++++++++++++---- source/blender/blenkernel/intern/image_save.cc | 4 +++- source/blender/windowmanager/intern/wm_files.cc | 12 +++++++++--- .../blender/windowmanager/intern/wm_init_exit.cc | 8 ++++++-- source/creator/creator_args.cc | 3 +++ 6 files changed, 41 insertions(+), 10 deletions(-) diff --git a/source/blender/blenkernel/BKE_global.hh b/source/blender/blenkernel/BKE_global.hh index 23b3481572d..1abad1e3307 100644 --- a/source/blender/blenkernel/BKE_global.hh +++ b/source/blender/blenkernel/BKE_global.hh @@ -65,6 +65,14 @@ struct Global { */ bool background; + /** + * When true, suppress any non-error print messages such as files saves, loaded, quitting etc. + * This is used so command line tools can control output without unnecessary noise. + * + * \note This should only be used to suppress printing (not reports or other kinds of logging). + */ + bool quiet; + /** * Skip reading the startup file and user preferences. * Also disable saving the preferences on exit (see #G_FLAG_USERPREF_NO_SAVE_ON_EXIT), diff --git a/source/blender/blenkernel/intern/blendfile.cc b/source/blender/blenkernel/intern/blendfile.cc index 9e24706d09c..d35330fba03 100644 --- a/source/blender/blenkernel/intern/blendfile.cc +++ b/source/blender/blenkernel/intern/blendfile.cc @@ -1046,7 +1046,9 @@ BlendFileData *BKE_blendfile_read(const char *filepath, { /* Don't print startup file loading. */ if (params->is_startup == false) { - printf("Read blend: \"%s\"\n", filepath); + if (!G.quiet) { + printf("Read blend: \"%s\"\n", filepath); + } } BlendFileData *bfd = BLO_read_from_file(filepath, eBLOReadSkip(params->skip_flags), reports); @@ -1332,12 +1334,18 @@ bool BKE_blendfile_userdef_write_all(ReportList *reports) /* Also save app-template preferences. */ BLI_path_join(filepath, sizeof(filepath), cfgdir->c_str(), BLENDER_USERPREF_FILE); - printf("Writing userprefs app-template: \"%s\" ", filepath); + if (!G.quiet) { + printf("Writing userprefs app-template: \"%s\" ", filepath); + } if (BKE_blendfile_userdef_write(filepath, reports) != 0) { - printf("ok\n"); + if (!G.quiet) { + printf("ok\n"); + } } else { - printf("fail\n"); + if (!G.quiet) { + printf("fail\n"); + } ok = false; } } diff --git a/source/blender/blenkernel/intern/image_save.cc b/source/blender/blenkernel/intern/image_save.cc index 64d81a87886..0cc7831136c 100644 --- a/source/blender/blenkernel/intern/image_save.cc +++ b/source/blender/blenkernel/intern/image_save.cc @@ -896,7 +896,9 @@ static void image_render_print_save_message(ReportList *reports, { if (ok) { /* no need to report, just some helpful console info */ - printf("Saved: '%s'\n", filepath); + if (!G.quiet) { + printf("Saved: '%s'\n", filepath); + } } else { /* report on error since users will want to know what failed */ diff --git a/source/blender/windowmanager/intern/wm_files.cc b/source/blender/windowmanager/intern/wm_files.cc index 9de3d03646a..9e4ad6562aa 100644 --- a/source/blender/windowmanager/intern/wm_files.cc +++ b/source/blender/windowmanager/intern/wm_files.cc @@ -2330,7 +2330,9 @@ static int wm_homefile_write_exec(bContext *C, wmOperator *op) BLI_path_join(filepath, sizeof(filepath), cfgdir->c_str(), BLENDER_STARTUP_FILE); - printf("Writing homefile: \"%s\" ", filepath); + if (!G.quiet) { + printf("Writing homefile: \"%s\" ", filepath); + } ED_editors_flush_edits(bmain); @@ -2351,11 +2353,15 @@ static int wm_homefile_write_exec(bContext *C, wmOperator *op) BKE_callback_exec_string(bmain, success ? BKE_CB_EVT_SAVE_POST : BKE_CB_EVT_SAVE_POST_FAIL, ""); if (success) { - printf("ok\n"); + if (!G.quiet) { + printf("ok\n"); + } BKE_report(op->reports, RPT_INFO, "Startup file saved"); return OPERATOR_FINISHED; } - printf("fail\n"); + if (!G.quiet) { + printf("fail\n"); + } return OPERATOR_CANCELLED; } diff --git a/source/blender/windowmanager/intern/wm_init_exit.cc b/source/blender/windowmanager/intern/wm_init_exit.cc index 191baa90639..d73054b9928 100644 --- a/source/blender/windowmanager/intern/wm_init_exit.cc +++ b/source/blender/windowmanager/intern/wm_init_exit.cc @@ -483,7 +483,9 @@ void WM_exit_ex(bContext *C, const bool do_python_exit, const bool do_user_exit_ BlendFileWriteParams blend_file_write_params{}; if (BLO_write_file(bmain, filepath, fileflags, &blend_file_write_params, nullptr)) { - printf("Saved session recovery to \"%s\"\n", filepath); + if (!G.quiet) { + printf("Saved session recovery to \"%s\"\n", filepath); + } } } @@ -700,7 +702,9 @@ void WM_exit(bContext *C, const int exit_code) const bool do_user_exit_actions = G.background ? false : (exit_code == EXIT_SUCCESS); WM_exit_ex(C, true, do_user_exit_actions); - printf("\nBlender quit\n"); + if (!G.quiet) { + printf("\nBlender quit\n"); + } exit(exit_code); } diff --git a/source/creator/creator_args.cc b/source/creator/creator_args.cc index 3c2fe4301a7..0c20fda493c 100644 --- a/source/creator/creator_args.cc +++ b/source/creator/creator_args.cc @@ -946,6 +946,9 @@ static int arg_handle_command_set(int argc, const char **argv, void * /*data*/) BLI_assert_unreachable(); } + /* Application "info" messages get in the way of command line output, suppress them. */ + G.quiet = true; + background_mode_set(); app_state.command.argc = argc - 1;