LogRecord.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 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 #ifndef _GAZEBO_UTIL_LOGRECORD_HH_
18 #define _GAZEBO_UTIL_LOGRECORD_HH_
19 
20 #include <fstream>
21 #include <set>
22 #include <string>
23 
24 #include "gazebo/msgs/msgs.hh"
26 #include "gazebo/util/system.hh"
27 
28 #define GZ_LOG_VERSION "1.0"
29 
30 namespace gazebo
31 {
32  namespace util
33  {
37  {
39  public: std::string encoding = "zlib";
40 
42  public: std::string path;
43 
46  public: double period = -1;
47 
49  public: std::string filter;
50  };
51 
52  // Forward declare private data class
53  class LogRecordPrivate;
54 
57 
75  class GZ_UTIL_VISIBLE LogRecord : public SingletonT<LogRecord>
76  {
78  private: LogRecord();
79 
81  private: virtual ~LogRecord();
82 
89  public: bool Init(const std::string &_subdir);
90 
103  public: void Add(const std::string &_name, const std::string &_filename,
104  std::function<bool (std::ostringstream &)> _logCallback);
105 
114  public: bool Remove(const std::string &_name);
115 
117  public: void Stop();
118 
120  public: void Notify();
121 
126  public: void SetPaused(const bool _paused);
127 
131  public: bool Paused() const;
132 
135  public: double Period() const;
136 
139  public: void SetPeriod(const double _period);
140 
143  public: std::string Filter() const;
144 
147  public: void SetFilter(const std::string &_filter);
148 
152  public: bool RecordResources() const;
153 
156  public: void SetRecordResources(const bool _record);
157 
160  // \return True if logger is ready to start.
161  public: bool IsReadyToStart() const;
162 
165  public: bool Running() const;
166 
169  public: bool Start(const LogRecordParams &_params);
170 
174  public: bool Start(const std::string &_encoding="zlib",
175  const std::string &_path="");
176 
180  public: const std::string &Encoding() const;
181 
185  public: std::string Filename(const std::string &_name = "") const;
186 
190  public: unsigned int FileSize(const std::string &_name = "") const;
191 
194  public: void SetBasePath(const std::string &_path);
195 
198  public: std::string BasePath() const;
199 
202  public: common::Time RunTime() const;
203 
205  public: void Fini();
206 
209  public: bool FirstUpdate() const;
210 
213  public: bool SaveModels(const std::set<std::string> &models);
214 
218  public: bool SaveFiles(const std::set<std::string> &resources);
219 
222  public: void Write(const bool _force = false);
223 
226  public: unsigned int BufferSize() const;
227 
232  private: void Update();
233 
235  private: void RunUpdate();
236 
238  private: void RunWrite();
239 
241  private: void ClearLogs();
242 
244  private: void PublishLogStatus();
245 
248  private: void OnLogControl(ConstLogControlPtr &_data);
249 
253  private: void Cleanup();
254 
256  private: void OnPause(const bool _pause);
257 
259  private: friend class SingletonT<LogRecord>;
260 
263  private: std::unique_ptr<LogRecordPrivate> dataPtr;
264  };
266  }
267 }
268 #endif
const std::string & Encoding() const
Get the encoding used.
bool SaveModels(const std::set< std::string > &models)
Return true if all the models are saved successfully.
void SetPeriod(const double _period)
Set the log recording period.
Forward declarations for the common classes.
Definition: Animation.hh:26
bool Init(const std::string &_subdir)
Initialize logging into a subdirectory.
A Time class, can be used to hold wall- or sim-time. stored as sec and nano-sec.
Definition: Time.hh:44
std::string filter
Log filter string.
Definition: LogRecord.hh:49
Singleton template class.
Definition: SingletonT.hh:33
unsigned int FileSize(const std::string &_name="") const
Get the file size for a log object.
std::string BasePath() const
Get the base path for a log recording.
void Add(const std::string &_name, const std::string &_filename, std::function< bool(std::ostringstream &)> _logCallback)
Add an object to a log file.
double period
Recording period.
Definition: LogRecord.hh:46
void Fini()
Finialize, and shutdown.
bool RecordResources() const
Get whether the model meshes and materials are saved when recording.
bool Running() const
Get whether logging is running.
std::string Filename(const std::string &_name="") const
Get the filename for a log object.
void Notify()
Tell the recorder that an update should occur.
bool Remove(const std::string &_name)
Remove an entity from a log.
std::string encoding
The type of encoding (txt, zlib, or bz2).
Definition: LogRecord.hh:39
addtogroup gazebo_util
Definition: LogRecord.hh:75
std::string path
Path in which to store log files.
Definition: LogRecord.hh:42
bool IsReadyToStart() const
Get whether the logger is ready to start, which implies that any previous runs have finished.
Log recording parameters.
Definition: LogRecord.hh:36
double Period() const
Get the log recording period.
bool Start(const LogRecordParams &_params)
Start the logger.
bool FirstUpdate() const
Return true if an Update has not yet been completed.
unsigned int BufferSize() const
Get the size of the buffer.
GAZEBO_VISIBLE void Init(google::protobuf::Message &_message, const std::string &_id="")
Initialize a message.
void SetPaused(const bool _paused)
Set whether logging should pause.
bool Paused() const
Get whether logging is paused.
std::string Filter() const
Get the log recording filter string.
void Stop()
Stop the logger.
common::Time RunTime() const
Get the run time in sim time.
void SetFilter(const std::string &_filter)
Set the log recording filter string.
void SetRecordResources(const bool _record)
Set whether to save model meshes and materials when recording.
void Write(const bool _force=false)
Write all logs.
void SetBasePath(const std::string &_path)
Set the base path.
bool SaveFiles(const std::set< std::string > &resources)
Return true if all the files are saved successfully.