src.dualinventive.com/jjacobs/dinetrpcll-sniffer/libdipp/include/di/Tracer.h

80 lines
1.5 KiB
C++

/**
* @file include/di/Tracer.h
* @brief Tracer for logging code duration
* @copyright 2018 Dual Inventive Technology Centre B.V.
*
* Class for logging code duration
*/
#ifndef INCLUDE_DI_TRACER_H_
#define INCLUDE_DI_TRACER_H_
#include <string>
#include <map>
#include <vector>
#include <di/Timer.h>
#include <di/Time.h>
#include <mutex>
#include <iostream>
#include <fstream>
namespace Di {
/**
* @class Tracer
*
* Class for logging code duration
*/
class Tracer {
public:
/**
* Constructor
* @param path the path to write the csv to
*/
explicit Tracer(const std::string &path);
/**
* Start the trace
*
* @param name the name of the trace
*/
void start(const std::string name);
/**
* Stop the trace
*
* @param name the name of the trace
*/
void stop(const std::string &name);
void trigger(const std::string &name);
private:
struct __trace {
__trace(const std::string &initName, bool initTick) :
name(initName),
minDuration(std::chrono::nanoseconds::max()),
maxDuration(std::chrono::nanoseconds::min()),
start(std::chrono::high_resolution_clock::now()),
numberOfTicks(0) {
if (initTick) {
numberOfTicks = 1;
}
}
std::string name;
std::chrono::nanoseconds minDuration;
std::chrono::nanoseconds maxDuration;
std::chrono::time_point<std::chrono::high_resolution_clock> start;
uint64_t numberOfTicks;
};
void __write();
std::shared_ptr<std::map<std::string, __trace>> __traces;
Di::Timer __timer;
std::mutex __mut;
std::ofstream __fout;
};
} // namespace Di
#endif // INCLUDE_DI_TRACER_H_