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