blob: aa2f3306dea652fab1c285f78666e3369ce35773 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -08002/**
Alexander Afanasyev667370f2016-08-24 11:54:53 -07003 * Copyright (c) 2013-2016 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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 Afanasyev5fa9e9a2013-12-24 19:45:07 -080020 */
21
Alexander Afanasyeve6c65e22015-01-28 19:56:03 -080022#define BOOST_TEST_MODULE ndn-cxx Unit Tests
Alexander Afanasyev9fae3db2015-12-07 18:04:32 -080023#define BOOST_TEST_DYN_LINK
24#define BOOST_TEST_ALTERNATIVE_INIT_API
25
Alexander Afanasyev667370f2016-08-24 11:54:53 -070026#include <boost/version.hpp>
27
28#if BOOST_VERSION >= 106200
29// Boost.Test v3.3 (Boost 1.62) natively supports multi-logger output
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070030#include "boost-test.hpp"
Alexander Afanasyev667370f2016-08-24 11:54:53 -070031#else
32#define BOOST_TEST_NO_MAIN
33#include "boost-test.hpp"
34
Alexander Afanasyev9fae3db2015-12-07 18:04:32 -080035#include "boost-multi-log-formatter.hpp"
36
37#include <boost/program_options/options_description.hpp>
38#include <boost/program_options/variables_map.hpp>
39#include <boost/program_options/parsers.hpp>
40
41#include <fstream>
42#include <iostream>
43
44static bool
45init_tests()
46{
47 init_unit_test();
48
49 namespace po = boost::program_options;
50 namespace ut = boost::unit_test;
51
52 po::options_description extraOptions;
53 std::string logger;
54 std::string outputFile = "-";
55 extraOptions.add_options()
56 ("log_format2", po::value<std::string>(&logger), "Type of second log formatter: HRF or XML")
57 ("log_sink2", po::value<std::string>(&outputFile)->default_value(outputFile), "Second log sink, - for stdout")
58 ;
59 po::variables_map vm;
60 try {
61 po::store(po::command_line_parser(ut::framework::master_test_suite().argc,
62 ut::framework::master_test_suite().argv)
63 .options(extraOptions)
64 .run(),
65 vm);
66 po::notify(vm);
67 }
68 catch (const std::exception& e) {
69 std::cerr << "ERROR: " << e.what() << "\n"
70 << extraOptions << std::endl;
71 return false;
72 }
73
74 if (vm.count("log_format2") == 0) {
75 // second logger is not configured
76 return true;
77 }
78
79 std::shared_ptr<ut::unit_test_log_formatter> formatter;
80 if (logger == "XML") {
81 formatter = std::make_shared<ut::output::xml_log_formatter>();
82 }
83 else if (logger == "HRF") {
84 formatter = std::make_shared<ut::output::compiler_log_formatter>();
85 }
86 else {
87 std::cerr << "ERROR: only HRF or XML log formatter can be specified" << std::endl;
88 return false;
89 }
90
91 std::shared_ptr<std::ostream> output;
92 if (outputFile == "-") {
93 output = std::shared_ptr<std::ostream>(&std::cout, std::bind([]{}));
94 }
95 else {
96 output = std::make_shared<std::ofstream>(outputFile.c_str());
97 }
98
99 auto multiFormatter = new ut::output::multi_log_formatter;
100 multiFormatter->add(formatter, output);
101 ut::unit_test_log.set_formatter(multiFormatter);
102
103 return true;
104}
105
106int
107main(int argc, char* argv[])
108{
109 return ::boost::unit_test::unit_test_main(&init_tests, argc, argv);
110}
Alexander Afanasyev667370f2016-08-24 11:54:53 -0700111
112#endif // BOOST_VERSION >= 106200