diff --git a/src/unit_tests/unit_test_main.cc b/src/unit_tests/unit_test_main.cc index cca0f4281..11bfc702d 100644 --- a/src/unit_tests/unit_test_main.cc +++ b/src/unit_tests/unit_test_main.cc @@ -544,9 +544,11 @@ main_cont (int &argc, char **argv) bool debug_mode = false; bool continue_flag = false; int repeat = 1; + std::string output; tl::CommandLineOptions cmd; cmd << tl::arg ("-a", &xml_format, "Provide XML output format (JUnit format)") + << tl::arg ("-o=log", &output, "Sends output to the given file") << tl::arg ("-l", &list_tests, "Lists tests and exits") << tl::arg ("-e", &editable, "Uses editable mode") << tl::arg ("-ne", &non_editable, "Uses non-editable mode") @@ -596,8 +598,18 @@ main_cont (int &argc, char **argv) tl::set_continue_flag (continue_flag); tl::set_debug_mode (debug_mode); + FILE *output_file = 0; + try { + if (! output.empty ()) { + output_file = fopen (output.c_str (), "w"); + if (! output_file) { + throw tl::Exception (std::string ("Unable to open log file for writing :") + output); + } + console.send_to (output_file); + } + ut::ctrl << ""; ut::ctrl << ""; @@ -652,8 +664,20 @@ main_cont (int &argc, char **argv) ut::ctrl << ""; + if (output_file) { + console.send_to (stdout); + fclose (output_file); + } + } catch (...) { + ut::ctrl << ""; + + if (output_file) { + console.send_to (stdout); + fclose (output_file); + } + throw; } diff --git a/src/unit_tests/utTestConsole.cc b/src/unit_tests/utTestConsole.cc index b7019a186..7726bc948 100644 --- a/src/unit_tests/utTestConsole.cc +++ b/src/unit_tests/utTestConsole.cc @@ -197,10 +197,25 @@ TestConsole::TestConsole (FILE *file) { ms_instance = this; + prepare_file (); + redirect (); +} + +TestConsole::~TestConsole () +{ + restore (); + + if (ms_instance == this) { + ms_instance = 0; + } +} + +void TestConsole::prepare_file () +{ #if defined(_MSC_VER) m_file_is_tty = false; #else - m_file_is_tty = isatty (fileno (file)); + m_file_is_tty = isatty (fileno (m_file)); #endif #if !defined(_WIN32) @@ -211,16 +226,15 @@ TestConsole::TestConsole (FILE *file) m_rows = std::max (0, (int) ws.ws_row); } #endif - - redirect (); } -TestConsole::~TestConsole () +void +TestConsole::send_to (FILE *file) { - restore (); - - if (ms_instance == this) { - ms_instance = 0; + if (file != m_file) { + flush (); + m_file = file; + prepare_file (); } } diff --git a/src/unit_tests/utTestConsole.h b/src/unit_tests/utTestConsole.h index 6ee323947..46a3dff60 100644 --- a/src/unit_tests/utTestConsole.h +++ b/src/unit_tests/utTestConsole.h @@ -52,6 +52,8 @@ public: TestConsole (FILE *file); ~TestConsole (); + void send_to (FILE *file); + void write_str (const char *text, output_stream os); void raw_write (const char *text); virtual void flush (); @@ -85,6 +87,7 @@ private: void redirect (); void restore (); + void prepare_file (); }; }