Console.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 
18 #ifndef _GAZEBO_CONSOLE_HH_
19 #define _GAZEBO_CONSOLE_HH_
20 
21 #include <iostream>
22 #include <fstream>
23 #include <sstream>
24 #include <string>
25 
26 #ifndef Q_MOC_RUN // See: https://bugreports.qt-project.org/browse/QTBUG-22829
27 # include <boost/thread.hpp>
28 #endif
31 #include "gazebo/util/system.hh"
32 
33 namespace gazebo
34 {
35  namespace common
36  {
39 
41  #define gzmsg (gazebo::common::Console::msg())
42 
44  #define gzdbg (gazebo::common::Console::dbg(__FILE__, __LINE__))
45 
47  #define gzwarn (gazebo::common::Console::warn(__FILE__, __LINE__))
48 
50  #define gzerr (gazebo::common::Console::err(__FILE__, __LINE__))
51 
53  #define gzlog (gazebo::common::Console::log())
54 
61  #define gzLogInit(_prefix, _str) \
62  (gazebo::common::Console::log.Init(_prefix, _str))
63 
66  #define gzLogDirectory() (gazebo::common::Console::log.GetLogDirectory())
67 
70  class GZ_COMMON_VISIBLE FileLogger : public std::ostream
71  {
75  public: explicit FileLogger(const std::string &_filename = "");
76 
78  public: virtual ~FileLogger();
79 
85  public: void Init(const std::string &_prefix,
86  const std::string &_filename);
87 
91  public: virtual FileLogger &operator()();
92 
98  public: virtual FileLogger &operator()(
99  const std::string &_file, int _line);
100 
104  public: std::string GetLogDirectory() const;
105 
108  private: static std::string GetMasterPort();
109 
111  protected: class Buffer : public std::stringbuf
112  {
115  public: explicit Buffer(const std::string &_filename);
116 
118  public: virtual ~Buffer();
119 
123  public: virtual int sync();
124 
126  public: std::ofstream *stream;
127  };
128 
131  private: std::string logDirectory;
132  };
133 
136  class GZ_COMMON_VISIBLE Logger : public std::ostream
137  {
140  public: enum LogType
141  {
145  STDERR
146  };
147 
152  public: Logger(const std::string &_prefix, int _color, LogType _type);
153 
155  public: virtual ~Logger();
156 
159  public: virtual Logger &operator()();
160 
166  public: virtual Logger &operator()(
167  const std::string &_file, int _line);
168 
170  protected: class Buffer : public std::stringbuf
171  {
176  public: Buffer(LogType _type, int _color);
177 
179  public: virtual ~Buffer();
180 
184  public: virtual int sync();
185 
187  public: LogType type;
188 
192  public: int color;
193  };
194 
196  public: int color;
197 
199  private: std::string prefix;
200  };
201 
205  class GZ_COMMON_VISIBLE Console
206  {
209  public: static void SetQuiet(bool _q);
210 
213  public: static bool GetQuiet();
214 
216  public: static Logger msg;
217 
219  public: static Logger err;
220 
222  public: static Logger dbg;
223 
225  public: static Logger warn;
226 
228  public: static FileLogger log;
229 
231  private: static bool quiet;
232  };
234  }
235 }
236 #endif
LogType type
Destination type for the messages.
Definition: Console.hh:187
Terminal logger.
Definition: Console.hh:136
static FileLogger log
Global instance of the file logger.
Definition: Console.hh:228
std::ofstream * stream
Stream to output information into.
Definition: Console.hh:126
Output to stdout.
Definition: Console.hh:143
LogType
Definition: Console.hh:140
static Logger dbg
Global instance of the debug logger.
Definition: Console.hh:222
int color
Color for the output.
Definition: Console.hh:196
int color
ANSI color code using Select Graphic Rendition parameters (SGR).
Definition: Console.hh:192
String buffer for the base logger.
Definition: Console.hh:170
Container for loggers, and global logging options (such as verbose vs.
Definition: Console.hh:205
static Logger err
Global instance of the error logger.
Definition: Console.hh:219
A logger that outputs messages to a file.
Definition: Console.hh:70
GAZEBO_VISIBLE void Init(google::protobuf::Message &_message, const std::string &_id="")
Initialize a message.
static Logger msg
Global instance of the message logger.
Definition: Console.hh:216
String buffer for the file logger.
Definition: Console.hh:111
static Logger warn
Global instance of the warning logger.
Definition: Console.hh:225