All Classes Namespaces Files Functions Variables Typedefs Friends Macros Modules
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 
52  {
55  {
59  public: ConsoleStream(std::ostream *_stream) :
60  stream(_stream) {}
61 
66  public: template <class T>
67  ConsoleStream &operator<<(const T &_rhs)
68  {
69  if (this->stream)
70  *this->stream << _rhs;
71 
72  if (Console::Instance()->logFileStream.is_open())
73  {
74  Console::Instance()->logFileStream << _rhs;
75  Console::Instance()->logFileStream.flush();
76  }
77 
78  return *this;
79  }
80 
86  public: void Prefix(const std::string &_lbl,
87  const std::string &_file,
88  unsigned int _line, int _color)
89  {
90  int index = _file.find_last_of("/") + 1;
91 
92  if (this->stream)
93  {
94  *this->stream << "\033[1;" << _color << "m" << _lbl << " [" <<
95  _file.substr(index , _file.size() - index)<< ":" << _line <<
96  "]\033[0m ";
97  }
98 
99  if (Console::Instance()->logFileStream.is_open())
100  {
101  Console::Instance()->logFileStream << _lbl << " [" <<
102  _file.substr(index , _file.size() - index)<< ":" << _line << "] ";
103  }
104  }
105 
107  private: std::ostream *stream;
108  };
109 
111  private: Console();
112 
114  public: virtual ~Console();
115 
117  public: static boost::shared_ptr<Console> Instance();
118 
121  public: void SetQuiet(bool _q);
122 
129  public: ConsoleStream &ColorMsg(const std::string &lbl,
130  const std::string &file,
131  unsigned int line, int color);
132 
135  public: ConsoleStream &Log(const std::string &lbl,
136  const std::string &file,
137  unsigned int line);
138 
140  private: ConsoleStream msgStream;
141 
143  private: ConsoleStream logStream;
144 
146  private: std::ofstream logFileStream;
147 
149  private: static boost::shared_ptr<Console> myself;
150  };
152 }
153 #endif
An ostream-like class that we'll use for logging.
Definition: Console.hh:54
Message, error, warning, and logging functionality.
Definition: Console.hh:51
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:86
#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)...
Definition: Console.hh:67
ConsoleStream(std::ostream *_stream)
Constructor.
Definition: Console.hh:59
namespace for Simulation Description Format parser
Definition: Console.hh:29
static Console * Instance()
Return an instance to this class.