Goby3  3.1.5
2024.05.14
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;
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 
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
Forms the basis of the Goby logger: std::ostream derived class for holding the FlexOStreamBuf.
Definition: flex_ostream.h:44
Helper class for enabling the group(std::string) manipulator.
void operator()(std::ostream &os) const
void operator()(goby::util::FlexOstream &os) const
Defines a group of messages to be sent to the Goby logger. For Verbosity == verbose streams,...
std::string name() const
Name of this group (used in the group manipulator)
bool enabled() const
Is this group enabled?
void description(const std::string &s)
std::string description() const
Human readable description of this group.
void name(const std::string &s)
void color(goby::util::Colors::Color c)
Group(std::string name="", std::string description="", goby::util::Colors::Color color=goby::util::Colors::nocolor)
goby::util::Colors::Color color() const
Color to use when displaying this group (for streams that support terminal escape codes only: std::co...
goby::util::logger::GroupSetter group(std::string n)
std::ostream & operator<<(std::ostream &os, const Group &g)
std::ostream & debug3(std::ostream &os)
label stream as "debug3"
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
std::ostream & warn(std::ostream &os)
label stream as "warning"
std::ostream & verbose(std::ostream &os)
label stream as "verbose"
std::ostream & debug2(std::ostream &os)
label stream as "debug2"
std::ostream & debug1(std::ostream &os)
label stream as "debug1"
std::ostream & die(std::ostream &os)
label stream as "error"
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
std::ostream & red(std::ostream &os)
All text following this manipulator is red. (e.g. std::cout << red << "text";)
Definition: term_color.h:62
The global namespace for the Goby project.
Color
The eight terminal colors (and bold or "light" variants)
Definition: term_color.h:112