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 <boost/shared_ptr.hpp>
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 
54  {
57  {
61  public: ConsoleStream(std::ostream *_stream) :
62  stream(_stream) {}
63 
68  public: template <class T>
69  ConsoleStream &operator<<(const T &_rhs);
70 
76  public: void Prefix(const std::string &_lbl,
77  const std::string &_file,
78  unsigned int _line, int _color);
79 
81  private: std::ostream *stream;
82  };
83 
85  private: Console();
86 
88  public: virtual ~Console();
89 
91  public: static boost::shared_ptr<Console> Instance();
92 
95  public: void SetQuiet(bool _q);
96 
103  public: ConsoleStream &ColorMsg(const std::string &lbl,
104  const std::string &file,
105  unsigned int line, int color);
106 
109  public: ConsoleStream &Log(const std::string &lbl,
110  const std::string &file,
111  unsigned int line);
112 
113  private: ConsolePrivate *dataPtr;
114  };
115 
119  {
121  public: ConsolePrivate() : msgStream(&std::cerr), logStream(NULL) {}
122 
125 
128 
130  public: std::ofstream logFileStream;
131  };
132 
134  template <class T>
136  {
137  if (this->stream)
138  *this->stream << _rhs;
139 
140  if (Console::Instance()->dataPtr->logFileStream.is_open())
141  {
142  Console::Instance()->dataPtr->logFileStream << _rhs;
143  Console::Instance()->dataPtr->logFileStream.flush();
144  }
145 
146  return *this;
147  }
148 
149 
151 }
152 #endif
Console::ConsoleStream msgStream
message stream
Definition: Console.hh:124
An ostream-like class that we'll use for logging.
Definition: Console.hh:56
Message, error, warning, and logging functionality.
Definition: Console.hh:53
std::ofstream logFileStream
logfile stream
Definition: Console.hh:130
ConsolePrivate()
Constructor.
Definition: Console.hh:121
Definition: Console.hh:118
Console::ConsoleStream logStream
log stream
Definition: Console.hh:127
#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:61
static Console * Instance()
Return an instance to this class.