fsfw/timemanager/Stopwatch.cpp

65 lines
1.7 KiB
C++
Raw Normal View History

2020-09-18 13:03:48 +02:00
#include "Stopwatch.h"
2020-08-13 20:53:35 +02:00
#include "../serviceinterface/ServiceInterfaceStream.h"
#include <iomanip>
Stopwatch::Stopwatch(bool displayOnDestruction,
StopwatchDisplayMode displayMode): displayOnDestruction(
displayOnDestruction), displayMode(displayMode) {
// Measures start time on initialization.
2020-09-18 12:27:40 +02:00
Clock::getUptime(&startTime);
2020-08-13 20:53:35 +02:00
}
void Stopwatch::start() {
2020-09-18 12:27:40 +02:00
Clock::getUptime(&startTime);
2020-08-13 20:53:35 +02:00
}
2020-09-18 12:27:40 +02:00
dur_millis_t Stopwatch::stop(bool display) {
2020-08-13 20:53:35 +02:00
stopInternal();
2020-09-18 12:27:40 +02:00
if(display) {
this->display();
}
2020-08-13 20:53:35 +02:00
return elapsedTime.tv_sec * 1000 + elapsedTime.tv_usec / 1000;
}
2020-09-18 12:27:40 +02:00
double Stopwatch::stopSeconds() {
2020-08-13 20:53:35 +02:00
stopInternal();
return timevalOperations::toDouble(elapsedTime);
}
void Stopwatch::display() {
if(displayMode == StopwatchDisplayMode::MILLIS) {
#if CPP_OSTREAM_ENABLED == 1
2020-08-13 20:53:35 +02:00
sif::info << "Stopwatch: Operation took " << (elapsedTime.tv_sec * 1000 +
elapsedTime.tv_usec / 1000) << " milliseconds" << std::endl;
#endif
2020-08-13 20:53:35 +02:00
}
else if(displayMode == StopwatchDisplayMode::SECONDS) {
#if CPP_OSTREAM_ENABLED == 1
2020-08-13 20:53:35 +02:00
sif::info <<"Stopwatch: Operation took " << std::setprecision(3)
<< std::fixed << timevalOperations::toDouble(elapsedTime)
<< " seconds" << std::endl;
#endif
2020-08-13 20:53:35 +02:00
}
}
Stopwatch::~Stopwatch() {
if(displayOnDestruction) {
stopInternal();
display();
}
}
void Stopwatch::setDisplayMode(StopwatchDisplayMode displayMode) {
this->displayMode = displayMode;
}
StopwatchDisplayMode Stopwatch::getDisplayMode() const {
return displayMode;
}
void Stopwatch::stopInternal() {
timeval endTime;
2020-09-18 12:27:40 +02:00
Clock::getUptime(&endTime);
2020-08-13 20:53:35 +02:00
elapsedTime = endTime - startTime;
}