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 2012 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 
36 #include "gazebo/common/Event.hh"
38 
39 #define GZ_LOG_VERSION "1.0"
40 
41 namespace gazebo
42 {
43  namespace common
44  {
47 
65  class LogRecord : public SingletonT<LogRecord>
66  {
68  private: LogRecord();
69 
71  private: virtual ~LogRecord();
72 
79  public: bool Init(const std::string &_subdir);
80 
93  public: void Add(const std::string &_name, const std::string &_filename,
94  boost::function<bool (std::ostringstream &)> _logCallback);
95 
104  public: bool Remove(const std::string &_name);
105 
107  public: void Stop();
108 
113  public: void SetPaused(bool _paused);
114 
118  public: bool GetPaused() const;
119 
122  public: bool GetRunning() const;
123 
126  public: bool Start(const std::string &_encoding="bz2");
127 
131  public: const std::string &GetEncoding() const;
132 
136  public: std::string GetFilename(const std::string &_name) const;
137 
141  public: unsigned int GetFileSize(const std::string &_name) const;
142 
145  public: void SetBasePath(const std::string &_path);
146 
149  public: std::string GetBasePath() const;
150 
153  public: common::Time GetRunTime() const;
154 
156  public: void Fini();
157 
160  public: bool GetFirstUpdate() const;
161 
166  private: void Update(const common::UpdateInfo &_info);
167 
169  private: void Run();
170 
172  private: void ClearLogs();
173 
175  // private: void WriteHeader();
176 
178  private: class Log
179  {
186  public: Log(LogRecord *_parent, const std::string &_relativeFilename,
187  boost::function<bool (std::ostringstream &)> _logCB);
188 
190  public: virtual ~Log();
191 
194  public: void Start(const boost::filesystem::path &_path);
195 
197  public: void Write();
198 
201  public: unsigned int Update();
202 
204  public: void ClearBuffer();
205 
208  public: unsigned int GetBufferSize();
209 
213  public: std::string GetRelativeFilename() const;
214 
217  public: std::string GetCompleteFilename() const;
218 
220  public: LogRecord *parent;
221 
223  public: boost::function<bool (std::ostringstream &)> logCB;
224 
226  public: std::string buffer;
227 
229  public: std::ofstream logFile;
230 
232  public: std::string relativeFilename;
233 
234  private: boost::filesystem::path completePath;
235  };
237 
240  private: typedef std::map<std::string, Log*> Log_M;
241 
243  private: Log_M logs;
244 
246  private: Log_M::iterator updateIter;
247 
249  private: Log_M::iterator logsEnd;
250 
252  private: event::ConnectionPtr updateConnection;
253 
255  private: bool running;
256 
258  private: boost::thread *writeThread;
259 
261  private: mutable boost::mutex writeMutex;
262 
264  private: boost::mutex controlMutex;
265 
268  private: boost::condition_variable dataAvailableCondition;
269 
271  private: boost::filesystem::path logBasePath;
272 
274  private: boost::filesystem::path logCompletePath;
275 
278  private: std::string logSubDir;
279 
281  private: std::string encoding;
282 
284  private: bool initialized;
285 
287  private: bool paused;
288 
290  private: bool firstUpdate;
291 
293  private: bool stopThread;
294 
296  private: common::Time startTime;
297 
299  private: common::Time currTime;
300 
302  private: friend class SingletonT<LogRecord>;
303  };
305  }
306 }
307 #endif