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