Skip to content

Commit edcfcaf

Browse files
committed
Write wsrep-lib log from unit tests into file
Write wsrep-lib logging facility output from unit tests into file. The argument --wsrep-log-file can be controlled where the log from wsrep-lib is written during unit tests. The default is 'wsrep-lib_test.log' in the working directory. In order to get the log written into stdout, use empty value, i.e. --wsrep-log-file=''. Made travis test run a bit more verbose.
1 parent 4285ff9 commit edcfcaf

File tree

2 files changed

+82
-3
lines changed

2 files changed

+82
-3
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,5 +245,5 @@ script:
245245
-DWSREP_LIB_WITH_DBSIM:BOOL=${DBSIM}
246246
-DWSREP_LIB_WITH_ASAN:BOOL=${ASAN}
247247
- make VERBOSE=1 -j 4
248-
- make test
248+
- make test ARGS=--verbose
249249

test/wsrep-lib_test.cpp

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2018 Codership Oy <[email protected]>
2+
* Copyright (C) 2018-2019 Codership Oy <[email protected]>
33
*
44
* This file is part of wsrep-lib.
55
*
@@ -17,6 +17,85 @@
1717
* along with wsrep-lib. If not, see <https://www.gnu.org/licenses/>.
1818
*/
1919

20-
#define BOOST_TEST_MODULE wsrep_test
20+
/** @file wsrep-lib_test.cpp
21+
*
22+
* Run wsrep-lib unit tests.
23+
*
24+
* Commandline arguments:
25+
*
26+
* --wsrep-log-file=<file> Write log from wsrep-lib logging facility
27+
* into <file>. If <file> is left empty, the
28+
* log is written into stdout.
29+
*/
30+
31+
#include "wsrep/logger.hpp"
32+
#include <fstream>
33+
34+
#define BOOST_TEST_ALTERNATIVE_INIT_API
2135
#include <boost/test/included/unit_test.hpp>
2236

37+
// Log file to write messages logged via wsrep-lib logging facility.
38+
static std::string log_file_name("wsrep-lib_test.log");
39+
static std::ofstream log_file;
40+
41+
static void log_fn(wsrep::log::level level,
42+
const char* msg)
43+
{
44+
log_file << wsrep::log::to_c_string(level) << ": " << msg << std::endl;
45+
}
46+
47+
static bool parse_arg(const std::string& arg)
48+
{
49+
const std::string delim("=");
50+
auto delim_pos(arg.find(delim));
51+
const auto parm(arg.substr(0, delim_pos));
52+
std::string val;
53+
if (delim_pos != std::string::npos)
54+
{
55+
val = arg.substr(delim_pos + 1);
56+
}
57+
58+
if (parm == "--wsrep-log-file")
59+
{
60+
log_file_name = val;
61+
}
62+
else
63+
{
64+
std::cerr << "Error: Unknown argument " << arg << std::endl;
65+
return false;
66+
}
67+
return true;
68+
}
69+
70+
static bool setup_env(int argc, char* argv[])
71+
{
72+
for (int i(1); i < argc; ++i)
73+
{
74+
if (parse_arg(argv[i]) == false)
75+
{
76+
return false;
77+
}
78+
}
79+
80+
if (log_file_name.size())
81+
{
82+
log_file.open(log_file_name);
83+
if (!log_file)
84+
{
85+
int err(errno);
86+
std::cerr << "Failed to open '" << log_file_name
87+
<< "': '" << ::strerror(err) << "'" << std::endl;
88+
return false;
89+
}
90+
std::cout << "Writing wsrep-lib log into '" << log_file_name << "'"
91+
<< std::endl;
92+
wsrep::log::logger_fn(log_fn);
93+
}
94+
return true;
95+
}
96+
97+
bool init_unit_test()
98+
{
99+
return setup_env(boost::unit_test::framework::master_test_suite().argc,
100+
boost::unit_test::framework::master_test_suite().argv);
101+
}

0 commit comments

Comments
 (0)