Goby v2
flex_ostream.cpp
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 #include "goby/common/logger/flex_ostream.h"
24 #include "goby/common/exception.h"
25 #include "logger_manipulators.h"
26 
27 using namespace goby::common::logger;
28 
29 // boost::shared_ptr<goby::common::FlexOstream> goby::common::FlexOstream::inst_;
30 
31 // goby::common::FlexOstream& goby::common::glogger()
32 // {
33 // if(!FlexOstream::inst_) FlexOstream::inst_.reset(new FlexOstream());
34 // return(*FlexOstream::inst_);
35 // }
36 
37 int goby::common::FlexOstream::instances_ = 0;
38 
40 
41 void goby::common::FlexOstream::add_group(const std::string& name,
42  Colors::Color color /*= Colors::nocolor*/,
43  const std::string& description /*= ""*/)
44 {
45  {
46  boost::recursive_mutex::scoped_lock l(goby::common::logger::mutex);
47 
48  if (description.empty())
49  {
50  Group ng(name, name, color);
51  sb_.add_group(name, ng);
52  }
53  else
54  {
55  Group ng(name, description, color);
56  sb_.add_group(name, ng);
57  }
58  }
59 
60  this->is(VERBOSE) &&
61  *this << "Adding FlexOstream group: " << TermColor::esc_code_from_col(color) << name
62  << TermColor::esc_code_from_col(Colors::nocolor) << " (" << description << ")"
63  << std::endl;
64 }
65 
66 std::ostream& goby::common::FlexOstream::operator<<(std::ostream& (*pf)(std::ostream&))
67 {
68  if (pf == die)
69  sb_.set_die_flag(true);
70  set_unset_verbosity();
71  return std::ostream::operator<<(pf);
72 }
73 
74 bool goby::common::FlexOstream::is(logger::Verbosity verbosity)
75 {
76  assert(sb_.verbosity_depth() == logger::UNKNOWN || lock_action_ != logger_lock::lock);
77 
78  bool display = (sb_.highest_verbosity() >= verbosity) || (verbosity == logger::DIE);
79 
80  if (display)
81  {
82  if (sb_.lock_action() == logger_lock::lock)
83  {
84  goby::common::logger::mutex.lock();
85  }
86 
87  sb_.set_verbosity_depth(verbosity);
88 
89  switch (verbosity)
90  {
91  case QUIET: break;
92  case WARN: *this << warn; break;
93  case UNKNOWN:
94  case VERBOSE: *this << verbose; break;
95  case DEBUG1: *this << debug1; break;
96  case DEBUG2: *this << debug2; break;
97  case DEBUG3: *this << debug3; break;
98  case DIE: *this << die; break;
99  }
100  }
101 
102  return display;
103 }
Forms the basis of the Goby logger: std::ostream derived class for holding the FlexOStreamBuf.
Definition: flex_ostream.h:43
Color
The eight terminal colors (and bold or "light" variants)
Definition: term_color.h:111
void add_group(const std::string &name, Colors::Color color=Colors::nocolor, const std::string &description="")
Add another group to the logger. A group provides related manipulator for categorizing log messages...
common::FlexOstream glog
Access the Goby logger through this object.
static std::string esc_code_from_col(const Colors::Color &c)
Escape code from color enumeration (e.g. red -> "\33[31m")
Definition: term_color.h:151
Defines a group of messages to be sent to the Goby logger. For Verbosity == verbose streams...