fsfw/timemanager/Stopwatch.cpp

61 lines
1.6 KiB
C++
Raw Normal View History

2020-09-18 12:27:40 +02:00
#include "../timemanager/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) {
sif::info << "Stopwatch: Operation took " << (elapsedTime.tv_sec * 1000 +
elapsedTime.tv_usec / 1000) << " milliseconds" << std::endl;
}
else if(displayMode == StopwatchDisplayMode::SECONDS) {
sif::info <<"Stopwatch: Operation took " << std::setprecision(3)
<< std::fixed << timevalOperations::toDouble(elapsedTime)
<< " seconds" << std::endl;
}
}
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;
}