MIRA
LogCustomizableFormatter.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
47 #ifndef _MIRA_LOGCUSTOMIZABLEFORMATTER_H_
48 #define _MIRA_LOGCUSTOMIZABLEFORMATTER_H_
49 
50 #ifndef Q_MOC_RUN
51 #include <boost/date_time/local_time/local_time.hpp>
52 #endif
53 
54 #include <error/LoggingCore.h>
55 
56 namespace mira {
57 
59 
82 {
83 public:
84 
90  {
91  public:
92  virtual ~CustomFormatBase() {}
99  virtual void format(const LogRecord& record, std::ostringstream& ioStream) = 0;
100  };
101 
105  template< typename T>
107  {
108  public:
109  Generic(const T& iData) : data(iData) {}
110 
111  void format(const LogRecord& record, std::ostringstream& ioStream)
112  {
113  ioStream << data;
114  }
115 
116  T data;
117  };
118 
123  {
124  public:
129  Time(const std::string& iFormat = boost::local_time::local_time_facet::default_time_format)
130  {
131  using namespace boost::posix_time;
132  // using a time facet for time2string conversion here
133  // we could create a new time facet every time we format a new log entry
134  // but we gain some performance by using a shared_ptr to it.
135  // time_facet uses an internal ref counter that we need to set to one
136  // because locale deletes the pointer after the ref counter gets 0
137  timeFacet.reset(new time_facet(iFormat.c_str(),
138  time_facet::period_formatter_type(),
139  time_facet::special_values_formatter_type(),
140  time_facet::date_gen_formatter_type(), 1));
141  }
142 
143  void format(const LogRecord& record, std::ostringstream& ioStream)
144  {
145  std::ostringstream s;
146  // imbue the stream with the facet
147  // the ref counter of the facet gets incremented on imbue and
148  // decremented when the stream goes out of scope, but we are holding a ptr to
149  // it and have also set the ref counter to 1 in the constructor so the
150  // facet stays valid after leaving this method.
151  s.imbue(std::locale(s.getloc(), timeFacet.get()));
152  s << record.time;
153  ioStream << s.str();
154  }
155 
156  boost::shared_ptr<boost::posix_time::time_facet> timeFacet;
157  };
158 
163  {
164  public:
169  Uptime(const std::string& iFormat = boost::local_time::local_time_facet::default_time_duration_format)
170  {
171  using namespace boost::posix_time;
172  // using a time facet for time2string conversion here
173  // we could create a new time facet every time we format a new log entry
174  // but we gain some performance by using a shared_ptr to it.
175  // time_facet uses an internal ref counter that we need to set to one
176  // because locale deletes the pointer after the ref counter gets 0
177  timeFacet.reset(new time_facet(iFormat.c_str(),
178  time_facet::period_formatter_type(),
179  time_facet::special_values_formatter_type(),
180  time_facet::date_gen_formatter_type(), 1));
181  timeFacet->time_duration_format(iFormat.c_str());
182  }
183 
184  void format(const LogRecord& record, std::ostringstream& ioStream)
185  {
186  std::ostringstream s;
187  // imbue the stream with the facet
188  // the ref counter of the facet gets incremented on imbue and
189  // decremented when the stream goes out of scope, but we are holding a ptr to
190  // it and have also set the ref counter to 1 in the constructor so the
191  // facet stays valid after leaving this method.
192  s.imbue(std::locale(s.getloc(), timeFacet.get()));
193  s << record.uptime;
194  ioStream << s.str();
195  }
196 
197  boost::shared_ptr<boost::posix_time::time_facet> timeFacet;
198  };
199 
204  {
205  public:
206  void format(const LogRecord& record, std::ostringstream& ioStream)
207  {
208  ioStream << record.line;
209  }
210  };
211 
215  class File : public CustomFormatBase
216  {
217  public:
218  void format(const LogRecord& record, std::ostringstream& ioStream)
219  {
220  ioStream << record.file;
221  }
222  };
223 
228  {
229  public:
230  void format(const LogRecord& record, std::ostringstream& ioStream)
231  {
232  ioStream << MIRA_LOGGER.parseFunction(record).functionName;
233  }
234  };
235 
239  class Class : public CustomFormatBase
240  {
241  public:
242  void format(const LogRecord& record, std::ostringstream& stream)
243  {
244  stream << MIRA_LOGGER.parseFunction(record).className;
245  }
246  };
247 
252  {
253  public:
254  void format(const LogRecord& record, std::ostringstream& ioStream)
255  {
256  ioStream << MIRA_LOGGER.parseFunction(record).nameSpace;
257  }
258  };
259 
264  {
265  public:
266  void format(const LogRecord& record, std::ostringstream& ioStream)
267  {
268  ioStream << record.threadID;
269  }
270  };
271 
276  {
277  public:
278  void format(const LogRecord& record, std::ostringstream& ioStream)
279  {
280  ioStream << record.message;
281  }
282  };
283 
288  {
289  public:
290  void format(const LogRecord& record, std::ostringstream& ioStream)
291  {
292  ioStream << severityLevelStr[(int) record.level];
293  }
294  };
295 
296  virtual std::string format(const LogRecord& record)
297  {
298  std::ostringstream s;
299  for (size_t i = 0; i < mFormatter.size(); ++i)
300  mFormatter[i]->format(record, s);
301  return s.str();
302  }
303 
305  {
306  mFormatter.push_back(boost::shared_ptr<LogCustomizableFormatter::CustomFormatBase>(
307  new LogCustomizableFormatter::Generic<std::string>(std::string(data))));
308  return *this;
309  }
310 
312  {
313  mFormatter.push_back(boost::shared_ptr<LogCustomizableFormatter::CustomFormatBase>(
315  return *this;
316  }
317 
318  template<typename T>
320  {
321  mFormatter.push_back(boost::shared_ptr<LogCustomizableFormatter::CustomFormatBase>(new T(f)));
322  return *this;
323  }
324 
325 protected:
326  std::vector<boost::shared_ptr<LogCustomizableFormatter::CustomFormatBase> > mFormatter;
327 };
328 
330 
331 }
332 
333 #endif
SeverityLevel level
Definition: LoggingCore.h:109
std::vector< boost::shared_ptr< LogCustomizableFormatter::CustomFormatBase > > mFormatter
Definition: LogCustomizableFormatter.h:326
void format(const LogRecord &record, std::ostringstream &stream)
Called by LogCustomizableFormatter whenever a new log entry is getting formatted. ...
Definition: LogCustomizableFormatter.h:242
Holds all the information about a log entry.
Definition: LoggingCore.h:107
Duration uptime
Definition: LoggingCore.h:111
void format(const LogRecord &record, std::ostringstream &ioStream)
Called by LogCustomizableFormatter whenever a new log entry is getting formatted. ...
Definition: LogCustomizableFormatter.h:184
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
void format(const LogRecord &record, std::ostringstream &ioStream)
Called by LogCustomizableFormatter whenever a new log entry is getting formatted. ...
Definition: LogCustomizableFormatter.h:230
Generic(const T &iData)
Definition: LogCustomizableFormatter.h:109
Uptime format, used to write the up-time of the logging core to the log format.
Definition: LogCustomizableFormatter.h:162
boost::shared_ptr< boost::posix_time::time_facet > timeFacet
Definition: LogCustomizableFormatter.h:197
void format(const LogRecord &record, std::ostringstream &ioStream)
Called by LogCustomizableFormatter whenever a new log entry is getting formatted. ...
Definition: LogCustomizableFormatter.h:266
void format(const LogRecord &record, std::ostringstream &ioStream)
Called by LogCustomizableFormatter whenever a new log entry is getting formatted. ...
Definition: LogCustomizableFormatter.h:254
void format(const LogRecord &record, std::ostringstream &ioStream)
Called by LogCustomizableFormatter whenever a new log entry is getting formatted. ...
Definition: LogCustomizableFormatter.h:143
LogCustomizableFormatter & operator<<(const char *data)
Definition: LogCustomizableFormatter.h:304
void format(const LogRecord &record, std::ostringstream &ioStream)
Called by LogCustomizableFormatter whenever a new log entry is getting formatted. ...
Definition: LogCustomizableFormatter.h:206
LogCustomizableFormatter & operator<<(const T &f)
Definition: LogCustomizableFormatter.h:319
Severity format, used to write the severity level of the log entry to the log format.
Definition: LogCustomizableFormatter.h:287
virtual std::string format(const LogRecord &record)
Creates a formatted string out of a log entry.
Definition: LogCustomizableFormatter.h:296
int line
Definition: LoggingCore.h:114
std::string file
Definition: LoggingCore.h:115
Generic format, used to write various data to the log format.
Definition: LogCustomizableFormatter.h:106
Function format, used to write the function name of the log entry to the log format.
Definition: LogCustomizableFormatter.h:227
Message format, used to write the message of the log entry to the log format.
Definition: LogCustomizableFormatter.h:275
virtual ~CustomFormatBase()
Definition: LogCustomizableFormatter.h:92
void format(const LogRecord &record, std::ostringstream &ioStream)
Called by LogCustomizableFormatter whenever a new log entry is getting formatted. ...
Definition: LogCustomizableFormatter.h:290
Line format, used to write the line of the log entry to the log format.
Definition: LogCustomizableFormatter.h:203
Time(const std::string &iFormat=boost::local_time::local_time_facet::default_time_format)
Definition: LogCustomizableFormatter.h:129
Core class of the logging library.
boost::shared_ptr< boost::posix_time::time_facet > timeFacet
Definition: LogCustomizableFormatter.h:156
T data
Definition: LogCustomizableFormatter.h:116
std::string message
Definition: LoggingCore.h:112
void format(const LogRecord &record, std::ostringstream &ioStream)
Called by LogCustomizableFormatter whenever a new log entry is getting formatted. ...
Definition: LogCustomizableFormatter.h:278
virtual void format(const LogRecord &record, std::ostringstream &ioStream)=0
Called by LogCustomizableFormatter whenever a new log entry is getting formatted. ...
Time format, used to write the time of the log entry to the log format.
Definition: LogCustomizableFormatter.h:122
Abstract base class for sink formatters.
Definition: LoggingCore.h:155
ThreadID threadID
Definition: LoggingCore.h:117
const std::string severityLevelStr[]
String conversion for the enum severity types.
Definition: LoggingCore.h:85
Namespace format, used to write the name space of the log entry to the log format.
Definition: LogCustomizableFormatter.h:251
void format(const LogRecord &record, std::ostringstream &ioStream)
Called by LogCustomizableFormatter whenever a new log entry is getting formatted. ...
Definition: LogCustomizableFormatter.h:111
Time time
Definition: LoggingCore.h:110
LogCustomizableFormatter & operator<<(std::string &data)
Definition: LogCustomizableFormatter.h:311
File format, used to write the file of the log entry to the log format.
Definition: LogCustomizableFormatter.h:215
Class format, used to write the class name of the log entry to the log format.
Definition: LogCustomizableFormatter.h:239
Abstract base class for all CustomFormats.
Definition: LogCustomizableFormatter.h:89
ThreadID format, used to write the thread id of the log entry to the log format.
Definition: LogCustomizableFormatter.h:263
Uptime(const std::string &iFormat=boost::local_time::local_time_facet::default_time_duration_format)
Definition: LogCustomizableFormatter.h:169
A customizable formatter.
Definition: LogCustomizableFormatter.h:81
void format(const LogRecord &record, std::ostringstream &ioStream)
Called by LogCustomizableFormatter whenever a new log entry is getting formatted. ...
Definition: LogCustomizableFormatter.h:218
#define MIRA_LOGGER
Macro for easier access to the logging core instance.
Definition: LoggingCore.h:417