LogRecord.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2015 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 /* Desc: A class to log data
18  * Author: Nate Koenig
19  * Date: 1 Jun 2010
20  */
21 
22 #ifndef _LOGRECORD_HH_
23 #define _LOGRECORD_HH_
24 
25 #include <fstream>
26 #include <string>
27 #include <map>
28 #include <boost/thread.hpp>
29 #include <boost/archive/iterators/base64_from_binary.hpp>
30 #include <boost/archive/iterators/insert_linebreaks.hpp>
31 #include <boost/archive/iterators/transform_width.hpp>
32 #include <boost/archive/iterators/ostream_iterator.hpp>
33 #include <boost/filesystem.hpp>
34 #include <boost/function.hpp>
35 
36 #include "gazebo/msgs/msgs.hh"
39 #include "gazebo/common/Event.hh"
41 #include "gazebo/util/system.hh"
42 
43 #define GZ_LOG_VERSION "1.0"
44 
45 namespace gazebo
46 {
47  namespace util
48  {
51 
69  class GZ_UTIL_VISIBLE LogRecord : public SingletonT<LogRecord>
70  {
72  private: LogRecord();
73 
75  private: virtual ~LogRecord();
76 
83  public: bool Init(const std::string &_subdir);
84 
97  public: void Add(const std::string &_name, const std::string &_filename,
98  boost::function<bool (std::ostringstream &)> _logCallback);
99 
108  public: bool Remove(const std::string &_name);
109 
111  public: void Stop();
112 
114  public: void Notify();
115 
120  public: void SetPaused(bool _paused);
121 
125  public: bool GetPaused() const;
126 
129  // \return True if logger is ready to start.
130  public: bool IsReadyToStart() const;
131 
134  public: bool GetRunning() const;
135 
139  public: bool Start(const std::string &_encoding="zlib",
140  const std::string &_path="");
141 
145  public: const std::string &GetEncoding() const;
146 
150  public: std::string GetFilename(const std::string &_name = "") const;
151 
155  public: unsigned int GetFileSize(const std::string &_name = "") const;
156 
159  public: void SetBasePath(const std::string &_path);
160 
163  public: std::string GetBasePath() const;
164 
167  public: common::Time GetRunTime() const;
168 
170  public: void Fini();
171 
174  public: bool GetFirstUpdate() const;
175 
178  public: void Write(bool _force = false);
179 
182  public: unsigned int GetBufferSize() const;
183 
188  // private: void Update(const common::UpdateInfo &_info);
189  private: void Update();
190 
192  private: void RunUpdate();
193 
195  private: void RunWrite();
196 
198  private: void ClearLogs();
199 
201  private: void PublishLogStatus();
202 
205  private: void OnLogControl(ConstLogControlPtr &_data);
206 
210  private: void Cleanup();
211 
213  private: void OnPause(bool _pause);
214 
216  private: class Log
217  {
224  public: Log(LogRecord *_parent, const std::string &_relativeFilename,
225  boost::function<bool (std::ostringstream &)> _logCB);
226 
228  public: virtual ~Log();
229 
232  public: void Start(const boost::filesystem::path &_path);
233 
235  public: void Stop();
236 
238  public: void Write();
239 
242  public: unsigned int Update();
243 
245  public: void ClearBuffer();
246 
249  public: unsigned int GetBufferSize();
250 
254  public: std::string GetRelativeFilename() const;
255 
258  public: std::string GetCompleteFilename() const;
259 
261  public: LogRecord *parent;
262 
264  public: boost::function<bool (std::ostringstream &)> logCB;
265 
267  public: std::string buffer;
268 
270  public: std::ofstream logFile;
271 
273  public: std::string relativeFilename;
274 
276  private: boost::filesystem::path completePath;
277  };
279 
282  private: typedef std::map<std::string, Log*> Log_M;
283 
285  private: Log_M logs;
286 
288  private: Log_M::iterator updateIter;
289 
291  private: Log_M::iterator logsEnd;
292 
294  private: boost::condition_variable startThreadCondition;
295 
297  private: boost::condition_variable updateCondition;
298 
300  private: boost::condition_variable cleanupCondition;
301 
303  private: bool running;
304 
306  private: boost::thread *writeThread;
307 
309  private: boost::thread *updateThread;
310 
312  private: boost::thread cleanupThread;
313 
315  private: mutable boost::mutex writeMutex;
316 
318  private: mutable boost::mutex runWriteMutex;
319 
321  private: mutable boost::mutex updateMutex;
322 
324  private: boost::mutex controlMutex;
325 
328  private: boost::condition_variable dataAvailableCondition;
329 
331  private: boost::filesystem::path logBasePath;
332 
334  private: boost::filesystem::path logCompletePath;
335 
338  private: std::string logSubDir;
339 
341  private: std::string encoding;
342 
344  private: bool initialized;
345 
347  private: bool paused;
348 
350  private: bool firstUpdate;
351 
353  private: bool stopThread;
354 
356  private: common::Time startTime;
357 
359  private: common::Time currTime;
360 
362  private: transport::NodePtr node;
363 
365  private: transport::SubscriberPtr logControlSub;
366 
368  private: transport::PublisherPtr logStatusPub;
369 
371  private: event::Connection_V connections;
372 
374  private: bool pauseState;
375 
377  private: friend class SingletonT<LogRecord>;
378 
381  private: bool readyToStart;
382  };
384  }
385 }
386 #endif
Forward declarations for the common classes.
Definition: Animation.hh:33
boost::shared_ptr< Subscriber > SubscriberPtr
Definition: TransportTypes.hh:53
Forward declarations for transport.
Singleton template class.
Definition: SingletonT.hh:33
#define GZ_UTIL_VISIBLE
Definition: system.hh:266
addtogroup gazebo_util
Definition: LogRecord.hh:69
std::vector< ConnectionPtr > Connection_V
Definition: CommonTypes.hh:151
boost::shared_ptr< Node > NodePtr
Definition: TransportTypes.hh:57
GAZEBO_VISIBLE void Init(google::protobuf::Message &_message, const std::string &_id="")
Initialize a message.
boost::shared_ptr< Publisher > PublisherPtr
Definition: TransportTypes.hh:49
A Time class, can be used to hold wall- or sim-time.
Definition: Time.hh:39