Goby v2
test.cpp
1 // Copyright 2009-2018 Toby Schneider (http://gobysoft.org/index.wt/people/toby)
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 //
5 //
6 // This file is part of the Goby Underwater Autonomy Project Binaries
7 // ("The Goby Binaries").
8 //
9 // The Goby Binaries are free software: you can redistribute them and/or modify
10 // them under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // The Goby Binaries are distributed in the hope that they will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
21 
22 #include "goby/common/logger.h"
23 #include <cassert>
24 
25 #include <sstream>
26 
27 using goby::glog;
28 
30 inline std::ostream& stream_assert(std::ostream& os) { assert(false); }
31 
32 using namespace goby::common::logger;
33 
34 struct A
35 {
36  A() : i(0) {}
37  A(int i) : i(i) {}
38 
39  int i;
40 };
41 
42 std::ostream& operator<<(std::ostream& out, const A& a) { return out << a.i; }
43 
44 void spew(int n, int m, int run)
45 {
46  switch (run)
47  {
48  case 0:
49  glog.is(VERBOSE) && glog << "Spew 1: " << std::endl;
50  for (int i = 0; i < n; i++) glog.is(VERBOSE) && glog << m << " " << i << std::endl;
51  break;
52 
53  case 1:
54  glog.is(VERBOSE) && glog << "Spew 2: " << std::endl;
55  for (int i = 0; i < n; i++) glog.is(VERBOSE) && glog << m << " " << A(i) << std::endl;
56  break;
57 
58  case 2:
59  glog.is(VERBOSE) && glog << "Spew 3: " << std::endl;
60  for (int i = 0; i < n; i++) glog << m << " " << i << std::endl;
61  break;
62 
63  case 3:
64  glog.is(VERBOSE) && glog << "Spew 4: " << std::endl;
65  for (int i = 0; i < n; i++) glog << m << " " << i;
66  glog << std::endl;
67  break;
68 
69  case 4:
70  glog.is(VERBOSE) && glog << "Spew 5: " << std::endl;
71  for (int i = 0; i < n; i++)
72  glog.is(VERBOSE) && glog << m << " " << i << std::endl << i << std::endl;
73  break;
74  }
75 }
76 
77 int main()
78 {
79  glog.set_name("test");
80 
81  std::cout << "attaching std::cout to QUIET" << std::endl;
82  glog.add_stream(QUIET, &std::cout);
83  glog.is(DEBUG3) && glog << stream_assert << std::endl;
84  glog.is(DEBUG2) && glog << stream_assert << std::endl;
85  glog.is(DEBUG1) && glog << stream_assert << std::endl;
86  glog.is(VERBOSE) && glog << stream_assert << std::endl;
87  glog.is(WARN) && glog << stream_assert << std::endl;
88 
89  std::cout << "attaching std::cout to WARN" << std::endl;
90  glog.add_stream(WARN, &std::cout);
91  glog.is(DEBUG3) && glog << stream_assert << std::endl;
92  glog.is(DEBUG2) && glog << stream_assert << std::endl;
93  glog.is(DEBUG1) && glog << stream_assert << std::endl;
94  glog.is(VERBOSE) && glog << stream_assert << std::endl;
95  glog.is(WARN) && glog << "warn ok" << std::endl;
96 
97  std::cout << "attaching std::cout to VERBOSE" << std::endl;
98  glog.add_stream(VERBOSE, &std::cout);
99  glog.is(DEBUG3) && glog << stream_assert << std::endl;
100  glog.is(DEBUG2) && glog << stream_assert << std::endl;
101  glog.is(DEBUG1) && glog << stream_assert << std::endl;
102  glog.is(VERBOSE) && glog << "verbose ok" << std::endl;
103  glog.is(WARN) && glog << "warn ok" << std::endl;
104 
105  std::cout << "checking locking ... " << std::endl;
106  glog.set_lock_action(goby::common::logger_lock::lock);
107  glog.is(VERBOSE) && glog << "lock ok" << std::endl;
108  glog.is(VERBOSE) && glog << "unlock ok" << std::endl;
109  // glog << "lock ok" << std::endl;
110  // glog << std::string("lock ok") << std::endl;
111 
112  // A a;
113  // a.i = 10;
114  // glog << "Testing overloaded operator<<" << std::endl;
115  // glog << a << std::endl;
116 
117  glog.is(DEBUG3) && glog << stream_assert << std::endl;
118 
119  for (int i = 0; i < 2; ++i)
120  {
121  boost::thread t1(boost::bind(spew, 1000, 1, i));
122  boost::thread t2(boost::bind(spew, 1000, 2, i));
123  t1.join();
124  t2.join();
125  }
126 
127  glog.set_lock_action(goby::common::logger_lock::none);
128 
129  std::cout << "attaching std::cout to DEBUG1" << std::endl;
130  glog.add_stream(DEBUG1, &std::cout);
131  glog.is(DEBUG3) && glog << stream_assert << std::endl;
132  glog.is(DEBUG2) && glog << stream_assert << std::endl;
133  glog.is(DEBUG1) && glog << "debug1 ok" << std::endl;
134  glog.is(VERBOSE) && glog << "verbose ok" << std::endl;
135  glog.is(WARN) && glog << "warn ok" << std::endl;
136 
137  std::cout << "attaching std::cout to DEBUG2" << std::endl;
138  glog.add_stream(DEBUG2, &std::cout);
139  glog.is(DEBUG3) && glog << stream_assert << std::endl;
140  glog.is(DEBUG2) && glog << "debug2 ok" << std::endl;
141  glog.is(DEBUG1) && glog << "debug1 ok" << std::endl;
142  glog.is(VERBOSE) && glog << "verbose ok" << std::endl;
143  glog.is(WARN) && glog << "warn ok" << std::endl;
144 
145  std::cout << "attaching std::cout to DEBUG3" << std::endl;
146  glog.add_stream(DEBUG3, &std::cout);
147  glog.is(DEBUG3) && glog << "debug3 ok" << std::endl;
148  glog.is(DEBUG2) && glog << "debug2 ok" << std::endl;
149  glog.is(DEBUG1) && glog << "debug1 ok" << std::endl;
150  glog.is(VERBOSE) && glog << "verbose ok" << std::endl;
151  glog.is(WARN) && glog << "warn ok" << std::endl;
152 
153  std::stringstream ss1;
154  std::cout << "attaching stringstream to VERBOSE" << std::endl;
155  glog.add_stream(VERBOSE, &ss1);
156  glog.is(DEBUG3) && glog << "debug3 ok" << std::endl;
157  glog.is(DEBUG2) && glog << "debug2 ok" << std::endl;
158  glog.is(DEBUG1) && glog << "debug1 ok" << std::endl;
159  glog.is(VERBOSE) && glog << "verbose ok" << std::endl;
160  glog.is(WARN) && glog << "warn ok" << std::endl;
161 
162  std::cout << "ss1: \n" << ss1.rdbuf();
163 
164  glog.add_group("test1", goby::common::Colors::lt_green, "Test 1");
165  glog.add_group("test2", goby::common::Colors::lt_green, "Test 2");
166 
167  glog << group("test1") << "test1 group ok" << std::endl;
168  glog.is(WARN) && glog << group("test2") << "test2 group warning ok" << std::endl;
169 
170  std::cout << "All tests passed." << std::endl;
171  return 0;
172 }
void set_name(const std::string &s)
Set the name of the application that the logger is serving.
Definition: flex_ostream.h:67
Definition: test_a.pb.h:42
int run(int argc, char *argv[], Config *cfg)
Run a Goby application derived from MinimalApplicationBase. blocks caller until MinimalApplicationBas...
void add_group(const std::string &name, Colors::Color color=Colors::nocolor, const std::string &description="")
Add another group to the logger. A group provides related manipulator for categorizing log messages...
std::ostream & operator<<(std::ostream &out, const google::protobuf::Message &msg)
provides stream output operator for Google Protocol Buffers Message
Definition: core_helpers.h:49
common::FlexOstream glog
Access the Goby logger through this object.
void add_stream(logger::Verbosity verbosity=logger::VERBOSE, std::ostream *os=0)
Attach a stream object (e.g. std::cout, std::ofstream, ...) to the logger with desired verbosity...
Definition: flex_ostream.h:96