diff --git a/BUILD b/BUILD index a7a79b3c6..4e97efcaf 100644 --- a/BUILD +++ b/BUILD @@ -96,6 +96,7 @@ cc_binary( "src/idl_gen_python.cpp", "src/idl_gen_rust.cpp", "src/idl_gen_text.cpp", + "src/util.cpp", ], includes = [ "grpc/", diff --git a/CMakeLists.txt b/CMakeLists.txt index 119855a95..0640c37b5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -140,6 +140,8 @@ set(FlatBuffers_Sample_BFBS_SRCS set(FlatBuffers_GRPCTest_SRCS include/flatbuffers/flatbuffers.h include/flatbuffers/grpc.h + include/flatbuffers/util.h + src/util.cpp tests/monster_test.grpc.fb.h tests/test_assert.h tests/test_builder.h diff --git a/include/flatbuffers/util.h b/include/flatbuffers/util.h index 4367bbecf..e8c066d5d 100644 --- a/include/flatbuffers/util.h +++ b/include/flatbuffers/util.h @@ -649,6 +649,9 @@ bool SetGlobalTestLocale(const char *locale_name, bool ReadEnvironmentVariable(const char *var_name, std::string *_value = nullptr); +// MSVC specific: Send all assert reports to STDOUT to prevent CI hangs. +void SetupDefaultCRTReportMode(); + } // namespace flatbuffers #endif // FLATBUFFERS_UTIL_H_ diff --git a/src/flatc_main.cpp b/src/flatc_main.cpp index 78e66a6f6..9dfe81ce2 100644 --- a/src/flatc_main.cpp +++ b/src/flatc_main.cpp @@ -15,6 +15,7 @@ */ #include "flatbuffers/flatc.h" +#include "flatbuffers/util.h" static const char *g_program_name = nullptr; @@ -34,6 +35,9 @@ static void Error(const flatbuffers::FlatCompiler *flatc, } int main(int argc, const char *argv[]) { + // Prevent Appveyor-CI hangs. + flatbuffers::SetupDefaultCRTReportMode(); + g_program_name = argv[0]; const flatbuffers::FlatCompiler::Generator generators[] = { diff --git a/src/util.cpp b/src/util.cpp index c1bb1975c..cd44ee72e 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -23,6 +23,7 @@ # ifndef NOMINMAX # define NOMINMAX # endif +# include # include // Must be included before # include # include @@ -235,6 +236,7 @@ bool SetGlobalTestLocale(const char *locale_name, std::string *_value) { if (_value) *_value = std::string(the_locale); return true; } + bool ReadEnvironmentVariable(const char *var_name, std::string *_value) { #ifdef _MSC_VER __pragma(warning(disable : 4996)); // _CRT_SECURE_NO_WARNINGS @@ -245,4 +247,27 @@ bool ReadEnvironmentVariable(const char *var_name, std::string *_value) { return true; } +void SetupDefaultCRTReportMode() { + // clang-format off + + #ifdef _MSC_VER + // By default, send all reports to STDOUT to prevent CI hangs. + // Enable assert report box [Abort|Retry|Ignore] if a debugger is present. + const int dbg_mode = (_CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG) | + (IsDebuggerPresent() ? _CRTDBG_MODE_WNDW : 0); + (void)dbg_mode; // release mode fix + // CrtDebug reports to _CRT_WARN channel. + _CrtSetReportMode(_CRT_WARN, dbg_mode); + _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDOUT); + // The assert from reports to _CRT_ERROR channel + _CrtSetReportMode(_CRT_ERROR, dbg_mode); + _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDOUT); + // Internal CRT assert channel? + _CrtSetReportMode(_CRT_ASSERT, dbg_mode); + _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDOUT); + #endif + + // clang-format on +} + } // namespace flatbuffers diff --git a/tests/test_assert.cpp b/tests/test_assert.cpp index ccbce2353..794ffe7d9 100644 --- a/tests/test_assert.cpp +++ b/tests/test_assert.cpp @@ -41,24 +41,9 @@ void InitTestEngine(TestFailEventListener listener) { setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); - // clang-format off + flatbuffers::SetupDefaultCRTReportMode(); - #ifdef _MSC_VER - // By default, send all reports to STDOUT to prevent CI hangs. - // Enable assert report box [Abort|Retry|Ignore] if a debugger is present. - const int dbg_mode = (_CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG) | - (IsDebuggerPresent() ? _CRTDBG_MODE_WNDW : 0); - (void)dbg_mode; // release mode fix - // CrtDebug reports to _CRT_WARN channel. - _CrtSetReportMode(_CRT_WARN, dbg_mode); - _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDOUT); - // The assert from reports to _CRT_ERROR channel - _CrtSetReportMode(_CRT_ERROR, dbg_mode); - _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDOUT); - // Internal CRT assert channel? - _CrtSetReportMode(_CRT_ASSERT, dbg_mode); - _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDOUT); - #endif + // clang-format off #if defined(FLATBUFFERS_MEMORY_LEAK_TRACKING_MSVC) // For more thorough checking: