Console.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2016 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  {
40  #define gzmsg (gazebo::common::Console::msg())
41 
43  #define gzdbg (gazebo::common::Console::dbg(__FILE__, __LINE__))
44 
46  #define gzwarn (gazebo::common::Console::warn(__FILE__, __LINE__))
47 
49  #define gzerr (gazebo::common::Console::err(__FILE__, __LINE__))
50 
52  #define gzlog (gazebo::common::Console::log())
53 
60  #define gzLogInit(_prefix, _str) \
61  (gazebo::common::Console::log.Init(_prefix, _str))
62 
65  #define gzLogDirectory() (gazebo::common::Console::log.GetLogDirectory())
66 
69  class GZ_COMMON_VISIBLE FileLogger : public std::ostream
70  {
74  public: FileLogger(const std::string &_filename = "");
75 
77  public: virtual ~FileLogger();
78 
84  public: void Init(const std::string &_prefix,
85  const std::string &_filename);
86 
90  public: virtual FileLogger &operator()();
91 
97  public: virtual FileLogger &operator()(
98  const std::string &_file, int _line);
99 
103  public: std::string GetLogDirectory() const;
104 
107  private: static std::string GetMasterPort();
108 
110  protected: class Buffer : public std::stringbuf
111  {
114  public: Buffer(const std::string &_filename);
115 
117  public: virtual ~Buffer();
118 
122  public: virtual int sync();
123 
125  public: std::ofstream *stream;
126  };
127 
130  private: std::string logDirectory;
131  };
132 
135  class GZ_COMMON_VISIBLE Logger : public std::ostream
136  {
139  public: enum LogType
140  {
144  STDERR
145  };
146 
151  public: Logger(const std::string &_prefix, int _color, LogType _type);
152 
154  public: virtual ~Logger();
155 
158  public: virtual Logger &operator()();
159 
165  public: virtual Logger &operator()(
166  const std::string &_file, int _line);
167 
169  protected: class Buffer : public std::stringbuf
170  {
175  public: Buffer(LogType _type, int _color);
176 
178  public: virtual ~Buffer();
179 
183  public: virtual int sync();
184 
186  public: LogType type;
187 
191  public: int color;
192  };
193 
195  public: int color;
196 
198  private: std::string prefix;
199  };
200 
205  {
208  public: static void SetQuiet(bool _q);
209 
212  public: static bool GetQuiet();
213 
215  public: static Logger msg;
216 
218  public: static Logger err;
219 
221  public: static Logger dbg;
222 
224  public: static Logger warn;
225 
227  public: static FileLogger log;
228 
230  private: static bool quiet;
231  };
233  }
234 }
235 #endif
Terminal logger.
Definition: Console.hh:135
LogType type
Destination type for the messages.
Definition: Console.hh:186
static Logger msg
Global instance of the message logger.
Definition: Console.hh:215
Output to stdout.
Definition: Console.hh:142
int color
ANSI color code using Select Graphic Rendition parameters (SGR).
Definition: Console.hh:191
static Logger dbg
Global instance of the debug logger.
Definition: Console.hh:221
String buffer for the base logger.
Definition: Console.hh:169
#define GZ_COMMON_VISIBLE
Definition: system.hh:84
Container for loggers, and global logging options (such as verbose vs.
Definition: Console.hh:204
std::ofstream * stream
Stream to output information into.
Definition: Console.hh:125
static FileLogger log
Global instance of the file logger.
Definition: Console.hh:227
A logger that outputs messages to a file.
Definition: Console.hh:69
static Logger err
Global instance of the error logger.
Definition: Console.hh:218
GAZEBO_VISIBLE void Init(google::protobuf::Message &_message, const std::string &_id="")
Initialize a message.
LogType
Definition: Console.hh:139
String buffer for the file logger.
Definition: Console.hh:110
static Logger warn
Global instance of the warning logger.
Definition: Console.hh:224
int color
Color for the output.
Definition: Console.hh:195