Alexander Afanasyev | 4ffcff2 | 2014-09-02 15:39:20 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
Alexander Afanasyev | 4ffcff2 | 2014-09-02 15:39:20 -0700 | [diff] [blame] | 10 | * |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 11 | * This file is part of NDNS (Named Data Networking Domain Name Service) and is |
| 12 | * based on the code written as part of NFD (Named Data Networking Daemon). |
Alexander Afanasyev | 4ffcff2 | 2014-09-02 15:39:20 -0700 | [diff] [blame] | 13 | * See AUTHORS.md for complete list of NDNS authors and contributors. |
| 14 | * |
| 15 | * NDNS is free software: you can redistribute it and/or modify it under the terms |
| 16 | * of the GNU General Public License as published by the Free Software Foundation, |
| 17 | * either version 3 of the License, or (at your option) any later version. |
| 18 | * |
| 19 | * NDNS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 20 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 21 | * PURPOSE. See the GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License along with |
| 24 | * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 25 | */ |
| 26 | |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 27 | #define BOOST_TEST_DYN_LINK |
| 28 | #define BOOST_TEST_ALTERNATIVE_INIT_API |
Alexander Afanasyev | aa46c27 | 2016-03-09 12:54:12 -0800 | [diff] [blame] | 29 | |
Alexander Afanasyev | c7c9900 | 2015-10-09 17:27:30 -0700 | [diff] [blame] | 30 | #include <boost/version.hpp> |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 31 | |
| 32 | #if BOOST_VERSION >= 106200 |
| 33 | // Boost.Test v3.3 (Boost 1.62) natively supports multi-logger output |
| 34 | #include "boost-test.hpp" |
| 35 | #else |
| 36 | #define BOOST_TEST_NO_MAIN |
| 37 | #include "boost-test.hpp" |
| 38 | |
| 39 | #include "boost-multi-log-formatter.hpp" |
| 40 | |
| 41 | #include <boost/program_options/options_description.hpp> |
| 42 | #include <boost/program_options/variables_map.hpp> |
| 43 | #include <boost/program_options/parsers.hpp> |
Alexander Afanasyev | 59a42ec | 2014-09-02 18:21:16 -0700 | [diff] [blame] | 44 | |
Alexander Afanasyev | aa46c27 | 2016-03-09 12:54:12 -0800 | [diff] [blame] | 45 | #include <fstream> |
| 46 | #include <iostream> |
Alexander Afanasyev | 59a42ec | 2014-09-02 18:21:16 -0700 | [diff] [blame] | 47 | |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 48 | static bool |
| 49 | init_tests() |
Alexander Afanasyev | 59a42ec | 2014-09-02 18:21:16 -0700 | [diff] [blame] | 50 | { |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 51 | init_unit_test(); |
Alexander Afanasyev | aa46c27 | 2016-03-09 12:54:12 -0800 | [diff] [blame] | 52 | |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 53 | namespace po = boost::program_options; |
| 54 | namespace ut = boost::unit_test; |
Alexander Afanasyev | aa46c27 | 2016-03-09 12:54:12 -0800 | [diff] [blame] | 55 | |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 56 | po::options_description extraOptions; |
| 57 | std::string logger; |
| 58 | std::string outputFile = "-"; |
| 59 | extraOptions.add_options() |
| 60 | ("log_format2", po::value<std::string>(&logger), "Type of second log formatter: HRF or XML") |
| 61 | ("log_sink2", po::value<std::string>(&outputFile)->default_value(outputFile), "Second log sink, - for stdout") |
| 62 | ; |
| 63 | po::variables_map vm; |
| 64 | try { |
| 65 | po::store(po::command_line_parser(ut::framework::master_test_suite().argc, |
| 66 | ut::framework::master_test_suite().argv) |
| 67 | .options(extraOptions) |
| 68 | .run(), |
| 69 | vm); |
| 70 | po::notify(vm); |
| 71 | } |
| 72 | catch (const std::exception& e) { |
| 73 | std::cerr << "ERROR: " << e.what() << "\n" |
| 74 | << extraOptions << std::endl; |
| 75 | return false; |
Alexander Afanasyev | 59a42ec | 2014-09-02 18:21:16 -0700 | [diff] [blame] | 76 | } |
Alexander Afanasyev | aa46c27 | 2016-03-09 12:54:12 -0800 | [diff] [blame] | 77 | |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 78 | if (vm.count("log_format2") == 0) { |
| 79 | // second logger is not configured |
| 80 | return true; |
Alexander Afanasyev | aa46c27 | 2016-03-09 12:54:12 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 83 | std::shared_ptr<ut::unit_test_log_formatter> formatter; |
| 84 | if (logger == "XML") { |
| 85 | formatter = std::make_shared<ut::output::xml_log_formatter>(); |
| 86 | } |
| 87 | else if (logger == "HRF") { |
| 88 | formatter = std::make_shared<ut::output::compiler_log_formatter>(); |
| 89 | } |
| 90 | else { |
| 91 | std::cerr << "ERROR: only HRF or XML log formatter can be specified" << std::endl; |
| 92 | return false; |
| 93 | } |
Alexander Afanasyev | 59a42ec | 2014-09-02 18:21:16 -0700 | [diff] [blame] | 94 | |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 95 | std::shared_ptr<std::ostream> output; |
| 96 | if (outputFile == "-") { |
| 97 | output = std::shared_ptr<std::ostream>(&std::cout, std::bind([]{})); |
| 98 | } |
| 99 | else { |
| 100 | output = std::make_shared<std::ofstream>(outputFile.c_str()); |
| 101 | } |
Alexander Afanasyev | 59a42ec | 2014-09-02 18:21:16 -0700 | [diff] [blame] | 102 | |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 103 | auto multiFormatter = new ut::output::multi_log_formatter; |
| 104 | multiFormatter->add(formatter, output); |
| 105 | ut::unit_test_log.set_formatter(multiFormatter); |
| 106 | |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | int |
| 111 | main(int argc, char* argv[]) |
| 112 | { |
| 113 | return ::boost::unit_test::unit_test_main(&init_tests, argc, argv); |
| 114 | } |
| 115 | |
| 116 | #endif // BOOST_VERSION >= 106200 |