Ignition Common

API Reference

3.14.0
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 <ignition/common/Util.hh>
26 #include <ignition/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 
119  public: virtual int sync();
120 
123  };
124 
128  private: std::string logDirectory;
130 
132  private: bool initialized;
133  };
134 
137  class IGNITION_COMMON_VISIBLE Logger : public std::ostream
138  {
141  public: enum LogType
142  {
146  STDERR
147  };
148 
154  public: Logger(const std::string &_prefix, const int _color,
155  const LogType _type, const int _verbosity);
156 
158  public: virtual ~Logger();
159 
162  public: virtual Logger &operator()();
163 
169  public: virtual Logger &operator()(
170  const std::string &_file, int _line);
171 
173  protected: class Buffer : public std::stringbuf
174  {
180  public: Buffer(LogType _type, const int _color,
181  const int _verbosity);
182 
184  public: virtual ~Buffer();
185 
189  public: virtual int sync();
190 
192  public: LogType type;
193 
197  public: int color;
198 
200  public: int verbosity;
201  };
202 
205  private: std::string prefix;
207  };
208 
212  class IGNITION_COMMON_VISIBLE Console
213  {
221  public: static void SetVerbosity(const int _level);
222 
226  public: static int Verbosity();
227 
241  public: static void SetPrefix(const std::string &_customPrefix);
242 
246  public: static std::string Prefix();
247 
249  public: static Logger msg;
250 
252  public: static Logger err;
253 
255  public: static Logger dbg;
256 
258  public: static Logger warn;
259 
261  public: static FileLogger log;
262 
264  private: static int verbosity;
265 
268  private: static std::string customPrefix;
270  };
271  }
272 }
273 #endif
#define IGN_COMMON_WARN_IGNORE__DLL_INTERFACE_MISSING
Microsoft Visual Studio does not automatically export the interface information for member variables ...
Definition: SuppressWarning.hh:64
#define IGN_COMMON_WARN_RESUME__DLL_INTERFACE_MISSING
Definition: SuppressWarning.hh:67
Container for loggers, and global logging options (such as verbose vs. quiet output).
Definition: Console.hh:213
static Logger dbg
Global instance of the debug logger.
Definition: Console.hh:255
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: Console.hh:261
static Logger err
Global instance of the error logger.
Definition: Console.hh:252
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: Console.hh:258
static Logger msg
Global instance of the message logger.
Definition: Console.hh:249
static int Verbosity()
Get the verbose level.
String buffer for the file logger.
Definition: Console.hh:108
virtual int sync()
Sync the stream (output the string buffer contents).
std::ofstream * stream
Stream to output information into.
Definition: Console.hh:122
Buffer(const std::string &_filename)
Constructor.
A logger that outputs messages to a file.
Definition: Console.hh:70
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: Console.hh:174
int color
ANSI color code using Select Graphic Rendition parameters (SGR). See http://en.wikipedia....
Definition: Console.hh:197
LogType type
Destination type for the messages.
Definition: Console.hh:192
int verbosity
Level of verbosity.
Definition: Console.hh:200
virtual int sync()
Sync the stream (output the string buffer contents).
Buffer(LogType _type, const int _color, const int _verbosity)
Constructor.
virtual ~Buffer()
Destructor.
Terminal logger.
Definition: Console.hh:138
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: Console.hh:142
@ STDOUT
Output to stdout.
Definition: Console.hh:144
Forward declarations for the common classes.