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 
36 namespace 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 
148  public: ConsoleStream &GetMsgStream();
149 
154  public: ConsoleStream &GetLogStream();
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
sdf
namespace for Simulation Description Format parser
Definition: Actor.hh:34
sdf::SDF_VERSION_NAMESPACE::ConsolePrivate::ConsolePrivate
ConsolePrivate()
Constructor.
Definition: Console.hh:166
sdf::SDF_VERSION_NAMESPACE::Console::ConsoleStream::operator<<
ConsoleStream & operator<<(const T &_rhs)
Redirect whatever is passed in to both our ostream (if non-NULL) and the log file (if open).
sdf::SDF_VERSION_NAMESPACE::ConsolePrivate
Definition: Console.hh:163
sdf::SDF_VERSION_NAMESPACE::Console::Instance
static ConsolePtr Instance()
Return an instance to this class.
sdf::SDF_VERSION_NAMESPACE::Console::ConsoleStream
An ostream-like class that we'll use for logging.
Definition: Console.hh:71
sdf_config.h
SDFORMAT_VISIBLE
#define SDFORMAT_VISIBLE
Definition: system_util.hh:25
sdf::SDF_VERSION_NAMESPACE::Console
Message, error, warning, and logging functionality.
Definition: Console.hh:68
sdf::SDF_VERSION_NAMESPACE::ConsolePtr
std::shared_ptr< Console > ConsolePtr
Definition: Console.hh:65
sdf::SDF_VERSION_NAMESPACE::operator<<
std::ostream & operator<<(std::ostream &os, ParamStreamer< T > s)
Definition: Param.hh:94
sdf::SDF_VERSION_NAMESPACE::ConsolePrivate::logFileStream
std::ofstream logFileStream
logfile stream
Definition: Console.hh:175
sdf::SDF_VERSION_NAMESPACE::Console::ConsoleStream::ConsoleStream
ConsoleStream(std::ostream *_stream)
Constructor.
Definition: Console.hh:76
system_util.hh
sdf::SDF_VERSION_NAMESPACE::ConsolePrivate::msgStream
Console::ConsoleStream msgStream
message stream
Definition: Console.hh:169
sdf::SDF_VERSION_NAMESPACE::ConsolePrivate::logStream
Console::ConsoleStream logStream
log stream
Definition: Console.hh:172