Ignition Common

API Reference

3.6.1
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 
60  #define ignLogDirectory()\
61  (ignition::common::Console::log.LogDirectory())
62 
65  class IGNITION_COMMON_VISIBLE FileLogger : public std::ostream
66  {
70  public: explicit FileLogger(const std::string &_filename = "");
71 
73  public: virtual ~FileLogger();
74 
78  public: void Init(const std::string &_directory,
79  const std::string &_filename);
80 
84  public: virtual FileLogger &operator()();
85 
91  public: virtual FileLogger &operator()(
92  const std::string &_file, int _line);
93 
97  public: std::string LogDirectory() const;
98 
100  protected: class Buffer : public std::stringbuf
101  {
104  public: explicit Buffer(const std::string &_filename);
105 
107  public: virtual ~Buffer();
108 
112  public: virtual int sync();
113 
116  };
117 
121  private: std::string logDirectory;
123 
125  private: bool initialized;
126  };
127 
130  class IGNITION_COMMON_VISIBLE Logger : public std::ostream
131  {
134  public: enum LogType
135  {
139  STDERR
140  };
141 
147  public: Logger(const std::string &_prefix, const int _color,
148  const LogType _type, const int _verbosity);
149 
151  public: virtual ~Logger();
152 
155  public: virtual Logger &operator()();
156 
162  public: virtual Logger &operator()(
163  const std::string &_file, int _line);
164 
166  protected: class Buffer : public std::stringbuf
167  {
173  public: Buffer(LogType _type, const int _color,
174  const int _verbosity);
175 
177  public: virtual ~Buffer();
178 
182  public: virtual int sync();
183 
185  public: LogType type;
186 
190  public: int color;
191 
193  public: int verbosity;
194  };
195 
198  private: std::string prefix;
200  };
201 
205  class IGNITION_COMMON_VISIBLE Console
206  {
214  public: static void SetVerbosity(const int _level);
215 
219  public: static int Verbosity();
220 
234  public: static void SetPrefix(const std::string &_customPrefix);
235 
239  public: static std::string Prefix();
240 
242  public: static Logger msg;
243 
245  public: static Logger err;
246 
248  public: static Logger dbg;
249 
251  public: static Logger warn;
252 
254  public: static FileLogger log;
255 
257  private: static int verbosity;
258 
261  private: static std::string customPrefix;
263  };
264  }
265 }
266 #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:190
int verbosity
Level of verbosity.
Definition: Console.hh:193
Container for loggers, and global logging options (such as verbose vs. quiet output).
Definition: Console.hh:205
Terminal logger.
Definition: Console.hh:130
static Logger warn
Global instance of the warning logger.
Definition: Console.hh:251
String buffer for the base logger.
Definition: Console.hh:166
Output to stdout.
Definition: Console.hh:137
#define IGN_COMMON_WARN_RESUME__DLL_INTERFACE_MISSING
Definition: SuppressWarning.hh:67
A logger that outputs messages to a file.
Definition: Console.hh:65
LogType type
Destination type for the messages.
Definition: Console.hh:185
STL class.
static Logger err
Global instance of the error logger.
Definition: Console.hh:245
STL class.
LogType
Output destination type.
Definition: Console.hh:134
static Logger msg
Global instance of the message logger.
Definition: Console.hh:242
Forward declarations for the common classes.
static Logger dbg
Global instance of the debug logger.
Definition: Console.hh:248
STL class.
String buffer for the file logger.
Definition: Console.hh:100
static FileLogger log
Global instance of the file logger.
Definition: Console.hh:254
#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:115