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