Goby3  3.1.5a
2024.05.23
flex_ostreambuf.h
Go to the documentation of this file.
1 // Copyright 2012-2023:
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 Goby Underwater Autonomy Project Libraries
10 // ("The Goby Libraries").
11 //
12 // The Goby Libraries are free software: you can redistribute them and/or modify
13 // them 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 // The Goby Libraries are distributed in the hope that they 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 Goby. If not, see <http://www.gnu.org/licenses/>.
24 
25 #ifndef GOBY_UTIL_DEBUG_LOGGER_FLEX_OSTREAMBUF_H
26 #define GOBY_UTIL_DEBUG_LOGGER_FLEX_OSTREAMBUF_H
27 
28 #include <atomic>
29 #include <cstdio>
30 #include <deque>
31 #include <iostream>
32 #include <mutex>
33 #include <sstream>
34 #include <vector>
35 
36 #include <thread>
37 
38 #include <boost/date_time.hpp>
39 #include <memory>
40 
42 
43 #include "term_color.h"
44 
45 namespace goby
46 {
47 namespace util
48 {
49 class FlexNCurses;
50 
51 namespace logger
52 {
53 class Group;
54 }
55 
56 namespace logger_lock
57 {
60 {
63 };
64 } // namespace logger_lock
65 
66 namespace logger
67 {
68 extern std::recursive_mutex mutex;
69 
71 {
72  UNKNOWN = 3,
76  //GUI = protobuf::GLogConfig::GUI,
80  DIE = -1
81 };
82 }; // namespace logger
83 
85 class FlexOstream;
86 
87 class FlexOStreamBuf : public std::streambuf
88 {
89  public:
90  FlexOStreamBuf(FlexOstream* parent);
92 
94  int sync();
95 
97  int overflow(int c = EOF);
98 
100  void name(const std::string& s) { name_ = s; }
101 
103  void add_stream(logger::Verbosity verbosity, std::ostream* os);
104 
106  void remove_stream(std::ostream* os);
107 
109  bool is_quiet() const { return highest_verbosity_ == logger::QUIET; }
110 
112  bool is_gui() const { return is_gui_; }
113 
114  void enable_gui();
115 
116  logger::Verbosity highest_verbosity() const { return highest_verbosity_; }
117 
119  void group_name(const std::string& s) { group_name_ = s; }
120 
122  void set_die_flag(bool b) { die_flag_ = b; }
123 
124  void set_verbosity_depth(logger::Verbosity depth) { current_verbosity_ = depth; }
125 
126  logger::Verbosity verbosity_depth() { return current_verbosity_; }
127 
129  void add_group(const std::string& name, logger::Group g);
130 
132  void refresh();
133 
135 
136  logger_lock::LockAction lock_action() { return lock_action_; }
137 
138  private:
139  void display(std::string& s);
140  void strip_escapes(std::string& s);
141 
142  private:
143  std::deque<std::string> buffer_;
144 
145  class StreamConfig
146  {
147  public:
148  StreamConfig(std::ostream* os, logger::Verbosity verbosity) : os_(os), verbosity_(verbosity)
149  {
150  }
151 
152  void set_verbosity(logger::Verbosity verbosity) { verbosity_ = verbosity; }
153 
154  std::ostream* os() const { return os_; }
155  logger::Verbosity verbosity() const { return verbosity_; }
156 
157  private:
158  std::ostream* os_;
159  logger::Verbosity verbosity_;
160  };
161 
162  std::string name_;
163  std::string group_name_;
164 
165  std::map<std::string, logger::Group> groups_;
166 
167  std::atomic<bool> die_flag_;
168  std::atomic<logger::Verbosity> current_verbosity_;
169 
170  FlexNCurses* curses_;
171  std::shared_ptr<std::thread> input_thread_;
172 
173  boost::posix_time::ptime start_time_;
174 
175  std::vector<StreamConfig> streams_;
176 
177  bool is_gui_;
178 
179  std::atomic<logger::Verbosity> highest_verbosity_;
180 
181  std::atomic<logger_lock::LockAction> lock_action_;
182  // FlexOstream* parent_;
183 };
184 } // namespace util
185 } // namespace goby
186 #endif
goby::util::protobuf::GLogConfig::DEBUG3
static const Verbosity DEBUG3
Definition: debug_logger.pb.h:532
goby::util::logger::Group
Defines a group of messages to be sent to the Goby logger. For Verbosity == verbose streams,...
Definition: logger_manipulators.h:68
goby::util::FlexOStreamBuf::add_group
void add_group(const std::string &name, logger::Group g)
add a new group
goby::util::protobuf::GLogConfig::WARN
static const Verbosity WARN
Definition: debug_logger.pb.h:524
goby::util::protobuf::GLogConfig::VERBOSE
static const Verbosity VERBOSE
Definition: debug_logger.pb.h:526
goby::util::FlexOstream
Forms the basis of the Goby logger: std::ostream derived class for holding the FlexOStreamBuf.
Definition: flex_ostream.h:43
goby
The global namespace for the Goby project.
Definition: acomms_constants.h:33
goby::util::logger::DEBUG2
@ DEBUG2
Definition: flex_ostreambuf.h:78
goby::util::FlexOStreamBuf::is_quiet
bool is_quiet() const
do all attached streams have Verbosity == quiet?
Definition: flex_ostreambuf.h:109
goby::util::logger::mutex
std::recursive_mutex mutex
term_color.h
goby::util::logger::WARN
@ WARN
Definition: flex_ostreambuf.h:74
goby::util::FlexOStreamBuf::sync
int sync()
virtual inherited from std::streambuf. Called when std::endl or std::flush is inserted into the strea...
goby::util::FlexOStreamBuf::name
void name(const std::string &s)
name of the application being served
Definition: flex_ostreambuf.h:100
goby::util::logger_lock::none
@ none
Definition: flex_ostreambuf.h:61
goby::util::logger_lock::LockAction
LockAction
Mutex actions available to the Goby logger (glogger)
Definition: flex_ostreambuf.h:59
goby::util::logger::VERBOSE
@ VERBOSE
Definition: flex_ostreambuf.h:75
goby::util::FlexOStreamBuf::FlexOStreamBuf
FlexOStreamBuf(FlexOstream *parent)
goby::util::logger::QUIET
@ QUIET
Definition: flex_ostreambuf.h:73
goby::util::logger_lock::lock
@ lock
Definition: flex_ostreambuf.h:62
goby::util::FlexOStreamBuf
Definition: flex_ostreambuf.h:87
goby::util::FlexOStreamBuf::refresh
void refresh()
refresh the display (does nothing if !is_gui())
goby::util::FlexOStreamBuf::lock_action
logger_lock::LockAction lock_action()
Definition: flex_ostreambuf.h:136
goby::util::protobuf::GLogConfig::DEBUG2
static const Verbosity DEBUG2
Definition: debug_logger.pb.h:530
goby::util::FlexOStreamBuf::set_die_flag
void set_die_flag(bool b)
exit on error at the next call to sync()
Definition: flex_ostreambuf.h:122
goby::util::FlexOStreamBuf::is_gui
bool is_gui() const
is there an attached stream with Verbosity == gui (ncurses GUI)
Definition: flex_ostreambuf.h:112
goby::util::protobuf::GLogConfig::DEBUG1
static const Verbosity DEBUG1
Definition: debug_logger.pb.h:528
goby::util::logger::DIE
@ DIE
Definition: flex_ostreambuf.h:80
goby::util::FlexOStreamBuf::enable_gui
void enable_gui()
goby::util::logger::DEBUG3
@ DEBUG3
Definition: flex_ostreambuf.h:79
goby::util::FlexOStreamBuf::set_verbosity_depth
void set_verbosity_depth(logger::Verbosity depth)
Definition: flex_ostreambuf.h:124
goby::util::logger::Verbosity
Verbosity
Definition: flex_ostreambuf.h:70
goby::util::FlexOStreamBuf::add_stream
void add_stream(logger::Verbosity verbosity, std::ostream *os)
add a stream to the logger
goby::util::FlexOStreamBuf::highest_verbosity
logger::Verbosity highest_verbosity() const
Definition: flex_ostreambuf.h:116
goby::util::protobuf::GLogConfig::QUIET
static const Verbosity QUIET
Definition: debug_logger.pb.h:522
goby::util::FlexOStreamBuf::group_name
void group_name(const std::string &s)
current group name (last insertion of group("") into the stream)
Definition: flex_ostreambuf.h:119
debug_logger.pb.h
goby::util::FlexOStreamBuf::~FlexOStreamBuf
~FlexOStreamBuf()
goby::util::logger::UNKNOWN
@ UNKNOWN
Definition: flex_ostreambuf.h:72
goby::util::FlexOStreamBuf::set_lock_action
void set_lock_action(logger_lock::LockAction lock_action)
Definition: flex_ostreambuf.h:134
goby::util::logger::DEBUG1
@ DEBUG1
Definition: flex_ostreambuf.h:77
goby::util::seawater::depth
boost::units::quantity< boost::units::si::length > depth(boost::units::quantity< PressureUnit > pressure, boost::units::quantity< LatitudeUnit > latitude)
Calculates depth from pressure and latitude Adapted from "Algorithms for computation of fundamental p...
Definition: depth.h:57
goby::util::FlexOStreamBuf::verbosity_depth
logger::Verbosity verbosity_depth()
Definition: flex_ostreambuf.h:126
goby::util::FlexOStreamBuf::overflow
int overflow(int c=EOF)
virtual inherited from std::streambuf. Called when something is inserted into the stream
goby::util::FlexOStreamBuf::remove_stream
void remove_stream(std::ostream *os)
remove a stream from the logger