Loading...
Searching...
No Matches
Console.hh
Go to the documentation of this file.
1/*
2 * Copyright 2011 Nate Koenig
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
18#ifndef SDF_CONSOLE_HH_
19#define SDF_CONSOLE_HH_
20
21#include <fstream>
22#include <iostream>
23#include <memory>
24#include <string>
25
26#include <sdf/sdf_config.h>
27#include "sdf/system_util.hh"
28
29#ifdef _WIN32
30// Disable warning C4251 which is triggered by
31// std::unique_ptr
32#pragma warning(push)
33#pragma warning(disable: 4251)
34#endif
35
36namespace sdf
37{
38 // Inline bracket to help doxygen filtering.
39 inline namespace SDF_VERSION_NAMESPACE {
40 //
41
44
46 #define sdfdbg (sdf::Console::Instance()->Log("Dbg", __FILE__, __LINE__))
47
49 #define sdfmsg (sdf::Console::Instance()->ColorMsg("Msg", \
50 __FILE__, __LINE__, 32))
51
53 #define sdfwarn (sdf::Console::Instance()->ColorMsg("Warning", \
54 __FILE__, __LINE__, 33))
55
57 #define sdferr (sdf::Console::Instance()->ColorMsg("Error", \
58 __FILE__, __LINE__, 31))
59
60 class ConsolePrivate;
61 class Console;
62
65 typedef std::shared_ptr<Console> ConsolePtr;
66
69 {
72 {
76 public: explicit ConsoleStream(std::ostream *_stream) :
77 stream(_stream) {}
78
83 public: template <class T>
84 ConsoleStream &operator<<(const T &_rhs);
85
91 public: void Prefix(const std::string &_lbl,
92 const std::string &_file,
93 unsigned int _line, int _color);
94
99 public: void SetStream(std::ostream *_stream);
100
103 public: std::ostream *GetStream();
104
106 private: std::ostream *stream;
107 };
108
110 private: Console();
111
113 public: virtual ~Console();
114
116 public: static ConsolePtr Instance();
117
119 public: static void Clear();
120
123 public: void SetQuiet(bool _q);
124
131 public: ConsoleStream &ColorMsg(const std::string &lbl,
132 const std::string &file,
133 unsigned int line, int color);
134
140 public: ConsoleStream &Log(const std::string &lbl,
141 const std::string &file,
142 unsigned int line);
143
149
155
158 private: std::unique_ptr<ConsolePrivate> dataPtr;
159 };
160
164 {
166 public: ConsolePrivate() : msgStream(&std::cerr), logStream(nullptr) {}
167
170
173
175 public: std::ofstream logFileStream;
176 };
177
179 template <class T>
181 {
182 if (this->stream)
183 {
184 *this->stream << _rhs;
185 }
186
187 if (Console::Instance()->dataPtr->logFileStream.is_open())
188 {
189 Console::Instance()->dataPtr->logFileStream << _rhs;
190 Console::Instance()->dataPtr->logFileStream.flush();
191 }
192
193 return *this;
194 }
195 }
196
198}
199
200#ifdef _WIN32
201#pragma warning(pop)
202#endif
203
204#endif
Definition Console.hh:164
An ostream-like class that we'll use for logging.
Definition Console.hh:72
Message, error, warning, and logging functionality.
Definition Console.hh:69
std::ofstream logFileStream
logfile stream
Definition Console.hh:175
void SetQuiet(bool _q)
Set quiet output.
Console::ConsoleStream msgStream
message stream
Definition Console.hh:169
void Prefix(const std::string &_lbl, const std::string &_file, unsigned int _line, int _color)
Print a prefix to both terminal and log file.
ConsoleStream & Log(const std::string &lbl, const std::string &file, unsigned int line)
Use this to output a message to a log file at $HOME/.sdformat/sdformat.log.
ConsoleStream & operator<<(const T &_rhs)
Redirect whatever is passed in to both our ostream (if non-NULL) and the log file (if open).
ConsolePrivate()
Constructor.
Definition Console.hh:166
ConsoleStream & GetMsgStream()
Get the current message stream object.
ConsoleStream & ColorMsg(const std::string &lbl, const std::string &file, unsigned int line, int color)
Use this to output a colored message to the terminal.
std::shared_ptr< Console > ConsolePtr
Definition Console.hh:65
ConsoleStream & GetLogStream()
Get the current log stream object.
static void Clear()
Clear out the current console to make room for a new one.
static ConsolePtr Instance()
Return an instance to this class.
ConsoleStream(std::ostream *_stream)
Constructor.
Definition Console.hh:76
std::ostream * GetStream()
Get the current stream object.
void SetStream(std::ostream *_stream)
Set the stream object.
Console::ConsoleStream logStream
log stream
Definition Console.hh:172
namespace for Simulation Description Format parser
Definition Actor.hh:35
#define SDFORMAT_VISIBLE
Use to represent "symbol visible" if supported.
Definition system_util.hh:25