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 namespace sdf
30 {
33 
35  #define sdfdbg (sdf::Console::Instance()->Log("Dbg", \
36  __FILE__, __LINE__))
37 
39  #define sdfmsg (sdf::Console::Instance()->ColorMsg("Msg", \
40  __FILE__, __LINE__, 32))
41 
43  #define sdfwarn (sdf::Console::Instance()->ColorMsg("Warning", \
44  __FILE__, __LINE__, 33))
45 
47  #define sdferr (sdf::Console::Instance()->ColorMsg("Error", \
48  __FILE__, __LINE__, 31))
49 
50  class ConsolePrivate;
51  class Console;
52 
55  typedef std::shared_ptr<Console> ConsolePtr;
56 
59  {
62  {
66  public: ConsoleStream(std::ostream *_stream) :
67  stream(_stream) {}
68 
73  public: template <class T>
74  ConsoleStream &operator<<(const T &_rhs);
75 
81  public: void Prefix(const std::string &_lbl,
82  const std::string &_file,
83  unsigned int _line, int _color);
84 
86  private: std::ostream *stream;
87  };
88 
90  private: Console();
91 
93  public: virtual ~Console();
94 
96  public: static ConsolePtr Instance();
97 
100  public: void SetQuiet(bool _q);
101 
108  public: ConsoleStream &ColorMsg(const std::string &lbl,
109  const std::string &file,
110  unsigned int line, int color);
111 
114  public: ConsoleStream &Log(const std::string &lbl,
115  const std::string &file,
116  unsigned int line);
117 
118  private: std::unique_ptr<ConsolePrivate> dataPtr;
119  };
120 
124  {
126  public: ConsolePrivate() : msgStream(&std::cerr), logStream(NULL) {}
127 
130 
133 
135  public: std::ofstream logFileStream;
136  };
137 
139  template <class T>
141  {
142  if (this->stream)
143  *this->stream << _rhs;
144 
145  if (Console::Instance()->dataPtr->logFileStream.is_open())
146  {
147  Console::Instance()->dataPtr->logFileStream << _rhs;
148  Console::Instance()->dataPtr->logFileStream.flush();
149  }
150 
151  return *this;
152  }
153 
154 
156 }
157 #endif
Console::ConsoleStream msgStream
message stream
Definition: Console.hh:129
An ostream-like class that we'll use for logging.
Definition: Console.hh:61
Message, error, warning, and logging functionality.
Definition: Console.hh:58
std::shared_ptr< Console > ConsolePtr
Definition: Console.hh:55
std::ofstream logFileStream
logfile stream
Definition: Console.hh:135
ConsolePrivate()
Constructor.
Definition: Console.hh:126
Definition: Console.hh:123
static ConsolePtr Instance()
Return an instance to this class.
Console::ConsoleStream logStream
log stream
Definition: Console.hh:132
#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:66