22 #ifndef REGILO_LOG_HPP 23 #define REGILO_LOG_HPP 33 #include <boost/algorithm/string/predicate.hpp> 35 #include "regilo/utils.hpp" 48 virtual ~ILog() =
default;
66 virtual bool isEnd()
const = 0;
72 virtual std::string
read() = 0;
79 virtual std::string
read(std::string& logCommand) = 0;
86 virtual std::string
readCommand(
const std::string& command) = 0;
94 virtual std::string
readCommand(
const std::string& command, std::string& logCommand) = 0;
101 virtual void write(
const std::string& command,
const std::string& response) = 0;
111 std::string filePath;
112 std::fstream *fileStream;
114 std::mutex streamMutex;
116 bool metadataRead =
false;
117 bool metadataWritten =
false;
121 std::size_t version = 1;
127 virtual void readMetadata(std::istream& metaStream);
133 virtual void writeMetadata(std::ostream& metaStream);
136 char MESSAGE_END =
'$';
142 Log(
const std::string& filePath);
148 Log(std::iostream& stream);
152 virtual inline const std::string&
getFilePath()
const override {
return filePath; }
153 virtual inline std::iostream&
getStream()
override {
return stream; }
154 virtual inline bool isEnd()
const override {
return !stream; }
156 virtual std::string
read()
override;
157 virtual std::string
read(std::string& logCommand)
override;
158 virtual std::string
readCommand(
const std::string& command)
override;
159 virtual std::string
readCommand(
const std::string& command, std::string& logCommand)
override;
161 virtual void write(
const std::string& command,
const std::string& response)
override;
179 virtual std::chrono::nanoseconds getLastCommandNanoseconds()
const = 0;
185 template<
typename Duration>
186 inline Duration
getLastCommandTimeAs()
const {
return std::chrono::duration_cast<Duration>(this->getLastCommandNanoseconds()); }
192 virtual void syncTime(
bool sync =
true) = 0;
198 template<
typename DurationT = std::chrono::milliseconds>
202 std::mutex streamMutex;
204 std::intmax_t num, den;
206 DurationT lastCommandTime;
208 DurationT firstReadTime = DurationT::zero();
209 DurationT firstWriteTime = DurationT::min();
212 virtual void readMetadata(std::istream& metaStream)
override;
213 virtual void writeMetadata(std::ostream& metaStream)
override;
227 return std::chrono::duration_cast<std::chrono::nanoseconds>(lastCommandTime);
236 virtual inline void syncTime(
bool sync =
true)
override { firstReadTime = (sync ? DurationT::max() : DurationT::zero()); }
238 virtual std::string
read(std::string& logCommand)
override;
239 virtual void write(
const std::string& command,
const std::string& response)
override;
247 template<
typename DurationT>
251 metaStream >> num >> den;
254 template<
typename DurationT>
258 metaStream <<
' ' << DurationT::period::num <<
' ' << DurationT::period::den;
261 template<
typename DurationT>
266 std::string response =
Log::read(logCommand);
268 std::string epochTime;
269 std::getline(stream, epochTime, MESSAGE_END);
270 std::istringstream epochStream(epochTime);
272 std::int64_t commandTimeCount;
273 epochStream >> commandTimeCount;
275 long double numRatio = num / DurationT::period::num;
276 long double denRation = DurationT::period::den / den;
277 lastCommandTime = DurationT(std::int64_t(std::round(commandTimeCount * numRatio * denRation)));
279 if(firstReadTime == DurationT::max()) firstReadTime = epoch<DurationT>();
282 DurationT elapsed = epoch<DurationT>() - firstReadTime;
284 while(elapsed < lastCommandTime)
286 std::this_thread::sleep_for(lastCommandTime - elapsed);
287 elapsed = epoch<DurationT>() - firstReadTime;
291 streamMutex.unlock();
296 template<
typename DurationT>
303 if(firstWriteTime == DurationT::min()) firstWriteTime = epoch<DurationT>();
304 stream << (epoch<DurationT>() - firstWriteTime).count() << MESSAGE_END;
306 streamMutex.unlock();
311 #endif // REGILO_LOG_HPP virtual void write(const std::string &command, const std::string &response) override
Write a command and response to the log.
Definition: log.hpp:297
virtual void syncTime(bool sync=true) override
Sync command times with real time.
Definition: log.hpp:236
virtual void readMetadata(std::istream &metaStream)
Read meta data from the log.
The TimedLog class is used to log all commands with their timestamp.
Definition: log.hpp:199
DurationT getLastCommandTime() const
Get the last command time (after reading).
Definition: log.hpp:234
Definition: controller.hpp:35
virtual const std::string & getFilePath() const override
Get the path of file if the log was created with a path otherwise the empty string.
Definition: log.hpp:152
virtual void write(const std::string &command, const std::string &response)=0
Write a command and response to the log.
virtual ~ILog()=default
Default destructor.
Duration getLastCommandTimeAs() const
Get the last command time (after reading).
Definition: log.hpp:186
virtual void writeMetadata(std::ostream &metaStream)
Write meta data to the log.
The Log class is a basic log with a simple read/write functionality.
Definition: log.hpp:108
DurationT Duration
The duration type for this log.
Definition: log.hpp:216
virtual std::string read() override
Read one command from the log.
std::iostream & stream
The underlying stream.
Definition: log.hpp:120
virtual void write(const std::string &command, const std::string &response) override
Write a command and response to the log.
The ILog interface has to be implemented in all Log classes.
Definition: log.hpp:42
virtual bool isEnd() const =0
Test if the stream is EOF.
virtual std::chrono::nanoseconds getLastCommandNanoseconds() const override
Get the last command time (after reading).
Definition: log.hpp:225
virtual bool isEnd() const override
Test if the stream is EOF.
Definition: log.hpp:154
The ITimedLog interface is implemented in TimedLog.
Definition: log.hpp:167
virtual std::iostream & getStream() override
Get the current underlying stream.
Definition: log.hpp:153
virtual std::string read()=0
Read one command from the log.
virtual void readMetadata(std::istream &metaStream) override
Read meta data from the log.
Definition: log.hpp:248
virtual std::string readCommand(const std::string &command)=0
Read specified command from the log (the others are skipped).
virtual void writeMetadata(std::ostream &metaStream) override
Write meta data to the log.
Definition: log.hpp:255
Log(const std::string &filePath)
Log constructor with logging to a file.
virtual std::iostream & getStream()=0
Get the current underlying stream.
virtual const std::string & getFilePath() const =0
Get the path of file if the log was created with a path otherwise the empty string.