#include <iostream>
#include <unistd.h>
#include <sys/resource.h>
#include "DataLogger.h"
#include "../data/DataFormat.h"
char *DataLogger::LoggerDie = (char *)0xdeaddead;
DataLogger::DataLogger(DAQpp::RunManager *r,
int buffer_size,
bool compress, unsigned long mx_size)
: DAQpp::FileIOManager(compress, mx_size),
run_manager(r), fifo(buffer_size),
stoplogging(false) , plogger(this)
{
}
DataLogger::~DataLogger()
{}
void DataLogger::start_logging()
{
stoplogging = false;
fifo.reset();
plogger.start();
}
void DataLogger::stop_logging()
{
if (plogger.is_running())
{
stoplogging = true;
// We write this to wake-up the fifo
std::cout << "Stopping logger" << std::endl;
fifo.write(LoggerDie, sizeof(unsigned int));
plogger.stop();
}
stoplogging = false;
}
void DataLogger::log_data()
{
static int evtn = -1;
unsigned int *data = 0, *zbuf = 0;
size_t sz, bfsiz;
bfsiz = 1;
zbuf = (unsigned int *)realloc(zbuf, bfsiz * sizeof(unsigned int));
data = (unsigned int *)realloc(data, bfsiz * sizeof(unsigned int));
do
{
// get the size of the buffer
fifo.read((char *)data, sizeof(unsigned int));
if ( stoplogging )
break;
plogger.test_cancel();
// paranoia
if ( (data[0]&0xffff0000) != DataHeader)
continue;
sz = data[0] & 0xfff;
// realloc the arrays
if ( bfsiz < sz )
{
zbuf = (unsigned int *)realloc(zbuf, sz * sizeof(unsigned int));
data = (unsigned int *)realloc(data, sz * sizeof(unsigned int));
bfsiz = sz;
}
// Get the real data
fifo.read((char *)(data + 1), (sz - 1)*sizeof(unsigned int));
plogger.test_cancel();
if ( sz != data[sz-1] )
continue;
write((char *)data, sz*sizeof(unsigned int));
if ( evtn != run_manager->get_event_number() )
{
run_manager->get_status().new_written();
evtn = run_manager->get_event_number();
}
}
while (1);
/// close the file
close();
return;
}