Gazebo Common

API Reference

3.17.0
gz/common/Console.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 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 #ifndef GZ_COMMON_CONSOLE_HH_
18 #define GZ_COMMON_CONSOLE_HH_
19 
20 #include <iostream>
21 #include <fstream>
22 #include <sstream>
23 #include <string>
24 
25 #include <gz/common/Util.hh>
26 #include <gz/common/Export.hh>
28 
29 namespace ignition
30 {
31  namespace common
32  {
34  #define ignerr (ignition::common::Console::err(__FILE__, __LINE__))
35 
37  #define ignwarn (ignition::common::Console::warn(__FILE__, __LINE__))
38 
40  #define ignmsg (ignition::common::Console::msg())
41 
43  #define igndbg (ignition::common::Console::dbg(__FILE__, __LINE__))
44 
46  #define ignlog (ignition::common::Console::log())
47 
55  #define ignLogInit(_dir, _file)\
56  ignition::common::Console::log.Init(_dir, _file)
57 
59  #define ignLogClose()\
60  ignition::common::Console::log.Close()
61 
64  #define ignLogDirectory()\
65  (ignition::common::Console::log.LogDirectory())
66 
69  class IGNITION_COMMON_VISIBLE FileLogger : public std::ostream
70  {
74  public: explicit FileLogger(const std::string &_filename = "");
75 
77  public: virtual ~FileLogger();
78 
82  public: void Init(const std::string &_directory,
83  const std::string &_filename);
84 
86  public: void Close();
87 
91  public: virtual FileLogger &operator()();
92 
98  public: virtual FileLogger &operator()(
99  const std::string &_file, int _line);
100 
104  public: std::string LogDirectory() const;
105 
107  protected: class Buffer : public std::stringbuf
108  {
111  public: explicit Buffer(const std::string &_filename);
112 
114  public: virtual ~Buffer();
115 
120  public: std::streamsize xsputn(
121  const char *_char, std::streamsize _count) override;
122 
126  public: int sync() override;
127 
134  // public: std::mutex syncMutex;
135  };
136 
140  private: std::string logDirectory;
142 
144  private: bool initialized;
145  };
146 
149  class IGNITION_COMMON_VISIBLE Logger : public std::ostream
150  {
153  public: enum LogType
154  {
158  STDERR
159  };
160 
166  public: Logger(const std::string &_prefix, const int _color,
167  const LogType _type, const int _verbosity);
168 
170  public: virtual ~Logger();
171 
174  public: virtual Logger &operator()();
175 
181  public: virtual Logger &operator()(
182  const std::string &_file, int _line);
183 
185  protected: class Buffer : public std::stringbuf
186  {
192  public: Buffer(LogType _type, const int _color,
193  const int _verbosity);
194 
196  public: virtual ~Buffer();
197 
202  public: std::streamsize xsputn(
203  const char *_char, std::streamsize _count) override;
204 
208  public: int sync() override;
209 
211  public: LogType type;
212 
216  public: int color;
217 
219  public: int verbosity;
220 
225  // public: std::mutex syncMutex;
226  };
227 
230  private: std::string prefix;
232  };
233 
237  class IGNITION_COMMON_VISIBLE Console
238  {
246  public: static void SetVerbosity(const int _level);
247 
251  public: static int Verbosity();
252 
266  public: static void SetPrefix(const std::string &_customPrefix);
267 
271  public: static std::string Prefix();
272 
274  public: static Logger msg;
275 
277  public: static Logger err;
278 
280  public: static Logger dbg;
281 
283  public: static Logger warn;
284 
286  public: static FileLogger log;
287 
289  private: static int verbosity;
290 
293  private: static std::string customPrefix;
295  };
296  }
297 }
298 #endif
Buffer(const std::string &_filename)
Constructor.
Buffer(LogType _type, const int _color, const int _verbosity)
Constructor.
static std::string Prefix()
Get custom prefix. This is empty by default.
LogType type
Destination type for the messages.
Definition: gz/common/Console.hh:211
virtual ~Logger()
Destructor.
Forward declarations for the common classes.
STL class.
#define IGN_COMMON_WARN_IGNORE__DLL_INTERFACE_MISSING
Microsoft Visual Studio does not automatically export the interface information for member variables ...
Definition: gz/common/SuppressWarning.hh:65
static FileLogger log
Global instance of the file logger.
Definition: gz/common/Console.hh:286
String buffer for the file logger.
Definition: gz/common/Console.hh:107
std::streamsize xsputn(const char *_char, std::streamsize _count) override
Writes _count characters to the string buffer.
virtual Logger & operator()()
Access operator.
FileLogger(const std::string &_filename="")
Constructor.
static void SetVerbosity(const int _level)
Set verbosity, where <= 0: No output, 1: Error messages, 2: Error and warning messages,...
static Logger warn
Global instance of the warning logger.
Definition: gz/common/Console.hh:283
@ STDOUT
Output to stdout.
Definition: gz/common/Console.hh:156
static void SetPrefix(const std::string &_customPrefix)
Add a custom prefix in front of the default prefixes.
Logger(const std::string &_prefix, const int _color, const LogType _type, const int _verbosity)
Constructor.
virtual ~Buffer()
Destructor.
virtual FileLogger & operator()()
Output a filename and line number, then return a reference to the logger.
void Close()
Close the open file handles.
static Logger err
Global instance of the error logger.
Definition: gz/common/Console.hh:277
int verbosity
Level of verbosity.
Definition: gz/common/Console.hh:219
std::string LogDirectory() const
Get the full path of the directory where all the log files are stored.
STL class.
STL class.
static int Verbosity()
Get the verbose level.
Container for loggers, and global logging options (such as verbose vs. quiet output).
Definition: gz/common/Console.hh:237
virtual ~FileLogger()
Destructor.
int sync() override
Sync the stream (output the string buffer contents).
A logger that outputs messages to a file.
Definition: gz/common/Console.hh:69
Terminal logger.
Definition: gz/common/Console.hh:149
std::ofstream * stream
Stream to output information into.
Definition: gz/common/Console.hh:129
std::streamsize xsputn(const char *_char, std::streamsize _count) override
Writes _count characters to the string buffer.
static Logger msg
Global instance of the message logger.
Definition: gz/common/Console.hh:274
int sync() override
Sync the stream (output the string buffer contents).
LogType
Output destination type.
Definition: gz/common/Console.hh:153
int color
ANSI color code using Select Graphic Rendition parameters (SGR). See http://en.wikipedia....
Definition: gz/common/Console.hh:216
@ STDERR
Output to stderr.
Definition: gz/common/Console.hh:158
void Init(const std::string &_directory, const std::string &_filename)
Initialize the file logger.
static Logger dbg
Global instance of the debug logger.
Definition: gz/common/Console.hh:280
String buffer for the base logger.
Definition: gz/common/Console.hh:185
#define IGN_COMMON_WARN_RESUME__DLL_INTERFACE_MISSING
Definition: gz/common/SuppressWarning.hh:68