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: 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 
96  private: std::ostream *stream;
97  };
98 
100  private: Console();
101 
103  public: virtual ~Console();
104 
106  public: static ConsolePtr Instance();
107 
109  public: static void Clear();
110 
113  public: void SetQuiet(bool _q);
114 
121  public: ConsoleStream &ColorMsg(const std::string &lbl,
122  const std::string &file,
123  unsigned int line, int color);
124 
130  public: ConsoleStream &Log(const std::string &lbl,
131  const std::string &file,
132  unsigned int line);
133 
136  private: std::unique_ptr<ConsolePrivate> dataPtr;
137  };
138 
142  {
144  public: ConsolePrivate() : msgStream(&std::cerr), logStream(nullptr) {}
145 
148 
151 
153  public: std::ofstream logFileStream;
154  };
155 
157  template <class T>
159  {
160  if (this->stream)
161  {
162  *this->stream << _rhs;
163  }
164 
165  if (Console::Instance()->dataPtr->logFileStream.is_open())
166  {
167  Console::Instance()->dataPtr->logFileStream << _rhs;
168  Console::Instance()->dataPtr->logFileStream.flush();
169  }
170 
171  return *this;
172  }
173  }
174 
176 }
177 
178 #ifdef _WIN32
179 #pragma warning(pop)
180 #endif
181 
182 #endif
Definition: Console.hh:141
std::shared_ptr< Console > ConsolePtr
Definition: Console.hh:65
ConsoleStream(std::ostream *_stream)
Constructor.
Definition: Console.hh:76
STL namespace.
ConsolePrivate()
Constructor.
Definition: Console.hh:144
Console::ConsoleStream msgStream
message stream
Definition: Console.hh:147
std::ostream & operator<<(std::ostream &os, ParamStreamer< T > s)
Definition: Param.hh:76
#define SDFORMAT_VISIBLE
Use to represent "symbol visible" if supported.
Definition: system_util.hh:48
std::ofstream logFileStream
logfile stream
Definition: Console.hh:153
Message, error, warning, and logging functionality.
Definition: Console.hh:68
An ostream-like class that we&#39;ll use for logging.
Definition: Console.hh:71
namespace for Simulation Description Format parser
Definition: Actor.hh:32
Console::ConsoleStream logStream
log stream
Definition: Console.hh:150