28 static const Eigen::IOFormat eigen_output_format(Eigen::FullPrecision, 0,
", ",
";\n",
"",
"",
"[",
"];");
29 return (eigen_output_format);
42 const std::string & addition)
44 if ((name.size() > 0) && (addition.size() > 0))
59 const char * addition)
61 if ((name.size() > 0) && (
'\0' != addition[0]))
76 const std::size_t index)
79 name += boost::lexical_cast<std::string>(index + 1);
92 template <
typename t_Scalar>
94 const std::string & name,
95 const std::vector<t_Scalar> &vector)
97 *out << name <<
" = " <<
"[";
98 for (std::size_t i = 0; i < vector.size(); ++i)
101 if (i + 1 != vector.size())
122 template<
typename t_Scalar,
int t_rows,
int t_cols,
int t_flags>
124 const std::string & name,
125 const Eigen::Matrix<t_Scalar, t_rows, t_cols, t_flags> &matrix)
127 *out << name <<
" = ...\n" << matrix.format(getEigenMatrixFormat()) <<
"\n";
140 const std::string & name,
141 const std::string &
string)
143 *out << name <<
" = '" <<
string <<
"';\n";
155 template <
typename t_Data>
157 const std::string & name,
160 *out << name <<
" = " << data <<
";\n";
275 friend std::ostream& operator<< (std::ostream& out,
const LogEntryName & log_entry_name)
303 *output_stream_ << std::setprecision(std::numeric_limits<double>::digits10);
312 output_stream_ = NULL;
314 if (file_stream_is_open_)
316 output_file_stream_.close();
331 template<
typename t_Data>
334 HUMOTO_ASSERT(output_stream_ != NULL,
"Logger is not properly initialized.");
335 *output_stream_ << data;
347 LoggerBase& operator<< (std::ostream& (*manipulator_function)(std::ostream&))
349 HUMOTO_ASSERT(output_stream_ != NULL,
"Logger is not properly initialized.");
350 manipulator_function(*output_stream_);
366 file_stream_is_open_ =
false;
367 output_stream_ = NULL;
378 initialize(output_stream);
389 initialize(output_filename);
411 file_stream_is_open_ =
false;
412 output_stream_ = &output_stream;
425 file_stream_is_open_ =
true;
427 output_file_stream_.open(output_filename.c_str());
428 if (output_file_stream_.fail())
430 HUMOTO_THROW_MSG(std::string(
"Could not open log file: ") + output_filename);
432 output_stream_ = &output_file_stream_;
440 #ifdef HUMOTO_USE_THREADS_FOR_LOGGING 459 virtual void write(std::ostream * out) = 0;
468 template <
typename t_Data>
484 const t_Data &data) : entry_name_(entry_name), data_(data)
496 virtual void write(std::ostream * out)
508 template <
typename t_Data>
533 virtual void write(std::ostream * out)
535 *out << data_ <<
"\n";
567 static const std::size_t default_thread_sleep_ms_ = 2;
578 message_queue_mutex_.lock();
579 message_queue_.push(msg);
580 message_queue_mutex_.unlock();
589 bool stop_thread =
false;
593 std::size_t num_msg = 0;
601 message_queue_mutex_.lock();
602 num_msg = message_queue_.size();
603 message_queue_mutex_.unlock();
608 if (
false == stop_thread)
610 boost::this_thread::sleep_for(boost::chrono::milliseconds(thread_sleep_ms_));
616 for(std::size_t i = 0; i < num_msg; ++i)
620 message_queue_mutex_.lock();
621 msg = message_queue_.front();
622 message_queue_.pop();
623 message_queue_mutex_.unlock();
625 msg->
write(output_stream_);
629 output_stream_->flush();
648 processor_thread_.join();
658 interrupted_ =
false;
670 explicit Logger(
const std::size_t thread_sleep_ms = default_thread_sleep_ms_)
673 thread_sleep_ms_ = thread_sleep_ms;
685 const std::size_t thread_sleep_ms = default_thread_sleep_ms_) :
LoggerBase(output_stream)
687 thread_sleep_ms_ = thread_sleep_ms;
699 Logger(
const std::string &output_filename,
700 const std::size_t thread_sleep_ms = default_thread_sleep_ms_) :
LoggerBase(output_filename)
702 thread_sleep_ms_ = thread_sleep_ms;
724 const std::size_t thread_sleep_ms = default_thread_sleep_ms_)
729 thread_sleep_ms_ = thread_sleep_ms;
743 const std::size_t thread_sleep_ms = default_thread_sleep_ms_)
748 thread_sleep_ms_ = thread_sleep_ms;
767 template <
typename t_Data>
768 void log(
const t_Data &data,
769 EIGENTOOLS_EIGENTYPE_DISABLER_TYPE(t_Data) * dummy = NULL)
781 template<
typename t_Derived>
782 void log(
const Eigen::DenseBase<t_Derived> &matrix)
785 typename Eigen::DenseBase<t_Derived>::Scalar,
786 Eigen::DenseBase<t_Derived>::RowsAtCompileTime,
787 Eigen::DenseBase<t_Derived>::ColsAtCompileTime,
788 ((Eigen::DenseBase<t_Derived>::RowsAtCompileTime == Eigen::Dynamic)
789 || (Eigen::DenseBase<t_Derived>::ColsAtCompileTime == Eigen::Dynamic))
791 | (Eigen::DenseBase<t_Derived>::IsRowMajor
795 | (Eigen::DenseBase<t_Derived>::IsRowMajor
797 : Eigen::ColMajor)> >(matrix.eval()) );
806 void log(
const char *
string)
826 template <
typename t_Data>
829 EIGENTOOLS_EIGENTYPE_DISABLER_TYPE(t_Data) * dummy = NULL)
842 template<
typename t_Derived>
844 const Eigen::DenseBase<t_Derived> &matrix)
847 typename Eigen::DenseBase<t_Derived>::Scalar,
848 Eigen::DenseBase<t_Derived>::RowsAtCompileTime,
849 Eigen::DenseBase<t_Derived>::ColsAtCompileTime,
850 ((Eigen::DenseBase<t_Derived>::RowsAtCompileTime == Eigen::Dynamic)
851 || (Eigen::DenseBase<t_Derived>::ColsAtCompileTime == Eigen::Dynamic))
853 | (Eigen::DenseBase<t_Derived>::IsRowMajor
857 | (Eigen::DenseBase<t_Derived>::IsRowMajor
859 : Eigen::ColMajor)> >(name, matrix.eval()) );
897 explicit Logger( std::ostream & output_stream) :
LoggerBase(output_stream)
907 explicit Logger(
const std::string &output_filename) :
LoggerBase(output_filename)
919 template <
typename t_Data>
920 void log(
const t_Data &data)
922 *output_stream_ << data <<
"\n";
923 output_stream_->flush();
935 template<
typename t_Derived>
937 const Eigen::DenseBase<t_Derived> &matrix)
940 output_stream_->flush();
957 template <
typename t_Data>
960 EIGENTOOLS_EIGENTYPE_DISABLER_TYPE(t_Data) * dummy = NULL)
963 output_stream_->flush();
966 #endif // HUMOTO_USE_THREADS_FOR_LOGGING 970 #ifdef HUMOTO_GLOBAL_LOGGER_ENABLED 981 #define HUMOTO_GLOBAL_LOGGER humoto::g_logger 983 #define HUMOTO_GLOBAL_LOGGER_IF_DEFINED = HUMOTO_GLOBAL_LOGGER 986 #define HUMOTO_INITIALIZE_GLOBAL_LOGGER(output) humoto::Logger HUMOTO_GLOBAL_LOGGER(output) 989 #define HUMOTO_LOG_RAW(message) HUMOTO_GLOBAL_LOGGER.log(message) 992 #define HUMOTO_LOG(name, data) HUMOTO_GLOBAL_LOGGER.log(name, data) 994 #else //HUMOTO_GLOBAL_LOGGER_ENABLED 997 #define HUMOTO_GLOBAL_LOGGER_IF_DEFINED 998 #define HUMOTO_INITIALIZE_GLOBAL_LOGGER(output) 999 #define HUMOTO_INITIALIZE_LOGGER(stream) 1000 #define HUMOTO_LOG_RAW(message) 1001 #define HUMOTO_LOG(name, data) 1004 #endif //HUMOTO_GLOBAL_LOGGER_ENABLED Logger(std::ostream &output_stream, const std::size_t thread_sleep_ms=default_thread_sleep_ms_)
Constructor.
Virtual base class for representation of an enqueued log message.
void log(const LogEntryName &name, const char *string)
Log data (char string)
std::queue< LogMessageBase * > message_queue_
virtual void write(std::ostream *out)
Write message to a stream. This method must be implemented in derived classes.
void setDefaults()
Initialize output parameters.
void log(const Eigen::DenseBase< t_Derived > &matrix)
Log entry (Eigen matrix)
bool file_stream_is_open_
Set if file stream is open.
LogEntryName & add(const std::size_t index)
extends entry name with a subname
virtual void write(std::ostream *out)
Write message to a stream. This method must be implemented in derived classes.
~LogMessage()
Default destructor.
LogEntryName(const char *name)
Constructor with name initialization.
void log(const LogEntryName &name, const Eigen::DenseBase< t_Derived > &matrix)
Log entry (Eigen matrix)
#define HUMOTO_ASSERT(condition, message)
virtual void write(std::ostream *out)=0
Write message to a stream. This method must be implemented in derived classes.
void processQueue()
This function is running in a separate thread.
#define HUMOTO_THROW_MSG(s)
HUMOTO_THROW_MSG throws an error message concatenated with the name of the function (if supported)...
Represents log entry name.
LoggerBase(const std::string &output_filename)
Constructor.
void initialize(std::ostream &output_stream)
initialize
LogMessageRaw(const t_Data &data)
Construct message.
const std::string & getAsString() const
Returns entry name as a string.
void start()
Start the thread.
void initialize(std::ostream &output_stream, const std::size_t thread_sleep_ms=default_thread_sleep_ms_)
initialize
void interrupt()
Stop the logging thread if necessary.
void initialize(const std::string &output_filename)
initialize
Logger(const std::string &output_filename, const std::size_t thread_sleep_ms=default_thread_sleep_ms_)
Constructor.
void pushMessage(LogMessageBase *msg)
Push message to a queue.
std::ofstream output_file_stream_
Output file stream (not used if a stream given to constructor)
Threaded logger: any data sent to this logger is wrapped in a message and pushed to a queue...
void log(const t_Data &data, EIGENTOOLS_EIGENTYPE_DISABLER_TYPE(t_Data) *dummy=NULL)
Log raw data.
LoggerBase(std::ostream &output_stream)
Constructor.
void clean()
Cleanup before destruction or reinitialization.
void log(const char *string)
Log raw data (char string)
Raw log message (without a name)
Standard (with a name) log message.
LoggerBase()
Default constructor.
virtual ~LogMessageBase()
Destructor should be virtual.
The root namespace of HuMoTo.
LogEntryName(const LogEntryName &name)
Constructor with name initialization.
LogMessage(const LogEntryName &entry_name, const t_Data &data)
Construct message.
std::size_t thread_sleep_ms_
Logger base class (stream handling)
void log(const LogEntryName &name, const t_Data &data, EIGENTOOLS_EIGENTYPE_DISABLER_TYPE(t_Data) *dummy=NULL)
Log data (fundamental types)
void initialize(const std::string &output_filename, const std::size_t thread_sleep_ms=default_thread_sleep_ms_)
initialize
boost::thread processor_thread_
boost::mutex message_queue_mutex_
LogEntryName & add(const char *name)
extends entry name with a subname
std::ostream * output_stream_
Output stream.
Logger(const std::size_t thread_sleep_ms=default_thread_sleep_ms_)
Constructor.
LogEntryName(const std::string &name)
Constructor with name initialization.
LogEntryName & add(const std::string &name)
extends entry name with a subname
LogEntryName()
Default constructor.
~LogMessageRaw()
Default destructor.