Gazebo Common

API Reference

4.8.1
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 IGNITION_COMMON_CONSOLE_HH_
18 #define IGNITION_COMMON_CONSOLE_HH_
19 
20 #include <iostream>
21 #include <fstream>
22 #include <sstream>
23 #include <string>
24 
25 #include <gz/common/config.hh>
26 #include <gz/common/Export.hh>
28 #include <gz/common/Util.hh>
29 
30 namespace ignition
31 {
32  namespace common
33  {
35  #define ignerr (ignition::common::Console::err(__FILE__, __LINE__))
36 
38  #define ignwarn (ignition::common::Console::warn(__FILE__, __LINE__))
39 
41  #define ignmsg (ignition::common::Console::msg())
42 
44  #define igndbg (ignition::common::Console::dbg(__FILE__, __LINE__))
45 
47  #define ignlog (ignition::common::Console::log())
48 
56  #define ignLogInit(_dir, _file)\
57  ignition::common::Console::log.Init(_dir, _file)
58 
60  #define ignLogClose()\
61  ignition::common::Console::log.Close()
62 
65  #define ignLogDirectory()\
66  (ignition::common::Console::log.LogDirectory())
67 
70  class IGNITION_COMMON_VISIBLE FileLogger : public std::ostream
71  {
75  public: explicit FileLogger(const std::string &_filename = "");
76 
78  public: virtual ~FileLogger();
79 
83  public: void Init(const std::string &_directory,
84  const std::string &_filename);
85 
87  public: void Close();
88 
92  public: virtual FileLogger &operator()();
93 
99  public: virtual FileLogger &operator()(
100  const std::string &_file, int _line);
101 
105  public: std::string LogDirectory() const;
106 
108  protected: class Buffer : public std::stringbuf
109  {
112  public: explicit Buffer(const std::string &_filename);
113 
115  public: virtual ~Buffer();
116 
122  const char *_char, std::streamsize _count) override;
123 
127  public: int sync() override;
128 
135  // public: std::mutex syncMutex;
136  };
137 
141  private: std::string logDirectory;
143 
145  private: bool initialized;
146  };
147 
150  class IGNITION_COMMON_VISIBLE Logger : public std::ostream
151  {
154  public: enum LogType
155  {
159  STDERR
160  };
161 
167  public: Logger(const std::string &_prefix, const int _color,
168  const LogType _type, const int _verbosity);
169 
171  public: virtual ~Logger();
172 
175  public: virtual Logger &operator()();
176 
182  public: virtual Logger &operator()(
183  const std::string &_file, int _line);
184 
186  protected: class Buffer : public std::stringbuf
187  {
193  public: Buffer(LogType _type, const int _color,
194  const int _verbosity);
195 
197  public: virtual ~Buffer();
198 
204  const char *_char, std::streamsize _count) override;
205 
209  public: int sync() override;
210 
212  public: LogType type;
213 
217  public: int color;
218 
220  public: int verbosity;
221 
226  // public: std::mutex syncMutex;
227  };
228 
231  private: std::string prefix;
233  };
234 
238  class IGNITION_COMMON_VISIBLE Console
239  {
247  public: static void SetVerbosity(const int _level);
248 
252  public: static int Verbosity();
253 
267  public: static void SetPrefix(const std::string &_customPrefix);
268 
272  public: static std::string Prefix();
273 
275  public: static Logger msg;
276 
278  public: static Logger err;
279 
281  public: static Logger dbg;
282 
284  public: static Logger warn;
285 
287  public: static FileLogger log;
288 
290  private: static int verbosity;
291 
294  private: static std::string customPrefix;
296  };
297  }
298 }
299 #endif
Container for loggers, and global logging options (such as verbose vs. quiet output).
Definition: gz/common/Console.hh:239
static Logger dbg
Global instance of the debug logger.
Definition: gz/common/Console.hh:281
static std::string Prefix()
Get custom prefix. This is empty by default.
static void SetVerbosity(const int _level)
Set verbosity, where <= 0: No output, 1: Error messages, 2: Error and warning messages,...
static FileLogger log
Global instance of the file logger.
Definition: gz/common/Console.hh:287
static Logger err
Global instance of the error logger.
Definition: gz/common/Console.hh:278
static void SetPrefix(const std::string &_customPrefix)
Add a custom prefix in front of the default prefixes.
static Logger warn
Global instance of the warning logger.
Definition: gz/common/Console.hh:284
static Logger msg
Global instance of the message logger.
Definition: gz/common/Console.hh:275
static int Verbosity()
Get the verbose level.
String buffer for the file logger.
Definition: gz/common/Console.hh:109
std::streamsize xsputn(const char *_char, std::streamsize _count) override
Writes _count characters to the string buffer.
int sync() override
Sync the stream (output the string buffer contents).
std::ofstream * stream
Stream to output information into.
Definition: gz/common/Console.hh:130
Buffer(const std::string &_filename)
Constructor.
A logger that outputs messages to a file.
Definition: gz/common/Console.hh:71
virtual ~FileLogger()
Destructor.
void Close()
Close the open file handles.
virtual FileLogger & operator()()
Output a filename and line number, then return a reference to the logger.
std::string LogDirectory() const
Get the full path of the directory where all the log files are stored.
virtual FileLogger & operator()(const std::string &_file, int _line)
Output a filename and line number, then return a reference to the logger.
FileLogger(const std::string &_filename="")
Constructor.
void Init(const std::string &_directory, const std::string &_filename)
Initialize the file logger.
String buffer for the base logger.
Definition: gz/common/Console.hh:187
std::streamsize xsputn(const char *_char, std::streamsize _count) override
Writes _count characters to the string buffer.
int color
ANSI color code using Select Graphic Rendition parameters (SGR). See http://en.wikipedia....
Definition: gz/common/Console.hh:217
LogType type
Destination type for the messages.
Definition: gz/common/Console.hh:212
int verbosity
Level of verbosity.
Definition: gz/common/Console.hh:220
int sync() override
Sync the stream (output the string buffer contents).
Buffer(LogType _type, const int _color, const int _verbosity)
Constructor.
virtual ~Buffer()
Destructor.
Terminal logger.
Definition: gz/common/Console.hh:151
virtual Logger & operator()(const std::string &_file, int _line)
Output a filename and line number, then return a reference to the logger.
virtual ~Logger()
Destructor.
Logger(const std::string &_prefix, const int _color, const LogType _type, const int _verbosity)
Constructor.
virtual Logger & operator()()
Access operator.
LogType
Output destination type.
Definition: gz/common/Console.hh:155
@ STDOUT
Output to stdout.
Definition: gz/common/Console.hh:157
#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
#define IGN_COMMON_WARN_RESUME__DLL_INTERFACE_MISSING
Definition: gz/common/SuppressWarning.hh:68
Forward declarations for the common classes.