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 namespace sdf
28 {
31 
33  #define sdfdbg (sdf::Console::Instance()->Log("Dbg", \
34  __FILE__, __LINE__))
35 
37  #define sdfmsg (sdf::Console::Instance()->ColorMsg("Msg", \
38  __FILE__, __LINE__, 32))
39 
41  #define sdfwarn (sdf::Console::Instance()->ColorMsg("Warning", \
42  __FILE__, __LINE__, 33))
43 
45  #define sdferr (sdf::Console::Instance()->ColorMsg("Error", \
46  __FILE__, __LINE__, 31))
47 
49  class Console
50  {
52  public: class ConsoleStream
53  {
57  public: ConsoleStream(std::ostream *_stream) :
58  stream(_stream) {}
59 
64  public: template <class T>
65  ConsoleStream &operator<<(const T &_rhs)
66  {
67  if (this->stream)
68  *this->stream << _rhs;
69 
70  if (Console::Instance()->logFileStream.is_open())
71  Console::Instance()->logFileStream << _rhs;
72 
73  return *this;
74  }
75 
81  public: void Prefix(const std::string &_lbl,
82  const std::string &_file,
83  unsigned int _line, int _color)
84  {
85  int index = _file.find_last_of("/") + 1;
86 
87  if (this->stream)
88  {
89  *this->stream << "\033[1;" << _color << "m" << _lbl << " [" <<
90  _file.substr(index , _file.size() - index)<< ":" << _line <<
91  "]\033[0m ";
92  }
93 
94  if (Console::Instance()->logFileStream.is_open())
95  {
96  Console::Instance()->logFileStream << _lbl << " [" <<
97  _file.substr(index , _file.size() - index)<< ":" << _line << "] ";
98  }
99  }
100 
102  private: std::ostream *stream;
103  };
104 
106  private: Console();
107 
109  public: virtual ~Console();
110 
112  public: static boost::shared_ptr<Console> Instance();
113 
116  public: void SetQuiet(bool _q);
117 
124  public: ConsoleStream &ColorMsg(const std::string &lbl,
125  const std::string &file,
126  unsigned int line, int color);
127 
130  public: ConsoleStream &Log(const std::string &lbl,
131  const std::string &file,
132  unsigned int line);
133 
135  private: ConsoleStream msgStream;
136 
138  private: ConsoleStream logStream;
139 
141  private: std::ofstream logFileStream;
142 
144  private: static boost::shared_ptr<Console> myself;
145  };
147 }
148 #endif
An ostream-like class that we'll use for logging.
Definition: Console.hh:52
Message, error, warning, and logging functionality.
Definition: Console.hh:49
ConsoleStream & ColorMsg(const std::string &lbl, const std::string &file, unsigned int line, int color)
Use this to output a colored message to the terminal.
ConsoleStream & Log(const std::string &lbl, const std::string &file, unsigned int line)
Use this to output a message to a log file.
void Prefix(const std::string &_lbl, const std::string &_file, unsigned int _line, int _color)
Print a prefix to both terminal and log file.
Definition: Console.hh:81
void SetQuiet(bool _q)
Set quiet output.
ConsoleStream & operator<<(const T &_rhs)
Redirect whatever is passed in to both our ostream (if non-NULL) and the log file (if open)...
Definition: Console.hh:65
ConsoleStream(std::ostream *_stream)
Constructor.
Definition: Console.hh:57
virtual ~Console()
Destructor.
static Console * Instance()
Return an instance to this class.