Ignition Common

API Reference

3.0.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 
54  #define ignLogInit(_dir, _file)\
55  ignition::common::Console::log.Init(_dir, _file)
56 
59  #define ignLogDirectory()\
60  (ignition::common::Console::log.LogDirectory())
61 
64  class IGNITION_COMMON_VISIBLE FileLogger : public std::ostream
65  {
69  public: explicit FileLogger(const std::string &_filename = "");
70 
72  public: virtual ~FileLogger();
73 
77  public: void Init(const std::string &_directory,
78  const std::string &_filename);
79 
83  public: virtual FileLogger &operator()();
84 
90  public: virtual FileLogger &operator()(
91  const std::string &_file, int _line);
92 
96  public: std::string LogDirectory() const;
97 
99  protected: class Buffer : public std::stringbuf
100  {
103  public: explicit Buffer(const std::string &_filename);
104 
106  public: virtual ~Buffer();
107 
111  public: virtual int sync();
112 
115  };
116 
120  private: std::string logDirectory;
122 
124  private: bool initialized;
125  };
126 
129  class IGNITION_COMMON_VISIBLE Logger : public std::ostream
130  {
133  public: enum LogType
134  {
138  STDERR
139  };
140 
146  public: Logger(const std::string &_prefix, const int _color,
147  const LogType _type, const int _verbosity);
148 
150  public: virtual ~Logger();
151 
154  public: virtual Logger &operator()();
155 
161  public: virtual Logger &operator()(
162  const std::string &_file, int _line);
163 
165  protected: class Buffer : public std::stringbuf
166  {
172  public: Buffer(LogType _type, const int _color,
173  const int _verbosity);
174 
176  public: virtual ~Buffer();
177 
181  public: virtual int sync();
182 
184  public: LogType type;
185 
189  public: int color;
190 
192  public: int verbosity;
193  };
194 
197  private: std::string prefix;
199  };
200 
204  class IGNITION_COMMON_VISIBLE Console
205  {
213  public: static void SetVerbosity(const int _level);
214 
218  public: static int Verbosity();
219 
233  public: static void SetPrefix(const std::string &_customPrefix);
234 
238  public: static std::string Prefix();
239 
241  public: static Logger msg;
242 
244  public: static Logger err;
245 
247  public: static Logger dbg;
248 
250  public: static Logger warn;
251 
253  public: static FileLogger log;
254 
256  private: static int verbosity;
257 
260  private: static std::string customPrefix;
262  };
263  }
264 }
265 #endif
int color
ANSI color code using Select Graphic Rendition parameters (SGR). See http://en.wikipedia.org/wiki/ANSI_escape_code#Colors.
Definition: Console.hh:189
int verbosity
Level of verbosity.
Definition: Console.hh:192
Container for loggers, and global logging options (such as verbose vs. quiet output).
Definition: Console.hh:204
Terminal logger.
Definition: Console.hh:129
static Logger warn
Global instance of the warning logger.
Definition: Console.hh:250
String buffer for the base logger.
Definition: Console.hh:165
Output to stdout.
Definition: Console.hh:136
#define IGN_COMMON_WARN_RESUME__DLL_INTERFACE_MISSING
Definition: SuppressWarning.hh:67
A logger that outputs messages to a file.
Definition: Console.hh:64
LogType type
Destination type for the messages.
Definition: Console.hh:184
STL class.
static Logger err
Global instance of the error logger.
Definition: Console.hh:244
STL class.
LogType
Output destination type.
Definition: Console.hh:133
static Logger msg
Global instance of the message logger.
Definition: Console.hh:241
Forward declarations for the common classes.
static Logger dbg
Global instance of the debug logger.
Definition: Console.hh:247
STL class.
String buffer for the file logger.
Definition: Console.hh:99
static FileLogger log
Global instance of the file logger.
Definition: Console.hh:253
#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
std::ofstream * stream
Stream to output information into.
Definition: Console.hh:114