Goby v2
flexostream_simple.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 <boost/asio/deadline_timer.hpp> // for deadline_timer
24 #include <fstream>
25 
27 using namespace goby::common::tcolor; // red, blue, etc.
28 using namespace goby::common::logger; // VERBOSE, DEBUG, WARN, etc.
29 using goby::glog;
30 
31 void output();
32 
33 int main(int argc, char* argv[])
34 {
35  if (argc < 2)
36  {
37  std::cout << "usage: flexostream quiet|warn|verbose|debug|gui [file.txt]" << std::endl;
38  return 1;
39  }
40 
41  // write our name with each log entry
42  goby::glog.set_name(argv[0]);
43 
44  // set colors and descriptions for the groups
45  goby::glog.add_group("a", Colors::green, "group a");
46  goby::glog.add_group("b", Colors::magenta, "group b");
47  goby::glog.add_group("c", Colors::blue, "group c");
48 
49  std::string verbosity = argv[1];
50 
51  std::ofstream fout;
52  if (argc == 3)
53  {
54  fout.open(argv[2]);
55  if (fout.is_open())
56  {
57  goby::glog.add_stream(goby::common::logger::DEBUG1, &fout);
58  }
59  else
60  {
61  std::cerr << "Could not open " << argv[2] << " for writing!" << std::endl;
62  return 1;
63  }
64  }
65 
66  if (verbosity == "quiet")
67  {
68  std::cout << "--- testing quiet ---" << std::endl;
69  // add a stream with the quiet setting
70  goby::glog.add_stream(goby::common::logger::QUIET, &std::cout);
71  output();
72  }
73  else if (verbosity == "warn")
74  {
75  std::cout << "--- testing warn ---" << std::endl;
76  // add a stream with the quiet setting
77  goby::glog.add_stream(goby::common::logger::WARN, &std::cout);
78  output();
79  }
80  else if (verbosity == "verbose")
81  {
82  std::cout << "--- testing verbose ---" << std::endl;
83  // add a stream with the quiet setting
84  goby::glog.add_stream(goby::common::logger::VERBOSE, &std::cout);
85  output();
86  }
87  else if (verbosity == "debug")
88  {
89  std::cout << "--- testing debug 1---" << std::endl;
90  // add a stream with the quiet setting
91  goby::glog.add_stream(goby::common::logger::DEBUG1, &std::cout);
92  output();
93  }
94  else if (verbosity == "gui")
95  {
96  std::cout << "--- testing gui ---" << std::endl;
97  // add a stream with the quiet setting
98  goby::glog.add_stream(goby::common::logger::VERBOSE, &std::cout);
99  goby::glog.enable_gui();
100  output();
101 
102  const int CLOSE_TIME = 60;
103  goby::glog << warn << "closing in " << CLOSE_TIME << " seconds!" << std::endl;
104 
105  // `sleep` is not thread safe, so we use a boost::asio::deadline_timer
106  boost::asio::io_service io_service;
107  boost::asio::deadline_timer timer(io_service);
108  timer.expires_from_now(boost::posix_time::seconds(CLOSE_TIME));
109  timer.wait();
110  }
111  else
112  {
113  std::cout << "invalid verbosity setting: " << verbosity << std::endl;
114  return 1;
115  }
116 
117  if (fout.is_open())
118  fout.close();
119  return 0;
120 }
121 
122 void output()
123 {
124  glog.is(WARN) && glog << "this is warning text" << std::endl;
125  glog.is(VERBOSE) && glog << "this is normal text" << std::endl;
126  glog.is(VERBOSE) && glog << lt_blue << "this is light blue text (in color terminals)" << nocolor
127  << std::endl;
128  glog.is(DEBUG1) && glog << "this is debug text" << std::endl;
129 
130  glog.is(VERBOSE) && glog << group("a") << "this text is related to a" << std::endl;
131  glog.is(VERBOSE) && glog << group("b") << "this text is related to b" << std::endl;
132  glog.is(VERBOSE) && glog << group("c") << warn << "this warning is related to c" << std::endl;
133 }
void set_name(const std::string &s)
Set the name of the application that the logger is serving.
Definition: flex_ostream.h:67
Contains functions for adding color to Terminal window streams.
Definition: term_color.h:54
std::ostream & lt_blue(std::ostream &os)
All text following this manipulator is light blue (e.g. std::cout << lt_blue << "text";) ...
Definition: term_color.h:83
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...
common::FlexOstream glog
Access the Goby logger through this object.
Represents the eight available terminal colors (and bold variants)
Definition: term_color.h:108
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
std::ostream & nocolor(std::ostream &os)
All text following this manipulator is uncolored (e.g. std::cout << green << "green" << nocolor << "u...
Definition: term_color.h:104