#include <iostream>
#include <unistd.h>
#include <DAQ++/logger.h>
#include "DataLogger.h"
DataLogger::DataLogger(DAQpp::RunManager *r,
int buffer_size,
bool compress, unsigned long mx_size)
: DAQpp::FileIOManager(compress, mx_size),
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() )
{
unsigned long dummy = 0xdeaddead;
stoplogging = true;
// we write this to wake-up the fifo
fifo.write((char *)&dummy, sizeof(unsigned long));
plogger.stop();
}
stoplogging = false;
}
void DataLogger::log_data()
{
unsigned long *data = 0, *zbuf = 0;
size_t sz, bfsiz;
bfsiz = 1;
zbuf = (unsigned long *)realloc(zbuf, bfsiz * sizeof(unsigned long));
data = (unsigned long *)realloc(data, bfsiz * sizeof(unsigned long));
do
{
// get the size of the buffer
fifo.read((char *)data, sizeof(unsigned long));
if ( stoplogging )
break;
plogger.test_cancel();
// paranoia
if ( (data[0]&0xffff0000) != 0xcafe0000)
{
DAQpp::ostream os;
os << DAQpp::loglevel(DAQpp::Log::critical) << "bad event" << std::endl;
continue;
}
sz = data[0] & 0xfff;
// realloc the arrays
if ( bfsiz < sz )
{
zbuf = (unsigned long *)realloc(zbuf, sz * sizeof(unsigned long));
data = (unsigned long *)realloc(data, sz * sizeof(unsigned long));
bfsiz = sz;
}
// Get the real data
fifo.read((char *)(data + 1), (sz - 1)*sizeof(unsigned long));
if ( sz != data[sz-1] )
{
DAQpp::ostream os;
os << DAQpp::loglevel(DAQpp::Log::critical) << "data corrupted" << std::endl;
continue;
}
write((char *)data, sz*sizeof(unsigned long));
}
while (1);
// close the file
close();
return;
}