blob: cb54695a58ef995b72a811525aab87e2888686cc [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento7e780642018-11-24 15:51:34 -05002/*
3 * Copyright (c) 2013-2018 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
Davide Pesavento7e780642018-11-24 15:51:34 -050022#define BOOST_TEST_MODULE ndn-cxx Unit Tests
Alexander Afanasyev9fae3db2015-12-07 18:04:32 -080023
Alexander Afanasyev667370f2016-08-24 11:54:53 -070024#include <boost/version.hpp>
25
26#if BOOST_VERSION >= 106200
27// Boost.Test v3.3 (Boost 1.62) natively supports multi-logger output
Davide Pesavento7e780642018-11-24 15:51:34 -050028#include "tests/boost-test.hpp"
Alexander Afanasyev667370f2016-08-24 11:54:53 -070029#else
Davide Pesavento7e780642018-11-24 15:51:34 -050030#define BOOST_TEST_ALTERNATIVE_INIT_API
Alexander Afanasyev667370f2016-08-24 11:54:53 -070031#define BOOST_TEST_NO_MAIN
Davide Pesavento7e780642018-11-24 15:51:34 -050032#include "tests/boost-test.hpp"
33#include "tests/unit/boost-multi-log-formatter.hpp"
Alexander Afanasyev9fae3db2015-12-07 18:04:32 -080034
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
42static bool
43init_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
104int
105main(int argc, char* argv[])
106{
107 return ::boost::unit_test::unit_test_main(&init_tests, argc, argv);
108}
Alexander Afanasyev667370f2016-08-24 11:54:53 -0700109
110#endif // BOOST_VERSION >= 106200