JayBeams  0.1
Another project to have fun coding.
ut_logging.cpp
Go to the documentation of this file.
1 #include <jb/as_hhmmss.hpp>
2 #include <jb/log.hpp>
3 
4 #include <boost/log/sinks/sync_frontend.hpp>
5 #include <boost/log/sinks/text_ostream_backend.hpp>
6 #include <boost/test/unit_test.hpp>
7 #include <sstream>
8 
9 /**
10  * @test Verify that basic logging functions work as expected.
11  */
12 BOOST_AUTO_TEST_CASE(logging_basic) {
15  .enable_file_logging(true)
16  .logfile_basename("ut_logging"));
17 
18  JB_LOG(trace) << "tracing tracing tracing";
19  for (int i = 0; i != 30000; ++i) {
20  JB_LOG(debug) << "i=" << i;
22  }
23  JB_LOG(info) << "testing my logger (" << 1 << ")";
24  JB_LOG(warning) << "testing warnings in my logger";
25  JB_LOG(error) << "here is an error: " << jb::severity_level::warning;
26 
27  std::string foo("more complex expr test");
28  int x = 1;
29  float y = 2.0;
30 
31  auto expensive = [](int n) {
32  int s = 0;
33  for (int i = 0; i != n; ++i) {
34  s += n;
35  }
36  return s;
37  };
38 
40  JB_LOG(trace) << "this never gets to run, and the optimizer "
41  << "should remove it" << expensive(20);
42  JB_LOG(debug) << "this does not run, but the compiler will keep..."
43  << expensive(1000);
44  JB_LOG(notice) << "L3 x=" << x << ", foo=" << foo << ", y=" << y;
45  JB_LOG(warning) << "L3 this";
46 
47  JB_LOG(notice) << "x=" << x << ", foo=" << foo << " y=" << y;
48  JB_LOG(debug) << "x=" << x << ", foo=" << foo << " y=" << y;
49  JB_LOG(error) << "x=" << x << ", foo=" << foo << " y=" << y;
50 
51  std::ostringstream os;
52  auto core = boost::log::core::get();
53  auto backend = boost::make_shared<boost::log::sinks::text_ostream_backend>();
54  backend->add_stream(boost::shared_ptr<std::ostream>(&os, [](void const*) {}));
55  backend->auto_flush(true);
56  typedef boost::log::sinks::synchronous_sink<
57  boost::log::sinks::text_ostream_backend>
58  sink_t;
59  auto sink = boost::make_shared<sink_t>(backend);
60  core->add_sink(sink);
61 
62  JB_LOG(alert) << "this is a log line";
63 
64  BOOST_CHECK_EQUAL(os.str(), "this is a log line\n");
65 
66  core->remove_sink(sink);
67 
69  JB_LOG(info) << "more logging after removing the sunk...";
70 }
71 
72 /**
73  * @test Verify that parsing YAML files to configure logging works as expected.
74  */
75 BOOST_AUTO_TEST_CASE(logging_yaml) {
76  char const contents[] = R"""(# YAML overrides
77 minimum-severity: ERROR
78 minimum-console-severity: NOTICE
79 )""";
80  jb::log::config tested;
81  std::istringstream is(contents);
82  int argc = 0;
83  tested.load_overrides(argc, nullptr, is);
84 
85  BOOST_CHECK_EQUAL(tested.minimum_severity(), jb::severity_level::error);
86  BOOST_CHECK_EQUAL(
88 
89  std::ostringstream os;
90  BOOST_CHECK_NO_THROW(os << tested);
91 }
92 
93 /**
94  * @test Verify that configuration errors are detected.
95  */
96 BOOST_AUTO_TEST_CASE(logging_config_errors) {
97  char const contents[] = R"""(# YAML overrides
98 enable-file-logging: true
99 )""";
100  jb::log::config tested;
101  std::istringstream is(contents);
102  int argc = 0;
103  BOOST_CHECK_THROW(tested.load_overrides(argc, nullptr, is), jb::usage);
104 }
Configuration object for the logging functions.
Definition: log.hpp:29
BOOST_AUTO_TEST_CASE(logging_basic)
Definition: ut_logging.cpp:12
void load_overrides(int &argc, char *argv[], std::string const &filename, char const *environment_variable_name)
Read the configuration file and load the overrides defined therein.
void init(config const &cfg)
Initialize the logging functions using the configuration provided.
Definition: log.cpp:190
jb::config_attribute< config, jb::severity_level > minimum_console_severity
Definition: log.hpp:35
void next_tid()
Definition: log.cpp:186
A simple class to communicate the result of parsing the options.
Definition: usage.hpp:11
jb::config_attribute< config, jb::severity_level > minimum_severity
Definition: log.hpp:34
#define JB_LOG(lvl)
Definition: log.hpp:70
jb::severity_level minimum_severity
Definition: log.cpp:53
bool enable_file_logging
Definition: log.cpp:56