You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
1.9 KiB
C++

/**
*
* \section COPYRIGHT
*
* Copyright 2013-2021 Software Radio Systems Limited
*
* By using this file, you agree to the terms and conditions set
* forth in the LICENSE file which can be found at the top level of
* the distribution.
*
*/
/******************************************************************************
* File: trace.h
* Description:
* Reference:
*****************************************************************************/
#ifndef SRSRAN_TRACE_H
#define SRSRAN_TRACE_H
#include <stdio.h>
#include <string>
#include <sys/time.h>
#include <vector>
namespace srsran {
template <class elemType>
class trace
{
public:
trace(uint32_t nof_elems_) : tti(nof_elems_), data(nof_elems_)
{
rpm = 0;
nof_elems = nof_elems_;
wrapped = false;
};
void push_cur_time_us(uint32_t cur_tti)
{
struct timeval t;
gettimeofday(&t, NULL);
elemType us = t.tv_sec * 1e6 + t.tv_usec;
push(cur_tti, us);
}
void push(uint32_t value_tti, elemType value)
{
tti[rpm] = value_tti;
data[rpm] = value;
rpm++;
if (rpm >= nof_elems) {
rpm = 0;
wrapped = true;
}
}
bool writeToBinary(std::string filename)
{
FILE* f = fopen(filename.c_str(), "w");
if (f != NULL) {
uint32_t st = wrapped ? (rpm + 1) : 0;
do {
writeToBinaryValue(f, st++);
if (st >= nof_elems) {
st = 0;
}
} while (st != rpm);
fclose(f);
return true;
} else {
perror("fopen");
return false;
}
}
private:
std::vector<uint32_t> tti;
std::vector<elemType> data;
uint32_t rpm;
uint32_t nof_elems;
bool wrapped;
void writeToBinaryValue(FILE* f, uint32_t idx)
{
fwrite(&tti[idx], 1, sizeof(uint32_t), f);
fwrite(&data[idx], 1, sizeof(elemType), f);
}
};
} // namespace srsran
#endif // SRSRAN_TRACE_H