XBotInterface  2.4.1
XBotInterface provides a generic API to model and control a robot.
RtLog.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017 IIT-ADVR
3  * Author: Arturo Laurenzi
4  * email: arturo.laurenzi@iit.it
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>
18 */
19 
20 #ifndef __XBOT_RT_LOGGER_HPP__
21 #define __XBOT_RT_LOGGER_HPP__
22 
23 
24 #include <iostream>
25 #include <stdarg.h>
26 #include <stdio.h>
27 
28 #include <functional>
29 
30 #include <boost/iostreams/stream.hpp>
31 #include <boost/iostreams/device/array.hpp>
32 
33 
34 namespace XBot {
35 
36  /* Modifiers */
37  std::ostream& bold_on(std::ostream& os);
38 
39  std::ostream& bold_off(std::ostream& os);
40 
41  std::ostream& color_green(std::ostream& os);
42 
43  std::ostream& color_red(std::ostream& os);
44 
45  std::ostream& color_yellow(std::ostream& os);
46 
47  std::ostream& color_reset(std::ostream& os);
48 
53  class LoggerClass;
54 
55 
60  class Endl {
61 
62  public:
63 
64  friend class LoggerClass;
65 
66  friend void operator<< ( std::ostream& os, Endl& endl );
67 
68  private:
69 
70  Endl(LoggerClass& logger_handle);
71 
72  LoggerClass& _logger_handle;
73 
74  void print();
75 
76  };
77 
78 
79 
80  class Logger {
81 
82  public:
83 
84  enum class Severity { DEBUG = -1, LOW = 0, MID = 1, HIGH = 2, FATAL = 3 };
85 
92  static std::ostream& log();
93 
100  static std::ostream& info(Logger::Severity s = Logger::Severity::LOW);
101 
109  static void info(Logger::Severity s, const char * fmt, ...);
110 
116  static void info(const char * fmt, ...);
117 
124  static std::ostream& error(Logger::Severity s = Logger::Severity::HIGH);
125 
133  static void error(Logger::Severity s, const char * fmt, ...);
134 
140  static void error(const char * fmt, ...);
141 
148  static std::ostream& warning(Logger::Severity s = Logger::Severity::MID);
149 
157  static void warning(Logger::Severity s, const char * fmt, ...);
158 
164  static void warning(const char * fmt, ...);
165 
172  static std::ostream& success(Logger::Severity s = Logger::Severity::LOW);
173 
181  static void success(Logger::Severity s, const char * fmt, ...);
182 
188  static void success(const char * fmt, ...);
189 
190 
194  static Endl& endl();
195 
200  static void SetVerbosityLevel(Logger::Severity s);
201 
207 
212  static void SetOnPrintCallback(std::function<void(char*, int, Logger::Severity)> f);
213 
214 
215  protected:
216 
217  private:
218 
219  Logger() = delete;
220 
221  static LoggerClass _logger;
222 
223  };
224 
225 
230  class LoggerClass {
231 
232  public:
233 
234  friend class Endl;
235 
236  friend class Logger;
237 
238  typedef std::shared_ptr<LoggerClass> Ptr;
239 
240  typedef std::function<void(char*, int, Logger::Severity)> OnPrintCallback;
241 
242  static void DefaultOnPrint(char *, int, Logger::Severity);
243 
244  LoggerClass(std::string logger_name);
245  LoggerClass(std::string logger_name, OnPrintCallback f);
246 
247  ~LoggerClass();
248 
255  std::ostream& log();
256 
263  std::ostream& info(Logger::Severity s = Logger::Severity::LOW);
264 
272  void info(Logger::Severity s, const char * fmt, ...);
273 
279  void info(const char * fmt, ...);
280 
287  std::ostream& error(Logger::Severity s = Logger::Severity::HIGH);
288 
296  void error(Logger::Severity s, const char * fmt, ...);
297 
303  void error(const char * fmt, ...);
304 
312 
320  void warning(Logger::Severity s, const char * fmt, ...);
321 
327  void warning(const char * fmt, ...);
328 
336 
344  void success(Logger::Severity s, const char * fmt, ...);
345 
351  void success(const char * fmt, ...);
352 
353 
357  Endl& endl();
358 
364 
370 
375  void setOnPrintCallback(std::function<void(char*, int, Logger::Severity)> f);
376 
377 
378 
379  private:
380 
381  typedef boost::iostreams::stream<boost::iostreams::array_sink> ostream_t;
382 
383  void print();
384 
385  void init_sink();
386 
387  void __info(Logger::Severity s, const char * fmt, va_list args);
388  void __error(Logger::Severity s, const char * fmt, va_list args);
389  void __warning(Logger::Severity s, const char * fmt, va_list args);
390  void __success(Logger::Severity s, const char * fmt, va_list args);
391  void __fmt_print(const char * fmt, va_list args);
392 
393  static const int BUFFER_SIZE = 4096;
394 
395  char _buffer[BUFFER_SIZE];
396 
397  ostream_t _sink;
398 
399  Endl _endl;
400 
401  std::string _name, _name_tag;
402  Logger::Severity _severity;
403  Logger::Severity _verbosity_level;
404 
405  std::function<void(char*, int, Logger::Severity)> _on_print;
406 
407  };
408 
409 
410 
411 
412 
413 }
414 
415 
416 #endif
XBot::bold_off
std::ostream & bold_off(std::ostream &os)
Definition: RtLog.cpp:177
XBot::LoggerClass::LoggerClass
LoggerClass(std::string logger_name)
Definition: RtLog.cpp:219
XBot::color_red
std::ostream & color_red(std::ostream &os)
Definition: RtLog.cpp:187
XBot::Logger::endl
static Endl & endl()
Closes the message and prints to screen.
Definition: RtLog.cpp:107
XBot::Logger::GetVerbosityLevel
static Logger::Severity GetVerbosityLevel()
Sets the global verbosity level, i.e.
Definition: RtLog.cpp:161
XBot::LoggerClass::error
std::ostream & error(Logger::Severity s=Logger::Severity::HIGH)
Logs an error message (in red, with bold [ERROR] header).
Definition: RtLog.cpp:333
XBot::Logger::info
static std::ostream & info(Logger::Severity s=Logger::Severity::LOW)
Logs an information message (with bold [INFO] header).
Definition: RtLog.cpp:112
XBot::Logger::SetVerbosityLevel
static void SetVerbosityLevel(Logger::Severity s)
Sets the global verbosity level, i.e.
Definition: RtLog.cpp:146
XBot::Logger::error
static std::ostream & error(Logger::Severity s=Logger::Severity::HIGH)
Logs an error message (in red, with bold [ERROR] header).
Definition: RtLog.cpp:41
XBot::Logger::SetOnPrintCallback
static void SetOnPrintCallback(std::function< void(char *, int, Logger::Severity)> f)
Sets the on print callback.
Definition: RtLog.cpp:166
XBot::Logger::Severity::HIGH
@ HIGH
XBot::color_yellow
std::ostream & color_yellow(std::ostream &os)
Definition: RtLog.cpp:192
XBot::LoggerClass
Logger class.
Definition: RtLog.hpp:230
XBot::Logger::warning
static std::ostream & warning(Logger::Severity s=Logger::Severity::MID)
Logs a warning message (in yellow, with bold [warning] header).
Definition: RtLog.cpp:156
XBot::Logger::Severity
Severity
Definition: RtLog.hpp:84
XBot::Logger::success
static std::ostream & success(Logger::Severity s=Logger::Severity::LOW)
Logs a success message (in green, with bold [OK] header).
Definition: RtLog.cpp:151
XBot::color_green
std::ostream & color_green(std::ostream &os)
Definition: RtLog.cpp:182
XBot::Logger
Definition: RtLog.hpp:80
XBot::LoggerClass::DefaultOnPrint
static void DefaultOnPrint(char *, int, Logger::Severity)
Definition: RtLog.cpp:213
XBot::LoggerClass::setVerbosityLevel
void setVerbosityLevel(Logger::Severity s)
Sets the global verbosity level, i.e.
Definition: RtLog.cpp:458
XBot::LoggerClass::warning
std::ostream & warning(Logger::Severity s=Logger::Severity::MID)
Logs a warning message (in yellow, with bold [WARNING] header).
Definition: RtLog.cpp:374
XBot::Endl::operator<<
friend void operator<<(std::ostream &os, Endl &endl)
Definition: RtLog.cpp:259
XBot::LoggerClass::info
std::ostream & info(Logger::Severity s=Logger::Severity::LOW)
Logs an information message (with bold [INFO] header).
Definition: RtLog.cpp:280
XBot::Logger::log
static std::ostream & log()
Writes to the internal stream with no special formatting and without changing the severity level (whi...
Definition: RtLog.cpp:140
XBot::LoggerClass::endl
Endl & endl()
Closes the message and prints to screen.
Definition: RtLog.cpp:455
XBot::LoggerClass::Ptr
std::shared_ptr< LoggerClass > Ptr
Definition: RtLog.hpp:238
XBot::color_reset
std::ostream & color_reset(std::ostream &os)
Definition: RtLog.cpp:197
XBot::LoggerClass::success
std::ostream & success(Logger::Severity s=Logger::Severity::LOW)
Logs a success message (in green, with bold [OK] header).
Definition: RtLog.cpp:413
XBot::LoggerClass::setOnPrintCallback
void setOnPrintCallback(std::function< void(char *, int, Logger::Severity)> f)
Sets the on print callback.
Definition: RtLog.cpp:470
XBot::bold_on
std::ostream & bold_on(std::ostream &os)
Definition: RtLog.cpp:172
XBot::LoggerClass::OnPrintCallback
std::function< void(char *, int, Logger::Severity)> OnPrintCallback
Definition: RtLog.hpp:240
XBot::LoggerClass::~LoggerClass
~LoggerClass()
Definition: RtLog.cpp:246
XBot::Endl
Class handling the flushing of log messages to console.
Definition: RtLog.hpp:60
XBot::Logger::Severity::FATAL
@ FATAL
XBot::LoggerClass::getVerbosityLevel
Logger::Severity getVerbosityLevel() const
Sets the global verbosity level, i.e.
Definition: RtLog.cpp:463
XBot::Logger::Severity::LOW
@ LOW
XBot::Logger::Severity::MID
@ MID
XBot
Definition: IXBotModel.h:20
XBot::LoggerClass::log
std::ostream & log()
Writes to the internal stream with no special formatting and without changing the severity level (whi...
Definition: RtLog.cpp:271
XBot::Logger::Severity::DEBUG
@ DEBUG