real-time-logger.cpp 3.3 KB
Newer Older
Joseph Mirabel's avatar
Joseph Mirabel committed
1
2
3
4
5
6
7
8
9
10
/*
 * Copyright 2018,
 * Joseph Mirabel
 *
 * LAAS-CNRS
 *
 */

#include <dynamic-graph/real-time-logger.h>

11
12
13
#include <boost/thread/thread.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

Joseph Mirabel's avatar
Joseph Mirabel committed
14
15
16
17
namespace dynamicgraph
{
  RealTimeLogger::RealTimeLogger (const std::size_t& bufferSize)
    : buffer_(bufferSize, NULL)
Joseph Mirabel's avatar
Joseph Mirabel committed
18
19
    , frontIdx_ (0)
    , backIdx_ (0)
Joseph Mirabel's avatar
Joseph Mirabel committed
20
    , oss_ (NULL)
21
    , nbDiscarded_ (0)
Joseph Mirabel's avatar
Joseph Mirabel committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  {
    for (std::size_t i = 0; i < buffer_.size(); ++i)
      buffer_[i] = new Data;
  }

  RealTimeLogger::~RealTimeLogger ()
  {
    // Check that we are not spinning...
    for (std::size_t i = 0; i < buffer_.size(); ++i) delete buffer_[i];
  }

  bool RealTimeLogger::spinOnce ()
  {
    if (empty()) return false;
    Data* data = buffer_[frontIdx_];
    frontIdx_ = (frontIdx_ + 1) % buffer_.size();
    std::string str = data->buf.str();
    // It is important to pass str.c_str() and not str
    // because the str object may contains a '\0' so
    // str.size() may be different from strlen(str.c_str())
    for (std::size_t i = 0; i < outputs_.size(); ++i)
      outputs_[i]->write (str.c_str());
    return true;
  }

  RTLoggerStream RealTimeLogger::front ()
  {
49
    // If no output or if buffer is full, discard message.
Joseph Mirabel's avatar
Joseph Mirabel committed
50
    if (outputs_.empty() || full()) {
51
52
53
54
55
56
57
      nbDiscarded_++;
      return RTLoggerStream (NULL, oss_);
    }
    bool alone = wmutex.try_lock();
    // If someone is writting, discard message.
    if (!alone) {
      nbDiscarded_++;
Joseph Mirabel's avatar
Joseph Mirabel committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
      return RTLoggerStream (NULL, oss_);
    }
    Data* data = buffer_[backIdx_];
    //backIdx_ = (backIdx_+1) % buffer_.size();
    // Reset position of cursor
    data->buf.pubseekpos(0);
    oss_.rdbuf(&data->buf);
    return RTLoggerStream (this, oss_);
  }

  RTLoggerStream::~RTLoggerStream()
  {
    os_ << std::ends;
    if (logger_ != NULL) logger_->frontReady();
  }
73
74
75
76
77
78
79
80
81
82
83
84
85

  struct RealTimeLogger::thread
  {
    bool requestShutdown_;
    boost::thread t_;

    thread (RealTimeLogger* logger)
      : requestShutdown_ (false)
      , t_ (&thread::spin, this, logger)
    {}

    void spin (RealTimeLogger* logger)
    {
86
87
88
89
      // Change the thread's scheduler from real-time to normal and reduce its priority
      int threadPolicy;
      struct sched_param threadParam;
      if (pthread_getschedparam (pthread_self(), &threadPolicy, &threadParam) == 0)
90
91
92
93
94
	{
	  threadPolicy = SCHED_OTHER;
	  threadParam.sched_priority -= 5;
	  if (threadParam.sched_priority < sched_get_priority_min (threadPolicy))
	    threadParam.sched_priority = sched_get_priority_min (threadPolicy);
Guilhem Saurel's avatar
Guilhem Saurel committed
95

96
97
	  pthread_setschedparam (pthread_self(), threadPolicy, &threadParam);
	}
98

99
      while (!requestShutdown_ || !logger->empty())
100
101
102
103
104
105
	{
	  // If the logger did not write anything, it means the buffer is empty.
	  // Do a pause
	  if (!logger->spinOnce())
	    boost::this_thread::sleep(boost::posix_time::milliseconds(100));
	}
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
    }
  };

  RealTimeLogger* RealTimeLogger::instance_ = NULL;
  RealTimeLogger::thread* RealTimeLogger::thread_ = NULL;

  RealTimeLogger& RealTimeLogger::instance()
  {
    if (instance_ == NULL) {
      instance_ = new RealTimeLogger (1000);
      thread_ = new thread (instance_);
    }
    return *instance_;
  }

  void RealTimeLogger::destroy ()
  {
    if (instance_ == NULL) return;
    thread_->requestShutdown_ = true;
    thread_->t_.join();
    delete instance_;
    delete thread_;
  }
Joseph Mirabel's avatar
Joseph Mirabel committed
129
}