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