cpp_main.ipp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // (C) Copyright Gennadiy Rozental 2001.
  2. // (C) Copyright Beman Dawes 1995-2001.
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // See http://www.boost.org/libs/test for the library home page.
  7. //
  8. // File : $RCSfile$
  9. //
  10. // Version : $Revision$
  11. //
  12. // Description : main function implementation for Program Executon Monitor
  13. // ***************************************************************************
  14. #ifndef BOOST_TEST_CPP_MAIN_IPP_012205GER
  15. #define BOOST_TEST_CPP_MAIN_IPP_012205GER
  16. // Boost.Test
  17. #include <boost/test/execution_monitor.hpp>
  18. #include <boost/test/detail/config.hpp>
  19. #include <boost/test/utils/basic_cstring/io.hpp>
  20. // Boost
  21. #include <boost/cstdlib.hpp> // for exit codes
  22. #include <boost/config.hpp> // for workarounds
  23. // STL
  24. #include <iostream>
  25. #include <cstdlib> // std::getenv
  26. #include <cstring> // std::strerror
  27. #include <boost/test/detail/suppress_warnings.hpp>
  28. //____________________________________________________________________________//
  29. #ifdef BOOST_NO_STDC_NAMESPACE
  30. namespace std { using ::getenv; using ::strerror; }
  31. #endif
  32. namespace {
  33. struct cpp_main_caller {
  34. cpp_main_caller( int (*cpp_main_func)( int argc, char* argv[] ), int argc, char** argv )
  35. : m_cpp_main_func( cpp_main_func )
  36. , m_argc( argc )
  37. , m_argv( argv ) {}
  38. int operator()() { return (*m_cpp_main_func)( m_argc, m_argv ); }
  39. private:
  40. // Data members
  41. int (*m_cpp_main_func)( int argc, char* argv[] );
  42. int m_argc;
  43. char** m_argv;
  44. };
  45. } // local namespace
  46. // ************************************************************************** //
  47. // ************** prg_exec_monitor_main ************** //
  48. // ************************************************************************** //
  49. namespace boost {
  50. int BOOST_TEST_DECL
  51. prg_exec_monitor_main( int (*cpp_main)( int argc, char* argv[] ), int argc, char* argv[] )
  52. {
  53. int result = 0;
  54. BOOST_TEST_I_TRY {
  55. boost::unit_test::const_string p( std::getenv( "BOOST_TEST_CATCH_SYSTEM_ERRORS" ) );
  56. ::boost::execution_monitor ex_mon;
  57. ex_mon.p_catch_system_errors.value = p != "no";
  58. result = ex_mon.execute( cpp_main_caller( cpp_main, argc, argv ) );
  59. if( result == 0 )
  60. result = ::boost::exit_success;
  61. else if( result != ::boost::exit_success ) {
  62. std::cout << "\n**** error return code: " << result << std::endl;
  63. result = ::boost::exit_failure;
  64. }
  65. }
  66. BOOST_TEST_I_CATCH( ::boost::execution_exception, exex ) {
  67. std::cout << "\n**** exception(" << exex.code() << "): " << exex.what() << std::endl;
  68. result = ::boost::exit_exception_failure;
  69. }
  70. BOOST_TEST_I_CATCH( ::boost::system_error, ex ) {
  71. std::cout << "\n**** failed to initialize execution monitor."
  72. << "\n**** expression at fault: " << ex.p_failed_exp
  73. << "\n**** error(" << ex.p_errno << "): " << std::strerror( ex.p_errno ) << std::endl;
  74. result = ::boost::exit_exception_failure;
  75. }
  76. if( result != ::boost::exit_success ) {
  77. std::cerr << "******** errors detected; see standard output for details ********" << std::endl;
  78. }
  79. else {
  80. // Some prefer a confirming message when all is well, while others don't
  81. // like the clutter. Use an environment variable to avoid command
  82. // line argument modifications; for use in production programs
  83. // that's a no-no in some organizations.
  84. ::boost::unit_test::const_string p( std::getenv( "BOOST_PRG_MON_CONFIRM" ) );
  85. if( p != "no" ) {
  86. std::cerr << std::flush << "no errors detected" << std::endl;
  87. }
  88. }
  89. return result;
  90. }
  91. } // namespace boost
  92. #if !defined(BOOST_TEST_DYN_LINK) && !defined(BOOST_TEST_NO_MAIN)
  93. // ************************************************************************** //
  94. // ************** main function for tests using lib ************** //
  95. // ************************************************************************** //
  96. int cpp_main( int argc, char* argv[] ); // prototype for user's cpp_main()
  97. int BOOST_TEST_CALL_DECL
  98. main( int argc, char* argv[] )
  99. {
  100. return ::boost::prg_exec_monitor_main( &cpp_main, argc, argv );
  101. }
  102. //____________________________________________________________________________//
  103. #endif // !BOOST_TEST_DYN_LINK && !BOOST_TEST_NO_MAIN
  104. #include <boost/test/detail/enable_warnings.hpp>
  105. #endif // BOOST_TEST_CPP_MAIN_IPP_012205GER