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 
137  public: ConsoleStream &Log(const std::string &lbl,
138  const std::string &file,
139  unsigned int line);
140 
145  public: ConsoleStream &GetMsgStream();
146 
151  public: ConsoleStream &GetLogStream();
152 
155  private: std::unique_ptr<ConsolePrivate> dataPtr;
156  };
157 
161  {
163  public: ConsolePrivate() : msgStream(&std::cerr), logStream(nullptr) {}
164 
167 
170 
172  public: std::ofstream logFileStream;
173  };
174 
176  template <class T>
178  {
179  if (this->stream)
180  {
181  *this->stream << _rhs;
182  }
183 
184  if (Console::Instance()->dataPtr->logFileStream.is_open())
185  {
186  Console::Instance()->dataPtr->logFileStream << _rhs;
187  Console::Instance()->dataPtr->logFileStream.flush();
188  }
189 
190  return *this;
191  }
192  }
193 
195 }
196 
197 #ifdef _WIN32
198 #pragma warning(pop)
199 #endif
200 
201 #endif
sdf::v9::ConsolePrivate::logStream
Console::ConsoleStream logStream
log stream
Definition: Console.hh:169
sdf
namespace for Simulation Description Format parser
Definition: Actor.hh:32
sdf::v9::operator<<
std::ostream & operator<<(std::ostream &os, ParamStreamer< T > s)
Definition: Param.hh:75
sdf::v9::ConsolePrivate::msgStream
Console::ConsoleStream msgStream
message stream
Definition: Console.hh:166
sdf::v9::Console::ConsoleStream
An ostream-like class that we'll use for logging.
Definition: Console.hh:71
sdf::v9::Console
Message, error, warning, and logging functionality.
Definition: Console.hh:68
sdf::v9::ConsolePrivate
Definition: Console.hh:160
SDFORMAT_VISIBLE
#define SDFORMAT_VISIBLE
Definition: system_util.hh:48
sdf::v9::ConsolePtr
std::shared_ptr< Console > ConsolePtr
Definition: Console.hh:65
sdf::v9::Console::ConsoleStream::ConsoleStream
ConsoleStream(std::ostream *_stream)
Constructor.
Definition: Console.hh:76
sdf::v9::ConsolePrivate::ConsolePrivate
ConsolePrivate()
Constructor.
Definition: Console.hh:163
system_util.hh
sdf::v9::ConsolePrivate::logFileStream
std::ofstream logFileStream
logfile stream
Definition: Console.hh:172