blob: a91564353c1d6a0fd8eef7da4a54866d7212f543 [file] [log] [blame]
Brian Silvermanfd5e2a32014-02-22 20:02:39 -08001#ifndef AOS_COMMON_LOGGING_MATRIX_LOGGING_H_
2#define AOS_COMMON_LOGGING_MATRIX_LOGGING_H_
3
4#include <string>
Brian Silvermancb5da1f2015-12-05 22:19:58 -05005#include <functional>
Brian Silvermanfd5e2a32014-02-22 20:02:39 -08006
7#include "Eigen/Dense"
8
Brian Silvermancb5da1f2015-12-05 22:19:58 -05009#include "aos/common/logging/interface.h"
Brian Silvermanfd5e2a32014-02-22 20:02:39 -080010#include "aos/common/die.h"
Brian Silvermancb5da1f2015-12-05 22:19:58 -050011#include "aos/common/queue_primitives.h"
Brian Silvermanfd5e2a32014-02-22 20:02:39 -080012
13namespace aos {
14namespace logging {
15
16// Logs the contents of a matrix and a constant string.
17// matrix must be an instance of an Eigen matrix (or something similar).
Brian Silvermancda86662016-01-08 01:00:41 -080018#define LOG_MATRIX(level, message, matrix) \
19 do { \
20 static const ::std::string kAosLoggingMessage( \
21 LOG_SOURCENAME ": " STRINGIFY(__LINE__) ": " message); \
22 ::aos::logging::DoLogMatrixTemplated(level, kAosLoggingMessage, \
23 (matrix).eval()); \
24 /* so that GCC knows that it won't return */ \
25 if (level == FATAL) { \
26 ::aos::Die("DoLogStruct(FATAL) fell through!!!!!\n"); \
27 } \
Brian Silvermanfd5e2a32014-02-22 20:02:39 -080028 } while (false)
29
30template <class T>
Brian Silvermancb5da1f2015-12-05 22:19:58 -050031void DoLogMatrixTemplated(log_level level, const ::std::string &message,
32 const T &matrix) {
Brian Silvermancda86662016-01-08 01:00:41 -080033 if (T::IsRowMajor) {
34 typename T::Scalar data[matrix.rows() * matrix.cols()];
35 ::Eigen::Map<T>(data, matrix.rows(), matrix.cols()) = matrix;
36 internal::DoLogMatrix(level, message, TypeID<typename T::Scalar>::id,
37 matrix.rows(), matrix.cols(), data, 1);
38 } else {
39 internal::DoLogMatrix(level, message, TypeID<typename T::Scalar>::id,
40 matrix.rows(), matrix.cols(), matrix.data(), 1);
41 }
Brian Silvermancb5da1f2015-12-05 22:19:58 -050042}
Brian Silvermanfd5e2a32014-02-22 20:02:39 -080043
44} // namespace logging
45} // namespace aos
46
Brian Silvermanfd5e2a32014-02-22 20:02:39 -080047#endif // AOS_COMMON_LOGGING_MATRIX_LOGGING_H_