humoto
logger.h
Go to the documentation of this file.
1 /**
2  @file
3  @author Alexander Sherikov
4  @copyright 2014-2017 INRIA. Licensed under the Apache License, Version 2.0.
5  (see @ref LICENSE or http://www.apache.org/licenses/LICENSE-2.0)
6 
7  @brief
8 */
9 
10 #pragma once
11 
12 namespace humoto
13 {
14  /**
15  * @brief A collection of functions which format data to be parsable by
16  * MATLAB/Octave.
17  */
19  {
20  public:
21  /**
22  * @brief Returns format description for Eigen matrices.
23  *
24  * @return Eigen IO format
25  */
26  static const Eigen::IOFormat& getEigenMatrixFormat()
27  {
28  static const Eigen::IOFormat eigen_output_format(Eigen::FullPrecision, 0, ", ", ";\n", "", "", "[", "];");
29  return (eigen_output_format);
30  }
31 
32 
33 
34 
35  /**
36  * @brief Add a part to the given log entry name.
37  *
38  * @param[in] name name
39  * @param[in] addition subname
40  */
41  static void appendEntryName(std::string & name,
42  const std::string & addition)
43  {
44  if ((name.size() > 0) && (addition.size() > 0))
45  {
46  name += ".";
47  }
48  name += addition;
49  }
50 
51 
52  /**
53  * @brief Add a part to the given log entry name.
54  *
55  * @param[in] name name
56  * @param[in] addition subname
57  */
58  static void appendEntryName(std::string & name,
59  const char * addition)
60  {
61  if ((name.size() > 0) && ('\0' != addition[0]))
62  {
63  name += ".";
64  }
65  name += addition;
66  }
67 
68 
69  /**
70  * @brief Add a part to the given log entry name.
71  *
72  * @param[in] name name
73  * @param[in] index index of a subentry
74  */
75  static void appendEntryName(std::string & name,
76  const std::size_t index)
77  {
78  name += "{";
79  name += boost::lexical_cast<std::string>(index + 1);
80  name += "}";
81  }
82 
83 
84  /**
85  * @brief Format and output a log entry (std::vector)
86  *
87  * @tparam t_Scalar type of vector elements
88  * @param[in,out] out output stream
89  * @param[in] name name of the log entry
90  * @param[in] vector data vector
91  */
92  template <typename t_Scalar>
93  static void formatAndOutputEntry( std::ostream *out,
94  const std::string & name,
95  const std::vector<t_Scalar> &vector)
96  {
97  *out << name << " = " << "[";
98  for (std::size_t i = 0; i < vector.size(); ++i)
99  {
100  *out << vector[i];
101  if (i + 1 != vector.size())
102  {
103  *out << ";";
104  }
105  }
106  *out << "];\n";
107  }
108 
109 
110  /**
111  * @brief Format and output a log entry (Eigen matrix)
112  *
113  * @tparam t_Scalar (Eigen) scalar type
114  * @tparam t_rows (Eigen) number of rows
115  * @tparam t_cols (Eigen) number of columns
116  * @tparam t_flags (Eigen) flags
117  *
118  * @param[in,out] out output stream
119  * @param[in] name name of the log entry
120  * @param[in] matrix matrix
121  */
122  template<typename t_Scalar, int t_rows, int t_cols, int t_flags>
123  static void formatAndOutputEntry( std::ostream *out,
124  const std::string & name,
125  const Eigen::Matrix<t_Scalar, t_rows, t_cols, t_flags> &matrix)
126  {
127  *out << name << " = ...\n" << matrix.format(getEigenMatrixFormat()) << "\n";
128  }
129 
130 
131  /**
132  * @brief Format and output a log entry (std::string)
133  *
134  * @tparam t_Data data type
135  * @param[in,out] out output stream
136  * @param[in] name name of the log entry
137  * @param[in] string string
138  */
139  static void formatAndOutputEntry( std::ostream *out,
140  const std::string & name,
141  const std::string &string)
142  {
143  *out << name << " = '" << string << "';\n";
144  }
145 
146 
147  /**
148  * @brief Format and output a log entry (default)
149  *
150  * @tparam t_Data data type
151  * @param[in,out] out output stream
152  * @param[in] name name of the log entry
153  * @param[in] data data
154  */
155  template <typename t_Data>
156  static void formatAndOutputEntry( std::ostream *out,
157  const std::string & name,
158  const t_Data & data)
159  {
160  *out << name << " = " << data << ";\n";
161  }
162  };
163 
164 
165 
166  /**
167  * @brief Represents log entry name
168  */
170  {
171  private:
172  std::string entry_name_;
173 
174 
175  public:
176  /**
177  * @brief Default constructor
178  */
180  {
181  }
182 
183 
184  /**
185  * @brief Constructor with name initialization
186  *
187  * @param[in] name
188  */
189  LogEntryName (const std::string & name) : entry_name_(name)
190  {
191  }
192 
193 
194  /**
195  * @brief Constructor with name initialization
196  *
197  * @param[in] name
198  */
199  LogEntryName (const char * name) : entry_name_(name)
200  {
201  }
202 
203 
204  /**
205  * @brief Constructor with name initialization
206  *
207  * @param[in] name
208  */
209  LogEntryName (const LogEntryName & name) : entry_name_(name.entry_name_)
210  {
211  }
212 
213 
214  /**
215  * @brief Returns entry name as a string
216  *
217  * @return string
218  */
219  const std::string& getAsString() const
220  {
221  return(entry_name_);
222  }
223 
224 
225  /**
226  * @brief extends entry name with a subname
227  *
228  * @param[in] name subname
229  *
230  * @return this
231  */
232  LogEntryName& add(const char * name)
233  {
234  OctaveFormatter::appendEntryName(entry_name_, name);
235  return (*this);
236  }
237 
238 
239  /**
240  * @brief extends entry name with a subname
241  *
242  * @param[in] name subname
243  *
244  * @return this
245  */
246  LogEntryName& add(const std::string & name)
247  {
248  OctaveFormatter::appendEntryName(entry_name_, name);
249  return (*this);
250  }
251 
252 
253  /**
254  * @brief extends entry name with a subname
255  *
256  * @param[in] index integer index
257  *
258  * @return this
259  */
260  LogEntryName& add(const std::size_t index)
261  {
262  OctaveFormatter::appendEntryName(entry_name_, index);
263  return (*this);
264  }
265 
266 
267  /**
268  * @brief Writes the log entry name to a stream
269  *
270  * @param[in,out] out output stream
271  * @param[in] log_entry_name
272  *
273  * @return output stream
274  */
275  friend std::ostream& operator<< (std::ostream& out, const LogEntryName & log_entry_name)
276  {
277  out << log_entry_name.entry_name_;
278  return(out);
279  }
280  };
281 
282 
283 
284  /**
285  * @brief Logger base class (stream handling)
286  */
288  {
289  private:
290  /// Set if file stream is open
292 
293  /// Output file stream (not used if a stream given to constructor)
294  std::ofstream output_file_stream_;
295 
296 
297  private:
298  /**
299  * @brief Initialize output parameters
300  */
301  void setDefaults()
302  {
303  *output_stream_ << std::setprecision(std::numeric_limits<double>::digits10);
304  }
305 
306 
307  /**
308  * @brief Cleanup before destruction or reinitialization.
309  */
310  void clean()
311  {
312  output_stream_ = NULL;
313 
314  if (file_stream_is_open_)
315  {
316  output_file_stream_.close();
317  }
318  }
319 
320 
321 
322  /**
323  * @brief Stream insertion operator
324  *
325  * @tparam t_Data output data type
326  *
327  * @param[in] data data
328  *
329  * @return Logger
330  */
331  template<typename t_Data>
332  LoggerBase & operator<< (const t_Data & data)
333  {
334  HUMOTO_ASSERT(output_stream_ != NULL, "Logger is not properly initialized.");
335  *output_stream_ << data;
336  return (*this);
337  }
338 
339 
340  /**
341  * @brief Stream manipulation
342  *
343  * @param[in] manipulator_function stream manipulation function
344  *
345  * @return Logger
346  */
347  LoggerBase& operator<< (std::ostream& (*manipulator_function)(std::ostream&))
348  {
349  HUMOTO_ASSERT(output_stream_ != NULL, "Logger is not properly initialized.");
350  manipulator_function(*output_stream_);
351  return (*this);
352  }
353 
354 
355  protected:
356  /// Output stream
357  std::ostream *output_stream_;
358 
359 
360  protected:
361  /**
362  * @brief Default constructor
363  */
365  {
366  file_stream_is_open_ = false;
367  output_stream_ = NULL;
368  }
369 
370 
371  /**
372  * @brief Constructor
373  *
374  * @param[in,out] output_stream output stream
375  */
376  explicit LoggerBase(std::ostream & output_stream)
377  {
378  initialize(output_stream);
379  }
380 
381 
382  /**
383  * @brief Constructor
384  *
385  * @param[in] output_filename name of the log file
386  */
387  explicit LoggerBase(const std::string &output_filename)
388  {
389  initialize(output_filename);
390  }
391 
392 
393  /**
394  * @brief Destructor
395  */
397  {
398  clean();
399  }
400 
401 
402 
403  public:
404  /**
405  * @brief initialize
406  *
407  * @param[in,out] output_stream output stream
408  */
409  void initialize(std::ostream & output_stream)
410  {
411  file_stream_is_open_ = false;
412  output_stream_ = &output_stream;
413 
414  setDefaults();
415  }
416 
417 
418  /**
419  * @brief initialize
420  *
421  * @param[in] output_filename name of the log file
422  */
423  void initialize(const std::string &output_filename)
424  {
425  file_stream_is_open_ = true;
426 
427  output_file_stream_.open(output_filename.c_str());
428  if (output_file_stream_.fail())
429  {
430  HUMOTO_THROW_MSG(std::string("Could not open log file: ") + output_filename);
431  }
432  output_stream_ = &output_file_stream_;
433 
434  setDefaults();
435  }
436  };
437 
438 
439 
440 #ifdef HUMOTO_USE_THREADS_FOR_LOGGING
441  /**
442  * @brief Virtual base class for representation of an enqueued log message.
443  */
445  {
446  public:
447  /**
448  * @brief Destructor should be virtual
449  */
450  virtual ~LogMessageBase() {}
451 
452 
453  /**
454  * @brief Write message to a stream. This method must be
455  * implemented in derived classes.
456  *
457  * @param[in] out output stream
458  */
459  virtual void write(std::ostream * out) = 0;
460  };
461 
462 
463  /**
464  * @brief Standard (with a name) log message
465  *
466  * @tparam t_Data message data type
467  */
468  template <typename t_Data>
470  {
471  public:
473  t_Data data_;
474 
475 
476  public:
477  /**
478  * @brief Construct message
479  *
480  * @param[in] entry_name
481  * @param[in] data
482  */
483  LogMessage( const LogEntryName & entry_name,
484  const t_Data &data) : entry_name_(entry_name), data_(data)
485  {
486  }
487 
488 
489  /**
490  * @brief Default destructor
491  */
493 
494 
495  /// @copydoc humoto::LogMessageBase::write
496  virtual void write(std::ostream * out)
497  {
498  OctaveFormatter::formatAndOutputEntry(out, entry_name_.getAsString(), data_);
499  }
500  };
501 
502 
503  /**
504  * @brief Raw log message (without a name)
505  *
506  * @tparam t_Data message data type
507  */
508  template <typename t_Data>
510  {
511  protected:
512  t_Data data_;
513 
514 
515  public:
516  /**
517  * @brief Construct message
518  *
519  * @param[in] data
520  */
521  explicit LogMessageRaw(const t_Data &data) : data_(data)
522  {
523  }
524 
525 
526  /**
527  * @brief Default destructor
528  */
530 
531 
532  /// @copydoc humoto::LogMessageBase::write
533  virtual void write(std::ostream * out)
534  {
535  *out << data_ << "\n";
536  }
537  };
538 
539 
540 
541  /**
542  * @brief Threaded logger: any data sent to this logger is wrapped in a
543  * message and pushed to a queue, the queue is processed in a separate
544  * thread. Hence, time consuming output does not delay execution of the
545  * main thread(s).
546  *
547  * @attention A high frequency controller may easily flood the logger with
548  * data.
549  *
550  * @attention This logger uses mutexes, so it should be possible to use a
551  * single logger in multiple threads, however, this is not recommended.
552  *
553  * @todo Consider using a nonblocking queue, e.g., <boost/lockfree/queue.hpp>.
554  */
556  {
557  public:
558  std::size_t thread_sleep_ms_;
559 
560  std::queue<LogMessageBase *> message_queue_;
561 
562  boost::mutex message_queue_mutex_;
563  boost::thread processor_thread_;
564 
566 
567  static const std::size_t default_thread_sleep_ms_ = 2;
568 
569 
570  private:
571  /**
572  * @brief Push message to a queue
573  *
574  * @param[in] msg message
575  */
577  {
578  message_queue_mutex_.lock();
579  message_queue_.push(msg);
580  message_queue_mutex_.unlock();
581  }
582 
583 
584  /**
585  * @brief This function is running in a separate thread.
586  */
588  {
589  bool stop_thread = false;
590 
591  for (;;)
592  {
593  std::size_t num_msg = 0;
594 
595 
596  if (interrupted_)
597  {
598  stop_thread = true;
599  }
600 
601  message_queue_mutex_.lock();
602  num_msg = message_queue_.size();
603  message_queue_mutex_.unlock();
604 
605 
606  if (0 == num_msg)
607  {
608  if (false == stop_thread)
609  {
610  boost::this_thread::sleep_for(boost::chrono::milliseconds(thread_sleep_ms_));
611  continue;
612  }
613  }
614  else
615  {
616  for(std::size_t i = 0; i < num_msg; ++i)
617  {
618  LogMessageBase * msg = NULL;
619 
620  message_queue_mutex_.lock();
621  msg = message_queue_.front();
622  message_queue_.pop();
623  message_queue_mutex_.unlock();
624 
625  msg->write(output_stream_);
626  delete msg;
627  }
628 
629  output_stream_->flush();
630  }
631 
632  if (stop_thread)
633  {
634  return;
635  }
636  }
637  }
638 
639 
640  /**
641  * @brief Stop the logging thread if necessary.
642  */
643  void interrupt()
644  {
645  if (! interrupted_)
646  {
647  interrupted_ = true;
648  processor_thread_.join();
649  }
650  }
651 
652 
653  /**
654  * @brief Start the thread.
655  */
656  void start()
657  {
658  interrupted_ = false;
659  processor_thread_ = boost::thread(&humoto::Logger::processQueue, this);
660  }
661 
662 
663  public:
664  /**
665  * @brief Constructor
666  *
667  * @param[in] thread_sleep_ms how long the queue processing
668  * thread should sleep if there is no data
669  */
670  explicit Logger(const std::size_t thread_sleep_ms = default_thread_sleep_ms_)
671  {
672  interrupted_ = true;
673  thread_sleep_ms_ = thread_sleep_ms;
674  }
675 
676 
677  /**
678  * @brief Constructor
679  *
680  * @param[in,out] output_stream output stream
681  * @param[in] thread_sleep_ms how long the queue processing
682  * thread should sleep if there is no data
683  */
684  Logger( std::ostream & output_stream,
685  const std::size_t thread_sleep_ms = default_thread_sleep_ms_) : LoggerBase(output_stream)
686  {
687  thread_sleep_ms_ = thread_sleep_ms;
688  start();
689  }
690 
691 
692  /**
693  * @brief Constructor
694  *
695  * @param[in] output_filename name of the log file
696  * @param[in] thread_sleep_ms how long the queue processing
697  * thread should sleep if there is no data
698  */
699  Logger( const std::string &output_filename,
700  const std::size_t thread_sleep_ms = default_thread_sleep_ms_) : LoggerBase(output_filename)
701  {
702  thread_sleep_ms_ = thread_sleep_ms;
703  start();
704  }
705 
706 
707  /**
708  * @brief Destructor
709  */
711  {
712  interrupt();
713  }
714 
715 
716  /**
717  * @brief initialize
718  *
719  * @param[in,out] output_stream output stream
720  * @param[in] thread_sleep_ms how long the queue processing
721  * thread should sleep if there is no data
722  */
723  void initialize(std::ostream & output_stream,
724  const std::size_t thread_sleep_ms = default_thread_sleep_ms_)
725  {
726  interrupt();
727 
728  LoggerBase::initialize(output_stream);
729  thread_sleep_ms_ = thread_sleep_ms;
730 
731  start();
732  }
733 
734 
735  /**
736  * @brief initialize
737  *
738  * @param[in] output_filename name of the log file
739  * @param[in] thread_sleep_ms how long the queue processing
740  * thread should sleep if there is no data
741  */
742  void initialize(const std::string &output_filename,
743  const std::size_t thread_sleep_ms = default_thread_sleep_ms_)
744  {
745  interrupt();
746 
747  LoggerBase::initialize(output_filename);
748  thread_sleep_ms_ = thread_sleep_ms;
749 
750  start();
751  }
752 
753 
754 
755  /**
756  * @brief Log raw data
757  *
758  * @tparam t_Data data type
759  *
760  * @param[in] data
761  * @param[in] dummy dummy parameter for enabling/disabling of the template
762  *
763  * @attention This template relies on Boost to filter out Eigen
764  * types, which cannot be treated by it safely -- the data might
765  * not be copied properly.
766  */
767  template <typename t_Data>
768  void log( const t_Data &data,
769  EIGENTOOLS_EIGENTYPE_DISABLER_TYPE(t_Data) * dummy = NULL)
770  {
771  pushMessage(new LogMessageRaw<t_Data>(data));
772  }
773 
774 
775  /**
776  * @brief Log entry (Eigen matrix)
777  *
778  * @tparam t_Derived Eigen template parameter
779  * @param[in] matrix matrix
780  */
781  template<typename t_Derived>
782  void log(const Eigen::DenseBase<t_Derived> &matrix)
783  {
784  pushMessage( new LogMessageRaw< Eigen::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))
790  ? Eigen::AutoAlign
791  | (Eigen::DenseBase<t_Derived>::IsRowMajor
792  ? Eigen::RowMajor
793  : Eigen::ColMajor)
795  | (Eigen::DenseBase<t_Derived>::IsRowMajor
796  ? Eigen::RowMajor
797  : Eigen::ColMajor)> >(matrix.eval()) );
798  }
799 
800 
801  /**
802  * @brief Log raw data (char string)
803  *
804  * @param[in] string
805  */
806  void log(const char * string)
807  {
808  pushMessage(new LogMessageRaw<std::string>(string));
809  }
810 
811 
812 
813  /**
814  * @brief Log data (fundamental types)
815  *
816  * @tparam t_Data data type
817  *
818  * @param[in] name
819  * @param[in] data
820  * @param[in] dummy dummy parameter for enabling/disabling of the template
821  *
822  * @attention This template relies on Boost to filter out Eigen
823  * types, which cannot be treated by it safely -- the data might
824  * not be copied properly.
825  */
826  template <typename t_Data>
827  void log( const LogEntryName & name,
828  const t_Data & data,
829  EIGENTOOLS_EIGENTYPE_DISABLER_TYPE(t_Data) * dummy = NULL)
830  {
831  pushMessage(new LogMessage<t_Data>(name, data));
832  }
833 
834 
835  /**
836  * @brief Log entry (Eigen matrix)
837  *
838  * @tparam t_Derived Eigen template parameter
839  * @param[in] name name of the log entry
840  * @param[in] matrix matrix
841  */
842  template<typename t_Derived>
843  void log( const LogEntryName & name,
844  const Eigen::DenseBase<t_Derived> &matrix)
845  {
846  pushMessage( new LogMessage< Eigen::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))
852  ? Eigen::AutoAlign
853  | (Eigen::DenseBase<t_Derived>::IsRowMajor
854  ? Eigen::RowMajor
855  : Eigen::ColMajor)
857  | (Eigen::DenseBase<t_Derived>::IsRowMajor
858  ? Eigen::RowMajor
859  : Eigen::ColMajor)> >(name, matrix.eval()) );
860  }
861 
862 
863  /**
864  * @brief Log data (char string)
865  *
866  * @param[in] name
867  * @param[in] string
868  */
869  void log( const LogEntryName & name,
870  const char *string)
871  {
872  pushMessage(new LogMessage<std::string>(name, string));
873  }
874  };
875 #else
876  /**
877  * @brief Basic logger.
878  *
879  * @attention NOT thread safe!
880  */
881  class HUMOTO_LOCAL Logger : public LoggerBase
882  {
883  public:
884  /**
885  * @brief Default constructor
886  */
887  Logger()
888  {
889  }
890 
891 
892  /**
893  * @brief Constructor
894  *
895  * @param[in,out] output_stream output stream
896  */
897  explicit Logger( std::ostream & output_stream) : LoggerBase(output_stream)
898  {
899  }
900 
901 
902  /**
903  * @brief Constructor
904  *
905  * @param[in] output_filename name of the log file
906  */
907  explicit Logger( const std::string &output_filename) : LoggerBase(output_filename)
908  {
909  }
910 
911 
912  /**
913  * @brief Log raw data
914  *
915  * @tparam t_Data data type
916  *
917  * @param[in] data
918  */
919  template <typename t_Data>
920  void log(const t_Data &data)
921  {
922  *output_stream_ << data << "\n";
923  output_stream_->flush();
924  }
925 
926 
927 
928  /**
929  * @brief Log entry (Eigen matrix)
930  *
931  * @tparam t_Derived Eigen template parameter
932  * @param[in] name name of the log entry
933  * @param[in] matrix matrix
934  */
935  template<typename t_Derived>
936  void log( const LogEntryName & name,
937  const Eigen::DenseBase<t_Derived> &matrix)
938  {
939  OctaveFormatter::formatAndOutputEntry(output_stream_, name.getAsString(), matrix.eval());
940  output_stream_->flush();
941  }
942 
943 
944 
945  /**
946  * @brief Log data (fundamental types)
947  *
948  * @tparam t_Data data type
949  *
950  * @param[in] name
951  * @param[in] data
952  * @param[in] dummy dummy parameter for enabling/disabling of the template
953  *
954  * @attention This template relies on Boost to filter out Eigen
955  * types, which cannot be treated by it safely.
956  */
957  template <typename t_Data>
958  void log( const LogEntryName & name,
959  const t_Data & data,
960  EIGENTOOLS_EIGENTYPE_DISABLER_TYPE(t_Data) * dummy = NULL)
961  {
962  OctaveFormatter::formatAndOutputEntry(output_stream_, name.getAsString(), data);
963  output_stream_->flush();
964  }
965  };
966 #endif // HUMOTO_USE_THREADS_FOR_LOGGING
967 }
968 
969 
970 #ifdef HUMOTO_GLOBAL_LOGGER_ENABLED
971 
972 namespace humoto
973 {
974  /**
975  * @brief Global logger variable.
976  */
977  extern humoto::Logger g_logger;
978 }
979 
980 /// Name of the global logger
981 #define HUMOTO_GLOBAL_LOGGER humoto::g_logger
982 
983 #define HUMOTO_GLOBAL_LOGGER_IF_DEFINED = HUMOTO_GLOBAL_LOGGER
984 
985 /// Initialize logger (must be called in the global scope)
986 #define HUMOTO_INITIALIZE_GLOBAL_LOGGER(output) humoto::Logger HUMOTO_GLOBAL_LOGGER(output)
987 
988 /// Log message
989 #define HUMOTO_LOG_RAW(message) HUMOTO_GLOBAL_LOGGER.log(message)
990 
991 /// Log an data with a name
992 #define HUMOTO_LOG(name, data) HUMOTO_GLOBAL_LOGGER.log(name, data)
993 
994 #else //HUMOTO_GLOBAL_LOGGER_ENABLED
995 
996 
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)
1002 
1003 
1004 #endif //HUMOTO_GLOBAL_LOGGER_ENABLED
Logger(std::ostream &output_stream, const std::size_t thread_sleep_ms=default_thread_sleep_ms_)
Constructor.
Definition: logger.h:684
~LoggerBase()
Destructor.
Definition: logger.h:396
Virtual base class for representation of an enqueued log message.
Definition: logger.h:444
void log(const LogEntryName &name, const char *string)
Log data (char string)
Definition: logger.h:869
std::queue< LogMessageBase * > message_queue_
Definition: logger.h:560
virtual void write(std::ostream *out)
Write message to a stream. This method must be implemented in derived classes.
Definition: logger.h:533
void setDefaults()
Initialize output parameters.
Definition: logger.h:301
void log(const Eigen::DenseBase< t_Derived > &matrix)
Log entry (Eigen matrix)
Definition: logger.h:782
bool file_stream_is_open_
Set if file stream is open.
Definition: logger.h:291
#define HUMOTO_LOCAL
Definition: export_import.h:26
LogEntryName & add(const std::size_t index)
extends entry name with a subname
Definition: logger.h:260
virtual void write(std::ostream *out)
Write message to a stream. This method must be implemented in derived classes.
Definition: logger.h:496
~LogMessage()
Default destructor.
Definition: logger.h:492
static void appendEntryName(std::string &name, const std::size_t index)
Add a part to the given log entry name.
Definition: logger.h:75
static void formatAndOutputEntry(std::ostream *out, const std::string &name, const Eigen::Matrix< t_Scalar, t_rows, t_cols, t_flags > &matrix)
Format and output a log entry (Eigen matrix)
Definition: logger.h:123
static void formatAndOutputEntry(std::ostream *out, const std::string &name, const t_Data &data)
Format and output a log entry (default)
Definition: logger.h:156
LogEntryName(const char *name)
Constructor with name initialization.
Definition: logger.h:199
static void appendEntryName(std::string &name, const char *addition)
Add a part to the given log entry name.
Definition: logger.h:58
void log(const LogEntryName &name, const Eigen::DenseBase< t_Derived > &matrix)
Log entry (Eigen matrix)
Definition: logger.h:843
#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.
static const Eigen::IOFormat & getEigenMatrixFormat()
Returns format description for Eigen matrices.
Definition: logger.h:26
void processQueue()
This function is running in a separate thread.
Definition: logger.h:587
A collection of functions which format data to be parsable by MATLAB/Octave.
Definition: logger.h:18
#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.
Definition: logger.h:169
LoggerBase(const std::string &output_filename)
Constructor.
Definition: logger.h:387
void initialize(std::ostream &output_stream)
initialize
Definition: logger.h:409
static void formatAndOutputEntry(std::ostream *out, const std::string &name, const std::vector< t_Scalar > &vector)
Format and output a log entry (std::vector)
Definition: logger.h:93
LogMessageRaw(const t_Data &data)
Construct message.
Definition: logger.h:521
const std::string & getAsString() const
Returns entry name as a string.
Definition: logger.h:219
void start()
Start the thread.
Definition: logger.h:656
void initialize(std::ostream &output_stream, const std::size_t thread_sleep_ms=default_thread_sleep_ms_)
initialize
Definition: logger.h:723
void interrupt()
Stop the logging thread if necessary.
Definition: logger.h:643
void initialize(const std::string &output_filename)
initialize
Definition: logger.h:423
Logger(const std::string &output_filename, const std::size_t thread_sleep_ms=default_thread_sleep_ms_)
Constructor.
Definition: logger.h:699
void pushMessage(LogMessageBase *msg)
Push message to a queue.
Definition: logger.h:576
static void appendEntryName(std::string &name, const std::string &addition)
Add a part to the given log entry name.
Definition: logger.h:41
std::ofstream output_file_stream_
Output file stream (not used if a stream given to constructor)
Definition: logger.h:294
Threaded logger: any data sent to this logger is wrapped in a message and pushed to a queue...
Definition: logger.h:555
void log(const t_Data &data, EIGENTOOLS_EIGENTYPE_DISABLER_TYPE(t_Data) *dummy=NULL)
Log raw data.
Definition: logger.h:768
~Logger()
Destructor.
Definition: logger.h:710
LoggerBase(std::ostream &output_stream)
Constructor.
Definition: logger.h:376
void clean()
Cleanup before destruction or reinitialization.
Definition: logger.h:310
void log(const char *string)
Log raw data (char string)
Definition: logger.h:806
static void formatAndOutputEntry(std::ostream *out, const std::string &name, const std::string &string)
Format and output a log entry (std::string)
Definition: logger.h:139
Raw log message (without a name)
Definition: logger.h:509
Standard (with a name) log message.
Definition: logger.h:469
LoggerBase()
Default constructor.
Definition: logger.h:364
virtual ~LogMessageBase()
Destructor should be virtual.
Definition: logger.h:450
The root namespace of HuMoTo.
Definition: config.h:12
LogEntryName(const LogEntryName &name)
Constructor with name initialization.
Definition: logger.h:209
std::string entry_name_
Definition: logger.h:172
LogMessage(const LogEntryName &entry_name, const t_Data &data)
Construct message.
Definition: logger.h:483
std::size_t thread_sleep_ms_
Definition: logger.h:558
Logger base class (stream handling)
Definition: logger.h:287
void log(const LogEntryName &name, const t_Data &data, EIGENTOOLS_EIGENTYPE_DISABLER_TYPE(t_Data) *dummy=NULL)
Log data (fundamental types)
Definition: logger.h:827
void initialize(const std::string &output_filename, const std::size_t thread_sleep_ms=default_thread_sleep_ms_)
initialize
Definition: logger.h:742
boost::thread processor_thread_
Definition: logger.h:563
boost::mutex message_queue_mutex_
Definition: logger.h:562
LogEntryName & add(const char *name)
extends entry name with a subname
Definition: logger.h:232
std::ostream * output_stream_
Output stream.
Definition: logger.h:357
#define EIGENTOOLS_CONSTANT_SIZE_ALIGN_TYPE
Definition: eigentools.h:55
Logger(const std::size_t thread_sleep_ms=default_thread_sleep_ms_)
Constructor.
Definition: logger.h:670
LogEntryName(const std::string &name)
Constructor with name initialization.
Definition: logger.h:189
LogEntryName & add(const std::string &name)
extends entry name with a subname
Definition: logger.h:246
LogEntryName()
Default constructor.
Definition: logger.h:179
LogEntryName entry_name_
Definition: logger.h:472
bool interrupted_
Definition: logger.h:565
~LogMessageRaw()
Default destructor.
Definition: logger.h:529