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 
41 #define GZ_LOG_VERSION "1.0"
42 
43 namespace gazebo
44 {
45  namespace util
46  {
49 
67  class LogRecord : public SingletonT<LogRecord>
68  {
70  private: LogRecord();
71 
73  private: virtual ~LogRecord();
74 
81  public: bool Init(const std::string &_subdir);
82 
95  public: void Add(const std::string &_name, const std::string &_filename,
96  boost::function<bool (std::ostringstream &)> _logCallback);
97 
106  public: bool Remove(const std::string &_name);
107 
109  public: void Stop();
110 
112  public: void Notify();
113 
118  public: void SetPaused(bool _paused);
119 
123  public: bool GetPaused() const;
124 
127  // \return True if logger is ready to start.
128  public: bool IsReadyToStart() const;
129 
132  public: bool GetRunning() const;
133 
137  public: bool Start(const std::string &_encoding="zlib",
138  const std::string &_path="");
139 
143  public: const std::string &GetEncoding() const;
144 
148  public: std::string GetFilename(const std::string &_name = "") const;
149 
153  public: unsigned int GetFileSize(const std::string &_name = "") const;
154 
157  public: void SetBasePath(const std::string &_path);
158 
161  public: std::string GetBasePath() const;
162 
165  public: common::Time GetRunTime() const;
166 
168  public: void Fini();
169 
172  public: bool GetFirstUpdate() const;
173 
176  public: void Write(bool _force = false);
177 
180  public: unsigned int GetBufferSize() const;
181 
186  // private: void Update(const common::UpdateInfo &_info);
187  private: void Update();
188 
190  private: void RunUpdate();
191 
193  private: void RunWrite();
194 
196  private: void ClearLogs();
197 
199  private: void PublishLogStatus();
200 
203  private: void OnLogControl(ConstLogControlPtr &_data);
204 
208  private: void Cleanup();
209 
211  private: void OnPause(bool _pause);
212 
214  private: class Log
215  {
222  public: Log(LogRecord *_parent, const std::string &_relativeFilename,
223  boost::function<bool (std::ostringstream &)> _logCB);
224 
226  public: virtual ~Log();
227 
230  public: void Start(const boost::filesystem::path &_path);
231 
233  public: void Stop();
234 
236  public: void Write();
237 
240  public: unsigned int Update();
241 
243  public: void ClearBuffer();
244 
247  public: unsigned int GetBufferSize();
248 
252  public: std::string GetRelativeFilename() const;
253 
256  public: std::string GetCompleteFilename() const;
257 
259  public: LogRecord *parent;
260 
262  public: boost::function<bool (std::ostringstream &)> logCB;
263 
265  public: std::string buffer;
266 
268  public: std::ofstream logFile;
269 
271  public: std::string relativeFilename;
272 
274  private: boost::filesystem::path completePath;
275  };
277 
280  private: typedef std::map<std::string, Log*> Log_M;
281 
283  private: Log_M logs;
284 
286  private: Log_M::iterator updateIter;
287 
289  private: Log_M::iterator logsEnd;
290 
292  private: boost::condition_variable startThreadCondition;
293 
295  private: boost::condition_variable updateCondition;
296 
298  private: boost::condition_variable cleanupCondition;
299 
301  private: bool running;
302 
304  private: boost::thread *writeThread;
305 
307  private: boost::thread *updateThread;
308 
310  private: boost::thread cleanupThread;
311 
313  private: mutable boost::mutex writeMutex;
314 
316  private: mutable boost::mutex runWriteMutex;
317 
319  private: mutable boost::mutex updateMutex;
320 
322  private: boost::mutex controlMutex;
323 
326  private: boost::condition_variable dataAvailableCondition;
327 
329  private: boost::filesystem::path logBasePath;
330 
332  private: boost::filesystem::path logCompletePath;
333 
336  private: std::string logSubDir;
337 
339  private: std::string encoding;
340 
342  private: bool initialized;
343 
345  private: bool paused;
346 
348  private: bool firstUpdate;
349 
351  private: bool stopThread;
352 
354  private: common::Time startTime;
355 
357  private: common::Time currTime;
358 
360  private: transport::NodePtr node;
361 
363  private: transport::SubscriberPtr logControlSub;
364 
366  private: transport::PublisherPtr logStatusPub;
367 
369  private: event::Connection_V connections;
370 
372  private: bool pauseState;
373 
375  private: friend class SingletonT<LogRecord>;
376 
379  private: bool readyToStart;
380  };
382  }
383 }
384 #endif