Goby3 3.2.3
2025.05.13
Loading...
Searching...
No Matches
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
45namespace goby
46{
47namespace util
48{
49class FlexNCurses;
50
51namespace logger
52{
53class Group;
54}
55
56namespace logger_lock
57{
60{
62 lock
63};
64} // namespace logger_lock
65
66namespace logger
67{
68extern std::recursive_mutex mutex;
69
82}; // namespace logger
83
85class FlexOstream;
86
87class FlexOStreamBuf : public std::streambuf
88{
89 public:
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
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
void set_lock_action(logger_lock::LockAction lock_action)
void set_die_flag(bool b)
exit on error at the next call to sync()
void set_verbosity_depth(logger::Verbosity depth)
int overflow(int c=EOF)
virtual inherited from std::streambuf. Called when something is inserted into the stream
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?
void name(const std::string &s)
name of the application being served
bool is_gui() const
is there an attached stream with Verbosity == gui (ncurses GUI)
FlexOStreamBuf(FlexOstream *parent)
logger_lock::LockAction lock_action()
void refresh()
refresh the display (does nothing if !is_gui())
logger::Verbosity verbosity_depth()
void add_group(const std::string &name, logger::Group g)
add a new group
void remove_stream(std::ostream *os)
remove a stream from the logger
logger::Verbosity highest_verbosity() const
void add_stream(logger::Verbosity verbosity, std::ostream *os)
add a stream to the logger
int sync()
virtual inherited from std::streambuf. Called when std::endl or std::flush is inserted into the strea...
Forms the basis of the Goby logger: std::ostream derived class for holding the FlexOStreamBuf.
Defines a group of messages to be sent to the Goby logger. For Verbosity == verbose streams,...
static constexpr Verbosity DEBUG1
static constexpr Verbosity VERBOSE
static constexpr Verbosity DEBUG2
static constexpr Verbosity WARN
static constexpr Verbosity QUIET
static constexpr Verbosity DEBUG3
LockAction
Mutex actions available to the Goby logger (glogger)
std::recursive_mutex mutex
The global namespace for the Goby project.