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