LogRecordPrivate.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015-2016 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 #ifndef _GAZEBO_UTIL_LOGRECORD_PRIVATE_HH_
18 #define _GAZEBO_UTIL_LOGRECORD_PRIVATE_HH_
19 
20 #include <map>
21 #include <string>
22 #include <thread>
23 #include <functional>
24 #include <condition_variable>
25 #include <boost/filesystem.hpp>
26 
27 namespace gazebo
28 {
29  namespace util
30  {
31  class LogRecord;
32 
36  {
38  public: virtual ~LogRecordPrivate() = default;
39 
41  public: class Log
42  {
49  public: Log(LogRecord *_parent, const std::string &_relativeFilename,
50  std::function<bool (std::ostringstream &)> _logCB);
51 
53  public: virtual ~Log();
54 
57  public: void Start(const boost::filesystem::path &_path);
58 
60  public: void Stop();
61 
63  public: void Write();
64 
67  public: unsigned int Update();
68 
70  public: void ClearBuffer();
71 
74  public: unsigned int BufferSize();
75 
79  public: std::string RelativeFilename() const;
80 
83  public: std::string CompleteFilename() const;
84 
86  public: LogRecord *parent;
87 
89  public: std::function<bool (std::ostringstream &)> logCB;
90 
92  public: std::string buffer;
93 
95  public: std::ofstream logFile;
96 
98  public: std::string relativeFilename;
99 
101  public: boost::filesystem::path completePath;
102  };
103 
106  public: typedef std::map<std::string, Log*> Log_M;
107 
109  public: Log_M logs;
110 
112  public: Log_M::iterator updateIter;
113 
115  public: Log_M::iterator logsEnd;
116 
118  public: std::condition_variable startThreadCondition;
119 
121  public: std::condition_variable updateCondition;
122 
124  public: std::condition_variable cleanupCondition;
125 
127  public: bool running;
128 
130  public: std::unique_ptr<std::thread> writeThread;
131 
133  public: std::unique_ptr<std::thread> updateThread;
134 
136  public: std::unique_ptr<std::thread> cleanupThread;
137 
139  public: mutable std::mutex writeMutex;
140 
142  public: mutable std::mutex runWriteMutex;
143 
145  public: mutable std::mutex updateMutex;
146 
148  public: std::mutex controlMutex;
149 
152  public: std::condition_variable dataAvailableCondition;
153 
155  public: boost::filesystem::path logBasePath;
156 
158  public: boost::filesystem::path logCompletePath;
159 
162  public: std::string logSubDir;
163 
165  public: std::string encoding;
166 
168  public: bool initialized;
169 
171  public: bool paused;
172 
174  public: bool firstUpdate;
175 
177  public: bool stopThread;
178 
181 
184 
187 
190 
193 
196 
198  public: bool pauseState;
199 
202  public: bool readyToStart;
203  };
205  }
206 }
207 #endif
std::string logSubDir
Subdirectory for log files.
Definition: LogRecordPrivate.hh:162
bool pauseState
Simulation pause state.
Definition: LogRecordPrivate.hh:198
std::ofstream logFile
The log file.
Definition: LogRecordPrivate.hh:95
transport::SubscriberPtr logControlSub
Subscriber to log control messages.
Definition: LogRecordPrivate.hh:189
boost::filesystem::path completePath
Complete file path.
Definition: LogRecordPrivate.hh:101
bool paused
True to pause recording.
Definition: LogRecordPrivate.hh:171
std::string CompleteFilename() const
Get the complete filename.
std::string buffer
Data buffer.
Definition: LogRecordPrivate.hh:92
virtual ~LogRecordPrivate()=default
Destructor (makes style checker happy)
std::condition_variable updateCondition
Condition used to trigger an update.
Definition: LogRecordPrivate.hh:121
LogRecord * parent
Pointer to the log record parent.
Definition: LogRecordPrivate.hh:86
boost::shared_ptr< Subscriber > SubscriberPtr
Definition: TransportTypes.hh:53
bool running
True if logging is running.
Definition: LogRecordPrivate.hh:127
std::mutex writeMutex
Mutex to protect against parallel calls to Write()
Definition: LogRecordPrivate.hh:139
bool initialized
True if initialized.
Definition: LogRecordPrivate.hh:168
std::unique_ptr< std::thread > updateThread
Thread used to update data.
Definition: LogRecordPrivate.hh:133
bool stopThread
Flag used to stop the write thread.
Definition: LogRecordPrivate.hh:177
Log(LogRecord *_parent, const std::string &_relativeFilename, std::function< bool(std::ostringstream &)> _logCB)
Constructor.
transport::NodePtr node
Transportation node.
Definition: LogRecordPrivate.hh:186
std::string relativeFilename
Relative log filename.
Definition: LogRecordPrivate.hh:98
common::Time startTime
Start simulation time.
Definition: LogRecordPrivate.hh:180
std::condition_variable cleanupCondition
Used by the cleanupThread to wait for a cleanup signal.
Definition: LogRecordPrivate.hh:124
boost::filesystem::path logBasePath
The base pathname for all the logs.
Definition: LogRecordPrivate.hh:155
Log_M logs
All the log objects.
Definition: LogRecordPrivate.hh:109
boost::filesystem::path logCompletePath
The complete pathname for all the logs.
Definition: LogRecordPrivate.hh:158
Definition: LogRecordPrivate.hh:35
std::mutex updateMutex
Mutex to synchronize with RunUpdate()
Definition: LogRecordPrivate.hh:145
std::condition_variable dataAvailableCondition
Used by the write thread to know when data needs to be written to disk.
Definition: LogRecordPrivate.hh:152
std::function< bool(std::ostringstream &)> logCB
Callback from which to get data.
Definition: LogRecordPrivate.hh:89
addtogroup gazebo_util
Definition: LogRecord.hh:56
std::condition_variable startThreadCondition
Condition used to start threads.
Definition: LogRecordPrivate.hh:118
Log_M::iterator updateIter
Iterator used to update the log objects.
Definition: LogRecordPrivate.hh:112
Log helper class.
Definition: LogRecordPrivate.hh:41
bool readyToStart
True if the logger is ready to start, and the previous run has finished.
Definition: LogRecordPrivate.hh:202
void Write()
Write data to disk.
std::map< std::string, Log * > Log_M
Definition: LogRecordPrivate.hh:106
void Start(const boost::filesystem::path &_path)
Start the log.
std::vector< ConnectionPtr > Connection_V
Definition: CommonTypes.hh:157
boost::shared_ptr< Node > NodePtr
Definition: TransportTypes.hh:57
common::Time currTime
Current simulation time.
Definition: LogRecordPrivate.hh:183
unsigned int Update()
Update the data buffer.
bool firstUpdate
Used to indicate the first update callback.
Definition: LogRecordPrivate.hh:174
std::mutex runWriteMutex
Mutex to synchronize with RunWrite()
Definition: LogRecordPrivate.hh:142
void ClearBuffer()
Clear the data buffer.
event::Connection_V connections
All the event connections.
Definition: LogRecordPrivate.hh:195
std::unique_ptr< std::thread > cleanupThread
Thread to cleanup log recording.
Definition: LogRecordPrivate.hh:136
std::mutex controlMutex
Mutex to protect logging control.
Definition: LogRecordPrivate.hh:148
transport::PublisherPtr logStatusPub
Publisher of log status messages.
Definition: LogRecordPrivate.hh:192
std::string encoding
Encoding format for each chunk.
Definition: LogRecordPrivate.hh:165
std::string RelativeFilename() const
Get the relative filename.
boost::shared_ptr< Publisher > PublisherPtr
Definition: TransportTypes.hh:49
Log_M::iterator logsEnd
Convenience iterator to the end of the log objects map.
Definition: LogRecordPrivate.hh:115
unsigned int BufferSize()
Get the byte size of the buffer.
std::unique_ptr< std::thread > writeThread
Thread used to write data to disk.
Definition: LogRecordPrivate.hh:130
A Time class, can be used to hold wall- or sim-time.
Definition: Time.hh:44