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 Nate Koenig
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 
121  private: void Update();
122 
124  private: void Run();
125 
127  // private: void WriteHeader();
128 
130  private: class Log
131  {
132  public: Log(LogRecord *_parent, const std::string &_filename,
133  boost::function<bool (std::ostringstream &)> _logCB);
134 
135  public: virtual ~Log();
136 
137  public: void Write();
138 
139  public: void Update();
140 
141  public: void ClearBuffer();
142 
143  public: LogRecord *parent;
144  public: boost::function<bool (std::ostringstream &)> logCB;
145  public: std::string buffer;
146  public: std::ofstream logFile;
147  public: std::string filename;
148  };
150 
153  private: typedef std::map<std::string, Log*> Log_M;
154 
156  private: Log_M logs;
157 
159  private: Log_M::iterator updateIter;
160 
162  private: Log_M::iterator logsEnd;
163 
165  private: event::ConnectionPtr updateConnection;
166 
168  private: bool stop;
169 
171  private: boost::thread *writeThread;
172 
174  private: boost::mutex writeMutex;
175 
177  private: boost::mutex controlMutex;
178 
181  private: boost::condition_variable dataAvailableCondition;
182 
184  private: boost::filesystem::path logPath;
185 
187  private: std::string encoding;
188 
190  private: bool initialized;
191 
193  private: friend class SingletonT<LogRecord>;
194  };
196  }
197 }
198 #endif