DCCL v4
test.cpp
1 // Copyright 2012-2023:
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 // Community contributors (see AUTHORS file)
5 // File authors:
6 // Toby Schneider <toby@gobysoft.org>
7 //
8 //
9 // This file is part of the Dynamic Compact Control Language Library
10 // ("DCCL").
11 //
12 // DCCL is free software: you can redistribute it and/or modify
13 // it under the terms of the GNU Lesser General Public License as published by
14 // the Free Software Foundation, either version 2.1 of the License, or
15 // (at your option) any later version.
16 //
17 // DCCL is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU Lesser General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public License
23 // along with DCCL. If not, see <http://www.gnu.org/licenses/>.
24 #include <cassert>
25 
26 #include "../../logger.h"
27 
29 inline std::ostream& stream_assert(std::ostream& os)
30 {
31  bool failed_to_short_circuit_logging_statement = false;
32  assert(failed_to_short_circuit_logging_statement);
33  return os;
34 }
35 
36 void info(const std::string& log_message, dccl::logger::Verbosity /*verbosity*/,
37  dccl::logger::Group /*group*/)
38 {
39  printf("%s\n", log_message.c_str());
40 }
41 
42 int main(int /*argc*/, char* /*argv*/ [])
43 {
44  using dccl::dlog;
45  using namespace dccl::logger;
46 
47  std::cout << "attaching info() to DEBUG3+" << std::endl;
48  dlog.connect(DEBUG3_PLUS, &info);
49  dlog.is(DEBUG3) && dlog << "debug3 ok" << std::endl;
50  dlog.is(DEBUG2) && dlog << "debug2 ok" << std::endl;
51  dlog.is(DEBUG1) && dlog << "debug1 ok" << std::endl;
52  dlog.is(INFO) && dlog << "verbose ok" << std::endl;
53  dlog.is(WARN) && dlog << "warn ok" << std::endl;
54  dlog.disconnect(ALL);
55 
56  std::cout << "attaching info() to nothing" << std::endl;
57  dlog.is(DEBUG3) && dlog << stream_assert << std::endl;
58  dlog.is(DEBUG2) && dlog << stream_assert << std::endl;
59  dlog.is(DEBUG1) && dlog << stream_assert << std::endl;
60  dlog.is(INFO) && dlog << stream_assert << std::endl;
61  dlog.is(WARN) && dlog << stream_assert << std::endl;
62 
63  std::cout << "attaching info() to WARN+" << std::endl;
64  dlog.connect(WARN_PLUS, &info);
65  dlog.is(DEBUG3) && dlog << stream_assert << std::endl;
66  dlog.is(DEBUG2) && dlog << stream_assert << std::endl;
67  dlog.is(DEBUG1) && dlog << stream_assert << std::endl;
68  dlog.is(INFO) && dlog << stream_assert << std::endl;
69  dlog.is(WARN) && dlog << "warn ok" << std::endl;
70  dlog.disconnect(ALL);
71 
72  std::cout << "attaching info() to INFO+" << std::endl;
73  dlog.connect(INFO_PLUS, &info);
74  dlog.is(DEBUG3) && dlog << stream_assert << std::endl;
75  dlog.is(DEBUG2) && dlog << stream_assert << std::endl;
76  dlog.is(DEBUG1) && dlog << stream_assert << std::endl;
77  dlog.is(INFO) && dlog << "verbose ok" << std::endl;
78  dlog.is(WARN) && dlog << "warn ok" << std::endl;
79  dlog.disconnect(ALL);
80 
81  std::cout << "attaching info() to DEBUG1+" << std::endl;
82  dlog.connect(DEBUG1_PLUS, &info);
83  dlog.is(DEBUG3) && dlog << stream_assert << std::endl;
84  dlog.is(DEBUG2) && dlog << stream_assert << std::endl;
85  dlog.is(DEBUG1) && dlog << "debug1 ok" << std::endl;
86  dlog.is(INFO) && dlog << "verbose ok" << std::endl;
87  dlog.is(WARN) && dlog << "warn ok" << std::endl;
88  dlog.disconnect(ALL);
89 
90  std::cout << "attaching info() to DEBUG2+" << std::endl;
91  dlog.connect(DEBUG2_PLUS, &info);
92  dlog.is(DEBUG3) && dlog << stream_assert << std::endl;
93  dlog.is(DEBUG2) && dlog << "debug2 ok" << std::endl;
94  dlog.is(DEBUG1) && dlog << "debug1 ok" << std::endl;
95  dlog.is(INFO) && dlog << "verbose ok" << std::endl;
96  dlog.is(WARN) && dlog << "warn ok" << std::endl;
97  dlog.disconnect(ALL);
98 
99  std::cout << "All tests passed." << std::endl;
100 }
dccl::Logger::is
bool is(logger::Verbosity verbosity, logger::Group group=logger::GENERAL)
Indicates the verbosity of the Logger until the next std::flush or std::endl. The boolean return is u...
Definition: logger.h:192
dccl::Logger::connect
void connect(int verbosity_mask, Slot slot)
Connect the output of one or more given verbosities to a slot (function pointer or similar)
Definition: logger.h:214
dccl::Logger::disconnect
void disconnect(int verbosity_mask)
Disconnect all slots for one or more given verbosities.
Definition: logger.h:250