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 
35 #include "common/Event.hh"
36 #include "common/SingletonT.hh"
37 
38 #define GZ_LOG_VERSION "1.0"
39 
40 namespace gazebo
41 {
42  namespace common
43  {
46 
64  class LogRecord : public SingletonT<LogRecord>
65  {
67  private: LogRecord();
68 
70  private: virtual ~LogRecord();
71 
78  public: bool Init(const std::string &_subdir);
79 
92  public: void Add(const std::string &_name, const std::string &_filename,
93  boost::function<bool (std::ostringstream &)> _logCallback);
94 
103  public: bool Remove(const std::string &_name);
104 
106  public: void Stop();
107 
110  public: void Start(const std::string &_encoding="bz2");
111 
115  public: const std::string &GetEncoding() const;
116 
119  public: bool GetRunning() const;
120 
125  private: void Update();
126 
128  private: void Run();
129 
131  // private: void WriteHeader();
132 
134  private: class Log
135  {
136  public: Log(LogRecord *_parent, const std::string &_filename,
137  boost::function<bool (std::ostringstream &)> _logCB);
138 
139  public: virtual ~Log();
140 
141  public: void Write();
142 
143  public: void Update();
144 
145  public: void ClearBuffer();
146 
147  public: LogRecord *parent;
148  public: boost::function<bool (std::ostringstream &)> logCB;
149  public: std::string buffer;
150  public: std::ofstream logFile;
151  public: std::string filename;
152  };
154 
157  private: typedef std::map<std::string, Log*> Log_M;
158 
160  private: Log_M logs;
161 
163  private: Log_M::iterator updateIter;
164 
166  private: Log_M::iterator logsEnd;
167 
169  private: event::ConnectionPtr updateConnection;
170 
172  private: bool stop;
173 
175  private: boost::thread *writeThread;
176 
178  private: boost::mutex writeMutex;
179 
181  private: boost::mutex controlMutex;
182 
185  private: boost::condition_variable dataAvailableCondition;
186 
188  private: boost::filesystem::path logPath;
189 
191  private: std::string encoding;
192 
194  private: bool initialized;
195 
197  private: friend class SingletonT<LogRecord>;
198  };
200  }
201 }
202 #endif