[wpilibc] Add ScopedTracer class (#2724)

This makes it easier for C++ teams to log a certain piece of code using RAII.
This commit is contained in:
Prateek Machiraju
2020-09-27 03:10:11 -04:00
committed by GitHub
parent 73950b9857
commit 17d3d2f754
3 changed files with 103 additions and 0 deletions

View File

@@ -0,0 +1,48 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2020 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#pragma once
#include <string>
#include <wpi/StringRef.h>
#include <wpi/Twine.h>
#include "frc/Tracer.h"
namespace wpi {
class raw_ostream;
} // namespace wpi
namespace frc {
/**
* A class for keeping track of how much time it takes for different
* parts of code to execute. This class uses RAII, meaning you simply
* need to create an instance at the top of the block you are timing. After the
* block finishes execution (i.e. when the ScopedTracer instance gets
* destroyed), the epoch is printed to the provided raw_ostream.
*/
class ScopedTracer {
public:
/**
* Constructs a ScopedTracer instance.
*
* @param name The name of the epoch.
* @param os A reference to the raw_ostream to print data to.
*/
ScopedTracer(wpi::Twine name, wpi::raw_ostream& os);
~ScopedTracer();
ScopedTracer(const ScopedTracer&) = delete;
ScopedTracer& operator=(const ScopedTracer&) = delete;
private:
Tracer m_tracer;
std::string m_name;
wpi::raw_ostream& m_os;
};
} // namespace frc