All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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/system_util.hh"
27 
28 #ifdef _WIN32
29 // Disable warning C4251 which is triggered by
30 // std::unique_ptr
31 #pragma warning(push)
32 #pragma warning(disable: 4251)
33 #endif
34 
35 namespace sdf
36 {
39 
41  #define sdfdbg (sdf::Console::Instance()->Log("Dbg", __FILE__, __LINE__))
42 
44  #define sdfmsg (sdf::Console::Instance()->ColorMsg("Msg", \
45  __FILE__, __LINE__, 32))
46 
48  #define sdfwarn (sdf::Console::Instance()->ColorMsg("Warning", \
49  __FILE__, __LINE__, 33))
50 
52  #define sdferr (sdf::Console::Instance()->ColorMsg("Error", \
53  __FILE__, __LINE__, 31))
54 
55  class ConsolePrivate;
56  class Console;
57 
60  typedef std::shared_ptr<Console> ConsolePtr;
61 
64  {
67  {
71  public: ConsoleStream(std::ostream *_stream) :
72  stream(_stream) {}
73 
78  public: template <class T>
79  ConsoleStream &operator<<(const T &_rhs);
80 
86  public: void Prefix(const std::string &_lbl,
87  const std::string &_file,
88  unsigned int _line, int _color);
89 
91  private: std::ostream *stream;
92  };
93 
95  private: Console();
96 
98  public: virtual ~Console();
99 
101  public: static ConsolePtr Instance();
102 
104  public: static void Clear();
105 
108  public: void SetQuiet(bool _q);
109 
116  public: ConsoleStream &ColorMsg(const std::string &lbl,
117  const std::string &file,
118  unsigned int line, int color);
119 
122  public: ConsoleStream &Log(const std::string &lbl,
123  const std::string &file,
124  unsigned int line);
125 
128  private: std::unique_ptr<ConsolePrivate> dataPtr;
129  };
130 
134  {
136  public: ConsolePrivate() : msgStream(&std::cerr), logStream(nullptr) {}
137 
140 
143 
145  public: std::ofstream logFileStream;
146  };
147 
149  template <class T>
151  {
152  if (this->stream)
153  {
154  *this->stream << _rhs;
155  }
156 
157  if (Console::Instance()->dataPtr->logFileStream.is_open())
158  {
159  Console::Instance()->dataPtr->logFileStream << _rhs;
160  Console::Instance()->dataPtr->logFileStream.flush();
161  }
162 
163  return *this;
164  }
165 
167 }
168 
169 #ifdef _WIN32
170 #pragma warning(pop)
171 #endif
172 
173 #endif
Console::ConsoleStream msgStream
message stream
Definition: Console.hh:139
An ostream-like class that we'll use for logging.
Definition: Console.hh:66
Message, error, warning, and logging functionality.
Definition: Console.hh:63
std::shared_ptr< Console > ConsolePtr
Definition: Console.hh:60
std::ofstream logFileStream
logfile stream
Definition: Console.hh:145
ConsolePrivate()
Constructor.
Definition: Console.hh:136
Definition: Console.hh:133
static ConsolePtr Instance()
Return an instance to this class.
Console::ConsoleStream logStream
log stream
Definition: Console.hh:142
#define SDFORMAT_VISIBLE
Use to represent "symbol visible" if supported.
Definition: system_util.hh:48
ConsoleStream & operator<<(const T &_rhs)
Redirect whatever is passed in to both our ostream (if non-NULL) and the log file (if open)...
ConsoleStream(std::ostream *_stream)
Constructor.
Definition: Console.hh:71