Goby v2
flex_ostreambuf.h
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 // Community contributors (see AUTHORS file)
5 //
6 //
7 // This file is part of the Goby Underwater Autonomy Project Libraries
8 // ("The Goby Libraries").
9 //
10 // The Goby Libraries are free software: you can redistribute them and/or modify
11 // them 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 // The Goby Libraries are distributed in the hope that they 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 Goby. If not, see <http://www.gnu.org/licenses/>.
22 
23 #ifndef FlexOStreamBuf20091110H
24 #define FlexOStreamBuf20091110H
25 
26 #include <cstdio>
27 #include <deque>
28 #include <iostream>
29 #include <sstream>
30 #include <vector>
31 
32 #include <boost/thread.hpp>
33 
34 #include <boost/date_time.hpp>
35 #include <boost/shared_ptr.hpp>
36 
37 #include "goby/common/protobuf/logger.pb.h"
38 
39 #include "term_color.h"
40 
41 class Group;
42 
43 namespace goby
44 {
45 namespace common
46 {
47 class FlexNCurses;
48 
49 namespace logger_lock
50 {
52 enum LockAction
53 {
54  none,
55  lock
56 };
57 } // namespace logger_lock
58 
59 namespace logger
60 {
61 extern boost::recursive_mutex mutex;
62 
63 enum Verbosity
64 {
65  UNKNOWN = 4,
66  QUIET = protobuf::GLogConfig::QUIET,
67  WARN = protobuf::GLogConfig::WARN,
68  VERBOSE = protobuf::GLogConfig::VERBOSE,
69  //GUI = protobuf::GLogConfig::GUI,
70  DEBUG1 = protobuf::GLogConfig::DEBUG1,
71  DEBUG2 = protobuf::GLogConfig::DEBUG2,
72  DEBUG3 = protobuf::GLogConfig::DEBUG3,
73  DIE = -1
74 };
75 }; // namespace logger
76 
78 class FlexOstream;
79 
80 class FlexOStreamBuf : public std::streambuf
81 {
82  public:
83  FlexOStreamBuf(FlexOstream* parent);
84  ~FlexOStreamBuf();
85 
87  int sync();
88 
90  int overflow(int c = EOF);
91 
93  void name(const std::string& s) { name_ = s; }
94 
96  void add_stream(logger::Verbosity verbosity, std::ostream* os);
97 
99  bool is_quiet() const { return highest_verbosity_ == logger::QUIET; }
100 
102  bool is_gui() const { return is_gui_; }
103 
104  void enable_gui();
105 
106  logger::Verbosity highest_verbosity() const { return highest_verbosity_; }
107 
109  void group_name(const std::string& s) { group_name_ = s; }
110 
112  void set_die_flag(bool b) { die_flag_ = b; }
113 
114  void set_verbosity_depth(logger::Verbosity depth) { current_verbosity_ = depth; }
115 
116  logger::Verbosity verbosity_depth() { return current_verbosity_; }
117 
119  void add_group(const std::string& name, Group g);
120 
122  void refresh();
123 
124  void set_lock_action(logger_lock::LockAction lock_action) { lock_action_ = lock_action; }
125 
126  logger_lock::LockAction lock_action() { return lock_action_; }
127 
128  private:
129  void display(std::string& s);
130  void strip_escapes(std::string& s);
131 
132  private:
133  std::deque<std::string> buffer_;
134 
135  class StreamConfig
136  {
137  public:
138  StreamConfig(std::ostream* os, logger::Verbosity verbosity) : os_(os), verbosity_(verbosity)
139  {
140  }
141 
142  void set_verbosity(logger::Verbosity verbosity) { verbosity_ = verbosity; }
143 
144  std::ostream* os() const { return os_; }
145  logger::Verbosity verbosity() const { return verbosity_; }
146 
147  private:
148  std::ostream* os_;
149  logger::Verbosity verbosity_;
150  };
151 
152  std::string name_;
153  std::string group_name_;
154 
155  std::map<std::string, Group> groups_;
156 
157  bool die_flag_;
158  logger::Verbosity current_verbosity_;
159 
160  FlexNCurses* curses_;
161  boost::shared_ptr<boost::thread> input_thread_;
162 
163  boost::posix_time::ptime start_time_;
164 
165  std::vector<StreamConfig> streams_;
166 
167  bool is_gui_;
168 
169  logger::Verbosity highest_verbosity_;
170 
171  logger_lock::LockAction lock_action_;
172  FlexOstream* parent_;
173 };
174 } // namespace common
175 } // namespace goby
176 #endif
bool is_gui() const
is there an attached stream with Verbosity == gui (ncurses GUI)
void name(const std::string &s)
name of the application being served
void set_die_flag(bool b)
exit on error at the next call to sync()
Forms the basis of the Goby logger: std::ostream derived class for holding the FlexOStreamBuf.
Definition: flex_ostream.h:43
void group_name(const std::string &s)
current group name (last insertion of group("") into the stream)
bool is_quiet() const
do all attached streams have Verbosity == quiet?
The global namespace for the Goby project.
Enables the Verbosity == gui mode of the Goby logger and displays an NCurses gui for the logger conte...
Definition: flex_ncurses.h:42
Defines a group of messages to be sent to the Goby logger. For Verbosity == verbose streams...