fsfw/timemanager/Stopwatch.cpp

62 lines
1.6 KiB
C++
Raw Normal View History

/**
* @file Stopwatch.cpp
*
* @date 08.04.2020
*/
#include <framework/timemanager/Stopwatch.h>
#include <framework/serviceinterface/ServiceInterfaceStream.h>
2020-04-08 19:05:21 +02:00
#include <iomanip>
2020-04-08 18:33:38 +02:00
Stopwatch::Stopwatch(bool displayOnDestruction,
2020-04-08 19:24:24 +02:00
StopwatchDisplayMode displayMode):
displayOnDestruction(displayOnDestruction) {
2020-04-08 18:33:38 +02:00
// Measures start time on initialization.
Clock::getUptime(&startTime);
}
void Stopwatch::start() {
startTime = Clock::getUptime();
}
2020-04-08 19:24:24 +02:00
millis_t Stopwatch::stop() {
2020-04-08 18:27:18 +02:00
stopInternal();
return elapsedTime.tv_sec * 1000 + elapsedTime.tv_usec / 1000;
}
2020-04-08 19:24:24 +02:00
seconds_t Stopwatch::stopSeconds() {
2020-04-08 18:27:18 +02:00
stopInternal();
2020-04-08 19:24:24 +02:00
return timevalOperations::toDouble(elapsedTime);
}
void Stopwatch::display() {
2020-04-08 19:24:24 +02:00
if(displayMode == StopwatchDisplayMode::MILLIS) {
info << "Stopwatch: Operation took " << elapsedTime.tv_sec * 1000 +
elapsedTime.tv_usec * 1000 << " milliseconds";
}
2020-04-08 19:24:24 +02:00
else if(displayMode == StopwatchDisplayMode::SECONDS) {
info <<"Stopwatch: Operation took " << std::setprecision(4)
<< std::fixed << timevalOperations::toDouble(elapsedTime)
<< " seconds";
}
}
Stopwatch::~Stopwatch() {
if(displayOnDestruction) {
stopInternal();
display();
}
}
2020-04-08 18:33:38 +02:00
void Stopwatch::setDisplayMode(StopwatchDisplayMode displayMode) {
this->displayMode = displayMode;
}
StopwatchDisplayMode Stopwatch::getDisplayMode() const {
return displayMode;
}
2020-04-08 18:27:18 +02:00
void Stopwatch::stopInternal() {
elapsedTime = Clock::getUptime() - startTime;
}