Logger.h
Go to the documentation of this file.
1 /* Copyright 2020 CNRS-AIST JRL */
2 
3 #pragma once
4 
5 #include <iosfwd>
6 #include <string>
7 
8 #include <jrl-qp/api.h>
9 #include <jrl-qp/defs.h>
10 #include <jrl-qp/internal/meta.h>
11 #include <jrl-qp/utils/toMatlab.h>
12 
13 namespace jrl::qp::utils
14 {
19 {
20 public:
25  Logger(std::ostream & os, std::string name, std::uint32_t flags = 0) : flags_(flags), iter_(-1), name_(name), os_(&os)
26  {
27  }
28 
33  Logger & setFlag(std::uint32_t flag, bool add = true);
34 
36  Logger & setOutputStream(std::ostream & os);
37 
39  template<typename... Args>
40  void comment(std::uint32_t flag, Args &&... args) const;
42  void startIter(int i);
48  template<typename... Args>
49  void log(std::uint32_t flag, Args &&... args) const;
50 
54  Logger subLog(const std::string & name) const;
55 
57  int iter() const
58  {
59  return iter_;
60  }
61 
62 private:
67  template<bool b>
68  std::ostream & logIter() const;
69 
70  template<bool b, typename T, typename... Args>
71  void log_(const std::string & valName, const T & val, Args &&... args) const;
72 
73  template<bool b, typename T>
74  void log_(const T &) const;
75 
76  template<bool>
77  void log_() const
78  {
79  }
80 
81  template<bool b, typename Derived>
82  void logVal_(const std::string & valName, const Eigen::EigenBase<Derived> & M) const;
83 
84  template<bool b, typename T>
85  void logVal_(const std::string & valName, const std::vector<T> & v) const;
86 
87  template<bool b,
88  typename Other,
89  typename std::enable_if<(!internal::derives_from<Other, Eigen::EigenBase>()), int>::type = 0>
90  void logVal_(const std::string & valName, const Other & val) const;
91 
92  std::uint32_t flags_;
93  int iter_;
94  std::string name_;
95  std::ostream * os_;
96 };
97 
98 template<>
99 inline std::ostream & Logger::logIter<true>() const
100 {
101  *os_ << name_ << "(" << iter_ + 1 << ")";
102  return *os_;
103 }
104 
105 template<>
106 inline std::ostream & Logger::logIter<false>() const
107 {
108  *os_ << name_ << "Data";
109  return *os_;
110 }
111 
112 template<typename... Args>
113 inline void Logger::comment(std::uint32_t flag, Args &&... args) const
114 {
115  if(flag & flags_) ((*os_ << "% ") << ... << args) << "\n";
116 }
117 
118 template<typename... Args>
119 inline void Logger::log(std::uint32_t flag, Args &&... args) const
120 {
121  if(flag & flags_)
122  {
123  if(flag & constant::noIterationFlag)
124  log_<false>(std::forward<Args>(args)...);
125  else
126  log_<true>(std::forward<Args>(args)...);
127  }
128 }
129 
130 template<bool b, typename T, typename... Args>
131 inline void Logger::log_(const std::string & valName, const T & val, Args &&... args) const
132 {
133  logVal_<b>(valName, val);
134  log_<b>(std::forward<Args>(args)...);
135 }
136 
137 template<bool, typename T>
138 inline void Logger::log_(const T &) const
139 {
140  static_assert(internal::always_false<T>::value && "incorrect number of arguments.");
141 }
142 
143 template<bool b, typename Derived>
144 inline void Logger::logVal_(const std::string & valName, const Eigen::EigenBase<Derived> & M) const
145 {
146  logIter<b>() << "." << valName << " = " << (toMatlab)M.const_derived() << ";\n";
147 }
148 
149 template<bool b, typename T>
150 inline void Logger::logVal_(const std::string & valName, const std::vector<T> & v) const
151 {
152  if(v.empty()) return;
153  auto & line = logIter<b>() << "." << valName << " = [";
154  for(size_t i = 0; i < v.size() - 1; ++i)
155  {
156  line << internal::cast_as_underlying_if_enum<T>{}(v[i]) << ", ";
157  }
158  line << internal::cast_as_underlying_if_enum<T>{}(v.back()) << "];\n";
159 }
160 
161 template<bool b, typename Other, typename std::enable_if<(!internal::derives_from<Other, Eigen::EigenBase>()), int>::type>
162 inline void Logger::logVal_(const std::string & valName, const Other & val) const
163 {
164  logIter<b>() << "." << valName << " = " << val << ";\n";
165 }
166 } // namespace jrl::qp::utils
jrl::qp::utils
Definition: Logger.h:13
jrl::qp::utils::Logger
Definition: Logger.h:18
jrl::qp::constant::noIterationFlag
constexpr std::uint32_t noIterationFlag
Definition: defs.h:22
jrl::qp::utils::Logger::log
void log(std::uint32_t flag, Args &&... args) const
Definition: Logger.h:119
jrl::qp::utils::Logger::iter
int iter() const
Definition: Logger.h:57
jrl::qp::utils::Logger::comment
void comment(std::uint32_t flag, Args &&... args) const
Definition: Logger.h:113
meta.h
jrl::qp::internal::always_false
Definition: meta.h:69
JRLQP_DLLAPI
#define JRLQP_DLLAPI
Definition: api.h:35
jrl::qp::utils::Logger::Logger
Logger(std::ostream &os, std::string name, std::uint32_t flags=0)
Definition: Logger.h:25
defs.h
toMatlab.h
api.h