DCCL v4
test.cpp
1 // Copyright 2012-2017:
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 
25 #include <cassert>
26 
27 #include "../../logger.h"
28 
30 inline std::ostream& stream_assert(std::ostream& os)
31 {
32  bool failed_to_short_circuit_logging_statement = false;
33  assert(failed_to_short_circuit_logging_statement);
34  return os;
35 }
36 
37 void info(const std::string& log_message, dccl::logger::Verbosity /*verbosity*/,
38  dccl::logger::Group /*group*/)
39 {
40  printf("%s\n", log_message.c_str());
41 }
42 
43 int main(int /*argc*/, char* /*argv*/ [])
44 {
45  using dccl::dlog;
46  using namespace dccl::logger;
47 
48  std::cout << "attaching info() to DEBUG3+" << std::endl;
49  dlog.connect(DEBUG3_PLUS, &info);
50  dlog.is(DEBUG3) && dlog << "debug3 ok" << std::endl;
51  dlog.is(DEBUG2) && dlog << "debug2 ok" << std::endl;
52  dlog.is(DEBUG1) && dlog << "debug1 ok" << std::endl;
53  dlog.is(INFO) && dlog << "verbose ok" << std::endl;
54  dlog.is(WARN) && dlog << "warn ok" << std::endl;
55  dlog.disconnect(ALL);
56 
57  std::cout << "attaching info() to nothing" << std::endl;
58  dlog.is(DEBUG3) && dlog << stream_assert << std::endl;
59  dlog.is(DEBUG2) && dlog << stream_assert << std::endl;
60  dlog.is(DEBUG1) && dlog << stream_assert << std::endl;
61  dlog.is(INFO) && dlog << stream_assert << std::endl;
62  dlog.is(WARN) && dlog << stream_assert << std::endl;
63 
64  std::cout << "attaching info() to WARN+" << std::endl;
65  dlog.connect(WARN_PLUS, &info);
66  dlog.is(DEBUG3) && dlog << stream_assert << std::endl;
67  dlog.is(DEBUG2) && dlog << stream_assert << std::endl;
68  dlog.is(DEBUG1) && dlog << stream_assert << std::endl;
69  dlog.is(INFO) && dlog << stream_assert << std::endl;
70  dlog.is(WARN) && dlog << "warn ok" << std::endl;
71  dlog.disconnect(ALL);
72 
73  std::cout << "attaching info() to INFO+" << std::endl;
74  dlog.connect(INFO_PLUS, &info);
75  dlog.is(DEBUG3) && dlog << stream_assert << std::endl;
76  dlog.is(DEBUG2) && dlog << stream_assert << std::endl;
77  dlog.is(DEBUG1) && dlog << stream_assert << std::endl;
78  dlog.is(INFO) && dlog << "verbose ok" << std::endl;
79  dlog.is(WARN) && dlog << "warn ok" << std::endl;
80  dlog.disconnect(ALL);
81 
82  std::cout << "attaching info() to DEBUG1+" << std::endl;
83  dlog.connect(DEBUG1_PLUS, &info);
84  dlog.is(DEBUG3) && dlog << stream_assert << std::endl;
85  dlog.is(DEBUG2) && dlog << stream_assert << std::endl;
86  dlog.is(DEBUG1) && dlog << "debug1 ok" << std::endl;
87  dlog.is(INFO) && dlog << "verbose ok" << std::endl;
88  dlog.is(WARN) && dlog << "warn ok" << std::endl;
89  dlog.disconnect(ALL);
90 
91  std::cout << "attaching info() to DEBUG2+" << std::endl;
92  dlog.connect(DEBUG2_PLUS, &info);
93  dlog.is(DEBUG3) && dlog << stream_assert << std::endl;
94  dlog.is(DEBUG2) && dlog << "debug2 ok" << std::endl;
95  dlog.is(DEBUG1) && dlog << "debug1 ok" << std::endl;
96  dlog.is(INFO) && dlog << "verbose ok" << std::endl;
97  dlog.is(WARN) && dlog << "warn ok" << std::endl;
98  dlog.disconnect(ALL);
99 
100  std::cout << "All tests passed." << std::endl;
101 }
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