All Classes Namespaces Files Functions Variables Typedefs 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 <iostream>
22 #include <fstream>
23 #include <string>
24 
25 #include <memory>
26 
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 {
40 
42  #define sdfdbg (sdf::Console::Instance()->Log("Dbg", \
43  __FILE__, __LINE__))
44 
46  #define sdfmsg (sdf::Console::Instance()->ColorMsg("Msg", \
47  __FILE__, __LINE__, 32))
48 
50  #define sdfwarn (sdf::Console::Instance()->ColorMsg("Warning", \
51  __FILE__, __LINE__, 33))
52 
54  #define sdferr (sdf::Console::Instance()->ColorMsg("Error", \
55  __FILE__, __LINE__, 31))
56 
57  class ConsolePrivate;
58  class Console;
59 
62  typedef std::shared_ptr<Console> ConsolePtr;
63 
66  {
69  {
73  public: ConsoleStream(std::ostream *_stream) :
74  stream(_stream) {}
75 
80  public: template <class T>
81  ConsoleStream &operator<<(const T &_rhs);
82 
88  public: void Prefix(const std::string &_lbl,
89  const std::string &_file,
90  unsigned int _line, int _color);
91 
93  private: std::ostream *stream;
94  };
95 
97  private: Console();
98 
100  public: virtual ~Console();
101 
103  public: static ConsolePtr Instance();
104 
107  public: void SetQuiet(bool _q);
108 
115  public: ConsoleStream &ColorMsg(const std::string &lbl,
116  const std::string &file,
117  unsigned int line, int color);
118 
121  public: ConsoleStream &Log(const std::string &lbl,
122  const std::string &file,
123  unsigned int line);
124 
127  private: std::unique_ptr<ConsolePrivate> dataPtr;
128  };
129 
133  {
135  public: ConsolePrivate() : msgStream(&std::cerr), logStream(NULL) {}
136 
139 
142 
144  public: std::ofstream logFileStream;
145  };
146 
148  template <class T>
150  {
151  if (this->stream)
152  *this->stream << _rhs;
153 
154  if (Console::Instance()->dataPtr->logFileStream.is_open())
155  {
156  Console::Instance()->dataPtr->logFileStream << _rhs;
157  Console::Instance()->dataPtr->logFileStream.flush();
158  }
159 
160  return *this;
161  }
162 
164 }
165 
166 #ifdef _WIN32
167 #pragma warning(pop)
168 #endif
169 
170 #endif
Console::ConsoleStream msgStream
message stream
Definition: Console.hh:138
An ostream-like class that we'll use for logging.
Definition: Console.hh:68
Message, error, warning, and logging functionality.
Definition: Console.hh:65
std::shared_ptr< Console > ConsolePtr
Definition: Console.hh:62
std::ofstream logFileStream
logfile stream
Definition: Console.hh:144
ConsolePrivate()
Constructor.
Definition: Console.hh:135
Definition: Console.hh:132
static ConsolePtr Instance()
Return an instance to this class.
Console::ConsoleStream logStream
log stream
Definition: Console.hh:141
#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:73