Goby3  3.1.4
2024.02.22
logger_manipulators.h
Go to the documentation of this file.
1 // Copyright 2012-2021:
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_LOGGER_MANIPULATORS_H
26 #define GOBY_UTIL_DEBUG_LOGGER_LOGGER_MANIPULATORS_H
27 
28 #include <iostream>
29 #include <string>
30 #include <utility>
31 
32 #include "term_color.h"
33 
34 namespace goby
35 {
36 namespace util
37 {
38 class FlexOstream;
39 
40 namespace logger
41 {
43 inline std::ostream& die(std::ostream& os)
44 {
45  return (os << goby::util::tcolor::red << "(Error): " << goby::util::tcolor::nocolor);
46 }
47 
49 inline std::ostream& warn(std::ostream& os)
50 {
51  return (os << goby::util::tcolor::red << "(Warning): " << goby::util::tcolor::nocolor);
52 }
53 
55 inline std::ostream& verbose(std::ostream& os) { return (os); }
56 
58 inline std::ostream& debug1(std::ostream& os) { return (os << "D: "); }
59 
61 inline std::ostream& debug2(std::ostream& os) { return (os << "D2: "); }
62 
64 inline std::ostream& debug3(std::ostream& os) { return (os << "D3: "); }
65 
66 
68 class Group
69 {
70  public:
71  Group(std::string name = "", std::string description = "",
73  : name_(std::move(name)), description_(std::move(description)), color_(color)
74  {
75  }
76 
78 
79  std::string name() const { return name_; }
82  std::string description() const { return description_; }
84  goby::util::Colors::Color color() const { return color_; }
86  bool enabled() const { return enabled_; }
88 
90 
91  void name(const std::string& s) { name_ = s; }
92  void description(const std::string& s) { description_ = s; }
93  void color(goby::util::Colors::Color c) { color_ = c; }
94  void enabled(bool b) { enabled_ = b; }
96 
97  private:
98  std::string name_;
99  std::string description_;
101  bool enabled_{true};
102 };
103 
106 {
107  public:
108  explicit GroupSetter(std::string s) : group_(std::move(s)) {}
109  void operator()(std::ostream& os) const;
110  void operator()(goby::util::FlexOstream& os) const;
111 
112  private:
113  std::string group_;
114 };
115 
117 std::ostream& basic_log_header(std::ostream& os, const std::string& group_name);
118 
119 std::ostream& operator<<(std::ostream& os, const Group& g);
120 inline std::ostream& operator<<(std::ostream& os, const GroupSetter& gs)
121 {
122  gs(os);
123  return (os);
124 }
125 
126 goby::util::FlexOstream& operator<<(goby::util::FlexOstream& os, const GroupSetter& gs);
127 
128 } // namespace logger
129 } // namespace util
130 } // namespace goby
131 
132 using goby::util::logger::operator<<;
133 
135 {
136  return (goby::util::logger::GroupSetter(std::move(n)));
137 }
138 
139 #endif
goby::util::logger::die
std::ostream & die(std::ostream &os)
label stream as "error"
Definition: logger_manipulators.h:43
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::Colors::Color
Color
The eight terminal colors (and bold or "light" variants)
Definition: term_color.h:111
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::Group::enabled
void enabled(bool b)
Definition: logger_manipulators.h:94
term_color.h
goby::util::logger::Group::name
std::string name() const
Name of this group (used in the group manipulator)
Definition: logger_manipulators.h:80
goby::util::logger::GroupSetter::GroupSetter
GroupSetter(std::string s)
Definition: logger_manipulators.h:108
goby::util::logger::Group::description
std::string description() const
Human readable description of this group.
Definition: logger_manipulators.h:82
group
goby::util::logger::GroupSetter group(std::string n)
Definition: logger_manipulators.h:134
goby::util::tcolor::red
std::ostream & red(std::ostream &os)
All text following this manipulator is red. (e.g. std::cout << red << "text";)
Definition: term_color.h:62
goby::util::logger::Group::description
void description(const std::string &s)
Definition: logger_manipulators.h:92
goby::util::logger::debug2
std::ostream & debug2(std::ostream &os)
label stream as "debug2"
Definition: logger_manipulators.h:61
goby::util::logger::basic_log_header
std::ostream & basic_log_header(std::ostream &os, const std::string &group_name)
used for non tty ostreams (everything but std::cout / std::cerr) as the header for every line
goby::util::Colors::nocolor
@ nocolor
Definition: term_color.h:113
goby::util::logger::Group::Group
Group(std::string name="", std::string description="", goby::util::Colors::Color color=goby::util::Colors::nocolor)
Definition: logger_manipulators.h:71
goby::util::logger::Group::color
goby::util::Colors::Color color() const
Color to use when displaying this group (for streams that support terminal escape codes only: std::co...
Definition: logger_manipulators.h:84
goby::util::logger::Group::name
void name(const std::string &s)
Definition: logger_manipulators.h:91
goby::util::logger::operator<<
std::ostream & operator<<(std::ostream &os, const Group &g)
goby::util::logger::GroupSetter::operator()
void operator()(std::ostream &os) const
goby::util::logger::GroupSetter
Helper class for enabling the group(std::string) manipulator.
Definition: logger_manipulators.h:105
goby::util::logger::warn
std::ostream & warn(std::ostream &os)
label stream as "warning"
Definition: logger_manipulators.h:49
goby::util::logger::Group::enabled
bool enabled() const
Is this group enabled?
Definition: logger_manipulators.h:86
goby::util::logger::Group::color
void color(goby::util::Colors::Color c)
Definition: logger_manipulators.h:93
goby::util::tcolor::nocolor
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
goby::util::logger::verbose
std::ostream & verbose(std::ostream &os)
label stream as "verbose"
Definition: logger_manipulators.h:55
goby::util::logger::debug3
std::ostream & debug3(std::ostream &os)
label stream as "debug3"
Definition: logger_manipulators.h:64
goby::util::logger::debug1
std::ostream & debug1(std::ostream &os)
label stream as "debug1"
Definition: logger_manipulators.h:58