humoto
Public Member Functions | Public Attributes | Static Public Attributes | Protected Attributes | Private Member Functions | List of all members
humoto::Logger Class Reference

Detailed Description

Threaded logger: any data sent to this logger is wrapped in a message and pushed to a queue, the queue is processed in a separate thread. Hence, time consuming output does not delay execution of the main thread(s).

Attention
A high frequency controller may easily flood the logger with data.
This logger uses mutexes, so it should be possible to use a single logger in multiple threads, however, this is not recommended.
Todo:
Consider using a nonblocking queue, e.g., <boost/lockfree/queue.hpp>.

Definition at line 555 of file logger.h.

#include <logger.h>

Inheritance diagram for humoto::Logger:
Inheritance graph

Public Member Functions

 Logger (const std::size_t thread_sleep_ms=default_thread_sleep_ms_)
 Constructor. More...
 
 Logger (std::ostream &output_stream, const std::size_t thread_sleep_ms=default_thread_sleep_ms_)
 Constructor. More...
 
 Logger (const std::string &output_filename, const std::size_t thread_sleep_ms=default_thread_sleep_ms_)
 Constructor. More...
 
 ~Logger ()
 Destructor. More...
 
void initialize (std::ostream &output_stream)
 initialize More...
 
void initialize (const std::string &output_filename)
 initialize More...
 
void initialize (std::ostream &output_stream, const std::size_t thread_sleep_ms=default_thread_sleep_ms_)
 initialize More...
 
void initialize (const std::string &output_filename, const std::size_t thread_sleep_ms=default_thread_sleep_ms_)
 initialize More...
 
template<typename t_Data >
void log (const t_Data &data, EIGENTOOLS_EIGENTYPE_DISABLER_TYPE(t_Data) *dummy=NULL)
 Log raw data. More...
 
template<typename t_Derived >
void log (const Eigen::DenseBase< t_Derived > &matrix)
 Log entry (Eigen matrix) More...
 
void log (const char *string)
 Log raw data (char string) More...
 
template<typename t_Data >
void log (const LogEntryName &name, const t_Data &data, EIGENTOOLS_EIGENTYPE_DISABLER_TYPE(t_Data) *dummy=NULL)
 Log data (fundamental types) More...
 
template<typename t_Derived >
void log (const LogEntryName &name, const Eigen::DenseBase< t_Derived > &matrix)
 Log entry (Eigen matrix) More...
 
void log (const LogEntryName &name, const char *string)
 Log data (char string) More...
 

Public Attributes

bool interrupted_
 
std::queue< LogMessageBase * > message_queue_
 
boost::mutex message_queue_mutex_
 
boost::thread processor_thread_
 
std::size_t thread_sleep_ms_
 

Static Public Attributes

static const std::size_t default_thread_sleep_ms_ = 2
 

Protected Attributes

std::ostream * output_stream_
 Output stream. More...
 

Private Member Functions

void interrupt ()
 Stop the logging thread if necessary. More...
 
void processQueue ()
 This function is running in a separate thread. More...
 
void pushMessage (LogMessageBase *msg)
 Push message to a queue. More...
 
void start ()
 Start the thread. More...
 

Constructor & Destructor Documentation

◆ Logger() [1/3]

humoto::Logger::Logger ( const std::size_t  thread_sleep_ms = default_thread_sleep_ms_)
inlineexplicit

Constructor.

Parameters
[in]thread_sleep_mshow long the queue processing thread should sleep if there is no data

Definition at line 670 of file logger.h.

◆ Logger() [2/3]

humoto::Logger::Logger ( std::ostream &  output_stream,
const std::size_t  thread_sleep_ms = default_thread_sleep_ms_ 
)
inline

Constructor.

Parameters
[in,out]output_streamoutput stream
[in]thread_sleep_mshow long the queue processing thread should sleep if there is no data

Definition at line 684 of file logger.h.

◆ Logger() [3/3]

humoto::Logger::Logger ( const std::string &  output_filename,
const std::size_t  thread_sleep_ms = default_thread_sleep_ms_ 
)
inline

Constructor.

Parameters
[in]output_filenamename of the log file
[in]thread_sleep_mshow long the queue processing thread should sleep if there is no data

Definition at line 699 of file logger.h.

◆ ~Logger()

humoto::Logger::~Logger ( )
inline

Destructor.

Definition at line 710 of file logger.h.

Member Function Documentation

◆ initialize() [1/4]

void humoto::LoggerBase::initialize ( std::ostream &  output_stream)
inlineinherited

initialize

Parameters
[in,out]output_streamoutput stream

Definition at line 409 of file logger.h.

◆ initialize() [2/4]

void humoto::LoggerBase::initialize ( const std::string &  output_filename)
inlineinherited

initialize

Parameters
[in]output_filenamename of the log file

Definition at line 423 of file logger.h.

◆ initialize() [3/4]

void humoto::Logger::initialize ( std::ostream &  output_stream,
const std::size_t  thread_sleep_ms = default_thread_sleep_ms_ 
)
inline

initialize

Parameters
[in,out]output_streamoutput stream
[in]thread_sleep_mshow long the queue processing thread should sleep if there is no data

Definition at line 723 of file logger.h.

◆ initialize() [4/4]

void humoto::Logger::initialize ( const std::string &  output_filename,
const std::size_t  thread_sleep_ms = default_thread_sleep_ms_ 
)
inline

initialize

Parameters
[in]output_filenamename of the log file
[in]thread_sleep_mshow long the queue processing thread should sleep if there is no data

Definition at line 742 of file logger.h.

◆ interrupt()

void humoto::Logger::interrupt ( )
inlineprivate

Stop the logging thread if necessary.

Definition at line 643 of file logger.h.

◆ log() [1/6]

template<typename t_Data >
void humoto::Logger::log ( const t_Data &  data,
EIGENTOOLS_EIGENTYPE_DISABLER_TYPE(t_Data) *  dummy = NULL 
)
inline

Log raw data.

Template Parameters
t_Datadata type
Parameters
[in]data
[in]dummydummy parameter for enabling/disabling of the template
Attention
This template relies on Boost to filter out Eigen types, which cannot be treated by it safely – the data might not be copied properly.

Definition at line 768 of file logger.h.

◆ log() [2/6]

template<typename t_Derived >
void humoto::Logger::log ( const Eigen::DenseBase< t_Derived > &  matrix)
inline

Log entry (Eigen matrix)

Template Parameters
t_DerivedEigen template parameter
Parameters
[in]matrixmatrix

Definition at line 782 of file logger.h.

◆ log() [3/6]

void humoto::Logger::log ( const char *  string)
inline

Log raw data (char string)

Parameters
[in]string

Definition at line 806 of file logger.h.

◆ log() [4/6]

template<typename t_Data >
void humoto::Logger::log ( const LogEntryName name,
const t_Data &  data,
EIGENTOOLS_EIGENTYPE_DISABLER_TYPE(t_Data) *  dummy = NULL 
)
inline

Log data (fundamental types)

Template Parameters
t_Datadata type
Parameters
[in]name
[in]data
[in]dummydummy parameter for enabling/disabling of the template
Attention
This template relies on Boost to filter out Eigen types, which cannot be treated by it safely – the data might not be copied properly.

Definition at line 827 of file logger.h.

◆ log() [5/6]

template<typename t_Derived >
void humoto::Logger::log ( const LogEntryName name,
const Eigen::DenseBase< t_Derived > &  matrix 
)
inline

Log entry (Eigen matrix)

Template Parameters
t_DerivedEigen template parameter
Parameters
[in]namename of the log entry
[in]matrixmatrix

Definition at line 843 of file logger.h.

◆ log() [6/6]

void humoto::Logger::log ( const LogEntryName name,
const char *  string 
)
inline

Log data (char string)

Parameters
[in]name
[in]string

Definition at line 869 of file logger.h.

◆ processQueue()

void humoto::Logger::processQueue ( )
inlineprivate

This function is running in a separate thread.

Definition at line 587 of file logger.h.

◆ pushMessage()

void humoto::Logger::pushMessage ( LogMessageBase msg)
inlineprivate

Push message to a queue.

Parameters
[in]msgmessage

Definition at line 576 of file logger.h.

◆ start()

void humoto::Logger::start ( )
inlineprivate

Start the thread.

Definition at line 656 of file logger.h.

Member Data Documentation

◆ default_thread_sleep_ms_

const std::size_t humoto::Logger::default_thread_sleep_ms_ = 2
static

Definition at line 567 of file logger.h.

◆ interrupted_

bool humoto::Logger::interrupted_

Definition at line 565 of file logger.h.

◆ message_queue_

std::queue<LogMessageBase *> humoto::Logger::message_queue_

Definition at line 560 of file logger.h.

◆ message_queue_mutex_

boost::mutex humoto::Logger::message_queue_mutex_

Definition at line 562 of file logger.h.

◆ output_stream_

std::ostream* humoto::LoggerBase::output_stream_
protectedinherited

Output stream.

Definition at line 357 of file logger.h.

◆ processor_thread_

boost::thread humoto::Logger::processor_thread_

Definition at line 563 of file logger.h.

◆ thread_sleep_ms_

std::size_t humoto::Logger::thread_sleep_ms_

Definition at line 558 of file logger.h.


The documentation for this class was generated from the following file: