LogManager.h
Go to the documentation of this file.
1 #ifndef __LOG_MANAGER_H__
2 #define __LOG_MANAGER_H__
3 
4 #include <iostream>
5 #include <sys/time.h>
6 #include <deque>
7 #include <boost/thread/thread.hpp>
8 #include "LogManagerBase.h"
9 
10 template<class T>
11 class LogManager : public LogManagerBase
12 {
13 public:
14  LogManager() : m_index(-1), m_isNewStateAdded(false), m_atLast(true),
15  m_maxLogLength(0){
16  }
17  void add(const T& state){
18  boost::mutex::scoped_lock lock(m_mutex);
19  m_log.push_back(state);
20  if (m_log.size() == 1) m_offsetT = state.time;
21  if (m_maxLogLength > 0 && m_log.size() > m_maxLogLength) {
22  m_log.pop_front();
23  if (m_index >= 1) m_index--;
24  }
25  m_isNewStateAdded = true;
26  }
27  void clear(){
28  boost::mutex::scoped_lock lock(m_mutex);
29  m_isPlaying = false;
30  m_log.clear();
31  m_index = -1;
32  m_atLast = true;
33  }
34  void prev(int delta=1){
35  boost::mutex::scoped_lock lock(m_mutex);
36  setIndex(m_index - delta);
37  }
38  void next(int delta=1){
39  boost::mutex::scoped_lock lock(m_mutex);
40  setIndex(m_index + delta);
41  }
42  void head(){
43  boost::mutex::scoped_lock lock(m_mutex);
44  setIndex(0);
45  }
46  void tail(){
47  boost::mutex::scoped_lock lock(m_mutex);
48  if (!m_log.empty()) setIndex(m_log.size()-1);
49  }
50  void move(double ratio){
51  boost::mutex::scoped_lock lock(m_mutex);
52  if (m_log.size()) setIndex(ratio*(m_log.size()-1));
53  }
55  double currentTime() {
56  boost::mutex::scoped_lock lock(m_mutex);
57  if (!m_log.empty() && m_index>=0){
58  return m_log[m_index].time - m_offsetT;
59  }else{
60  return -1;
61  }
62  }
63  int index() { return m_index; }
64  double time(int i) {
65  boost::mutex::scoped_lock lock(m_mutex);
66  return m_log[i].time;
67  }
68  void faster(){
69  boost::mutex::scoped_lock lock(m_mutex);
70  m_playRatio *= 2;
71  if (m_isPlaying){
72  m_initT = m_log[m_index].time;
73  gettimeofday(&m_startT, NULL);
74  }
75  }
76  void slower(){
77  boost::mutex::scoped_lock lock(m_mutex);
78  m_playRatio /= 2;
79  if (m_isPlaying){
80  m_initT = m_log[m_index].time;
81  gettimeofday(&m_startT, NULL);
82  }
83  }
84  bool record(double i_fps){
85  boost::mutex::scoped_lock lock(m_mutex);
86  if (m_log.empty()) return false;
87 
88  if (m_atLast) setIndex(0);
89  m_initT = m_log[0].time;
90  m_isRecording = true;
91  m_fps = i_fps;
92  return true;
93  }
94  bool isRecording() { return m_isRecording; }
95  void play(){
96  boost::mutex::scoped_lock lock(m_mutex);
97  if (m_log.empty()) return;
98 
99  if (!m_isPlaying){
100  m_isPlaying = true;
101  if (m_atLast) setIndex(0);
102  m_initT = m_log[m_index].time;
103  gettimeofday(&m_startT, NULL);
104  }else{
105  m_isPlaying = false;
106  }
107  }
108  int updateIndex(){
109  boost::mutex::scoped_lock lock(m_mutex);
110  if (m_isPlaying){
111  // compute time to draw
112  struct timeval tv;
113  gettimeofday(&tv, NULL);
114  double drawT = m_initT + ((tv.tv_sec - m_startT.tv_sec) + (tv.tv_usec - m_startT.tv_usec)*1e-6)*m_playRatio;
115  //
116  while(drawT > m_log[m_index].time){
117  setIndex(m_index+1);
118  if (m_atLast) {
119  m_isPlaying = false;
120  break;
121  }
122  }
123  } else if (m_isNewStateAdded && m_atLast){
124  // draw newest state
125  setIndex(m_log.size() - 1);
126  m_isNewStateAdded = false;
127  }
128  if(m_isRecording){
129  while(m_initT > m_log[m_index].time){
130  setIndex(m_index+1);
131  if (m_atLast) {
132  m_isRecording = false;
133  break;
134  }
135  }
136  m_initT += 1.0/m_fps*m_playRatio;
137  }
138  return m_index;
139  }
140  T& state() {
141  boost::mutex::scoped_lock lock(m_mutex);
142  if (m_index < 0 || m_index >= m_log.size()){
143  std::cerr << "invalid index:" << m_index << "," << m_log.size()
144  << std::endl;
145  }
146  return m_log[m_index];
147  }
148  bool state(T& o_state){
149  boost::mutex::scoped_lock lock(m_mutex);
150  if (m_index < 0 || m_index >= m_log.size()){
151  return false;
152  }else{
153  o_state = m_log[m_index];
154  return true;
155  }
156  }
157  void enableRingBuffer(int len) { m_maxLogLength = len; }
158  unsigned int length() {
159  boost::mutex::scoped_lock lock(m_mutex);
160  return m_log.size();
161  }
162  double time() {
163  boost::mutex::scoped_lock lock(m_mutex);
164  if (m_log.size() < m_index && m_index >= 0){
165  return -1;
166  }else{
167  return m_log[m_index].time;
168  }
169  }
170 protected:
171  void setIndex(int i){
172  if (m_log.empty()) return;
173 
174  m_index = i;
175  if (m_index < 0) m_index = 0;
176  if (m_index >= m_log.size()) m_index = m_log.size()-1;
177  m_atLast = m_index == m_log.size()-1;
178  }
179 
180  std::deque<T> m_log;
181  int m_index;
183  double m_initT;
184  struct timeval m_startT;
186  double m_offsetT;
187  boost::mutex m_mutex;
188 };
189 #endif
void enableRingBuffer(int len)
Definition: LogManager.h:157
double time(int i)
Definition: LogManager.h:64
bool m_atLast
Definition: LogManager.h:182
void slower()
Definition: LogManager.h:76
double m_initT
Definition: LogManager.h:183
void setIndex(int i)
Definition: LogManager.h:171
void move(double ratio)
Definition: LogManager.h:50
void faster()
Definition: LogManager.h:68
int gettimeofday(struct timeval *tv, struct timezone *tz)
void head()
Definition: LogManager.h:42
struct timeval m_startT
Definition: LogManager.h:184
int updateIndex()
Definition: LogManager.h:108
void add(const T &state)
Definition: LogManager.h:17
bool record(double i_fps)
Definition: LogManager.h:84
unsigned int length()
Definition: LogManager.h:158
boost::mutex m_mutex
Definition: LogManager.h:187
double m_offsetT
Definition: LogManager.h:186
int index()
Definition: LogManager.h:63
T & state()
Definition: LogManager.h:140
void next(int delta=1)
Definition: LogManager.h:38
std::deque< T > m_log
Definition: LogManager.h:180
double currentTime()
Definition: LogManager.h:55
void prev(int delta=1)
Definition: LogManager.h:34
void play()
Definition: LogManager.h:95
void clear()
Definition: LogManager.h:27
bool isRecording()
Definition: LogManager.h:94
int m_maxLogLength
Definition: LogManager.h:185
double time()
Definition: LogManager.h:162
bool state(T &o_state)
Definition: LogManager.h:148
bool m_isNewStateAdded
Definition: LogManager.h:182
bool isNewStateAdded()
Definition: LogManager.h:54
void tail()
Definition: LogManager.h:46


hrpsys
Author(s): AIST, Fumio Kanehiro
autogenerated on Thu May 6 2021 02:41:50