All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LogRecord.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2014 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 
35 #include "gazebo/msgs/msgs.hh"
38 #include "gazebo/common/Event.hh"
40 #include "gazebo/util/system.hh"
41 
42 #define GZ_LOG_VERSION "1.0"
43 
44 namespace gazebo
45 {
46  namespace util
47  {
50 
68  class GAZEBO_VISIBLE LogRecord : public SingletonT<LogRecord>
69  {
71  private: LogRecord();
72 
74  private: virtual ~LogRecord();
75 
82  public: bool Init(const std::string &_subdir);
83 
96  public: void Add(const std::string &_name, const std::string &_filename,
97  boost::function<bool (std::ostringstream &)> _logCallback);
98 
107  public: bool Remove(const std::string &_name);
108 
110  public: void Stop();
111 
113  public: void Notify();
114 
119  public: void SetPaused(bool _paused);
120 
124  public: bool GetPaused() const;
125 
128  // \return True if logger is ready to start.
129  public: bool IsReadyToStart() const;
130 
133  public: bool GetRunning() const;
134 
138  public: bool Start(const std::string &_encoding="zlib",
139  const std::string &_path="");
140 
144  public: const std::string &GetEncoding() const;
145 
149  public: std::string GetFilename(const std::string &_name = "") const;
150 
154  public: unsigned int GetFileSize(const std::string &_name = "") const;
155 
158  public: void SetBasePath(const std::string &_path);
159 
162  public: std::string GetBasePath() const;
163 
166  public: common::Time GetRunTime() const;
167 
169  public: void Fini();
170 
173  public: bool GetFirstUpdate() const;
174 
177  public: void Write(bool _force = false);
178 
181  public: unsigned int GetBufferSize() const;
182 
187  // private: void Update(const common::UpdateInfo &_info);
188  private: void Update();
189 
191  private: void RunUpdate();
192 
194  private: void RunWrite();
195 
197  private: void ClearLogs();
198 
200  private: void PublishLogStatus();
201 
204  private: void OnLogControl(ConstLogControlPtr &_data);
205 
209  private: void Cleanup();
210 
212  private: void OnPause(bool _pause);
213 
215  private: class Log
216  {
223  public: Log(LogRecord *_parent, const std::string &_relativeFilename,
224  boost::function<bool (std::ostringstream &)> _logCB);
225 
227  public: virtual ~Log();
228 
231  public: void Start(const boost::filesystem::path &_path);
232 
234  public: void Stop();
235 
237  public: void Write();
238 
241  public: unsigned int Update();
242 
244  public: void ClearBuffer();
245 
248  public: unsigned int GetBufferSize();
249 
253  public: std::string GetRelativeFilename() const;
254 
257  public: std::string GetCompleteFilename() const;
258 
260  public: LogRecord *parent;
261 
263  public: boost::function<bool (std::ostringstream &)> logCB;
264 
266  public: std::string buffer;
267 
269  public: std::ofstream logFile;
270 
272  public: std::string relativeFilename;
273 
275  private: boost::filesystem::path completePath;
276  };
278 
281  private: typedef std::map<std::string, Log*> Log_M;
282 
284  private: Log_M logs;
285 
287  private: Log_M::iterator updateIter;
288 
290  private: Log_M::iterator logsEnd;
291 
293  private: boost::condition_variable startThreadCondition;
294 
296  private: boost::condition_variable updateCondition;
297 
299  private: boost::condition_variable cleanupCondition;
300 
302  private: bool running;
303 
305  private: boost::thread *writeThread;
306 
308  private: boost::thread *updateThread;
309 
311  private: boost::thread cleanupThread;
312 
314  private: mutable boost::mutex writeMutex;
315 
317  private: mutable boost::mutex runWriteMutex;
318 
320  private: mutable boost::mutex updateMutex;
321 
323  private: boost::mutex controlMutex;
324 
327  private: boost::condition_variable dataAvailableCondition;
328 
330  private: boost::filesystem::path logBasePath;
331 
333  private: boost::filesystem::path logCompletePath;
334 
337  private: std::string logSubDir;
338 
340  private: std::string encoding;
341 
343  private: bool initialized;
344 
346  private: bool paused;
347 
349  private: bool firstUpdate;
350 
352  private: bool stopThread;
353 
355  private: common::Time startTime;
356 
358  private: common::Time currTime;
359 
361  private: transport::NodePtr node;
362 
364  private: transport::SubscriberPtr logControlSub;
365 
367  private: transport::PublisherPtr logStatusPub;
368 
370  private: event::Connection_V connections;
371 
373  private: bool pauseState;
374 
376  private: friend class SingletonT<LogRecord>;
377 
380  private: bool readyToStart;
381  };
383  }
384 }
385 #endif