18 #include "primarydriver.h"
25 #define OFSMODE std::ios::out | std::ios::app | std::ios::ate
27 XRawStream::XRawStream(
const char *name,
bool runtime,
const shared_ptr<XDriverList> &driverlist)
28 :
XNode(name, runtime),
29 m_drivers(driverlist),
33 XRawStream::~XRawStream() {
37 XRawStreamRecorder::XRawStreamRecorder(
const char *name,
bool runtime,
const shared_ptr<XDriverList> &driverlist)
39 m_recording(create<
XBoolNode>(
"Recording", true)) {
42 tr[ *recording()] =
false;
43 m_lsnOnOpen = tr[ *filename()].onValueChanged().connectWeakly(
44 shared_from_this(), &XRawStreamRecorder::onOpen);
45 m_lsnOnFlush = tr[ *recording()].onValueChanged().connectWeakly(
46 shared_from_this(), &XRawStreamRecorder::onFlush);
49 m_lsnOnCatch = tr[ *m_drivers].onCatch().connect( *
this, &XRawStreamRecorder::onCatch);
50 m_lsnOnRelease = tr[ *m_drivers].onRelease().connect( *
this, &XRawStreamRecorder::onRelease);
55 auto driver = static_pointer_cast<
XDriver>(e.caught);
58 tr[ *driver].onRecord().connect(m_lsnOnRecord);
60 m_lsnOnRecord = tr[ *driver].onRecord().connectWeakly(
61 shared_from_this(), &XRawStreamRecorder::onRecord);
66 auto driver = static_pointer_cast<
XDriver>(e.released);
68 tr[ *driver].onRecord().disconnect(m_lsnOnRecord);
74 m_pGFD = gzopen(QString(( **filename())->to_str()).toLocal8Bit().data(),
"wb");
81 gzflush(static_cast<gzFile>(
m_pGFD), Z_FULL_FLUSH);
87 if( ***recording() &&
m_pGFD) {
91 uint32_t size = rawdata.size();
100 + driver->getName().size()
105 header.
push((uint32_t)allsize);
106 header.push((int32_t)shot[ *driver].time().sec());
107 header.push((int32_t)shot[ *driver].time().usec());
108 assert(header.size() == headersize);
111 gzwrite(static_cast<gzFile>(
m_pGFD), &header[0], header.size());
112 gzprintf(static_cast<gzFile>(
m_pGFD),
"%s", (
const char*)driver->getName().c_str());
113 gzputc(static_cast<gzFile>(
m_pGFD),
'\0');
114 gzputc(static_cast<gzFile>(
m_pGFD),
'\0');
115 gzwrite(static_cast<gzFile>(
m_pGFD), &rawdata[0], size);
117 header.push((uint32_t)allsize);
118 gzwrite(static_cast<gzFile>(
m_pGFD), &header[0], header.size());
119 m_filemutex.unlock();
126 XTextWriter::XTextWriter(
const char *name,
bool runtime,
127 const shared_ptr<XDriverList> &driverlist,
const shared_ptr<XScalarEntryList> &entrylist)
128 :
XNode(name, runtime),
129 m_drivers(driverlist),
130 m_entries(entrylist),
133 m_recording(create<
XBoolNode>(
"Recording", true)),
134 m_logFilename(create<
XStringNode>(
"LogFilename", false)),
135 m_logRecording(create<
XBoolNode>(
"LogRecording", false)),
136 m_logEvery(create<
XUIntNode>(
"LogEvery", false)) {
139 tr[ *recording()] =
false;
140 tr[ *lastLine()].setUIEnabled(
false);
141 tr[ *logRecording()] =
false;
142 tr[ *logEvery()] = 300;
143 m_lsnOnFilenameChanged = tr[ *filename()].onValueChanged().connectWeakly(
144 shared_from_this(), &XTextWriter::onFilenameChanged);
145 m_lsnOnLogFilenameChanged = tr[ *logFilename()].onValueChanged().connectWeakly(
146 shared_from_this(), &XTextWriter::onLogFilenameChanged);
149 m_lsnOnCatch = tr[ *m_drivers].onCatch().connect( *
this, &XTextWriter::onCatch);
150 m_lsnOnRelease = tr[ *m_drivers].onRelease().connect( *
this, &XTextWriter::onRelease);
155 auto driver = static_pointer_cast<
XDriver>(e.caught);
158 tr[ *driver].onRecord().connect(m_lsnOnRecord);
160 m_lsnOnRecord = tr[ *driver].onRecord().connectWeakly(
161 shared_from_this(), &XTextWriter::onRecord);
166 auto driver = static_pointer_cast<
XDriver>(e.released);
168 tr[ *driver].onRecord().disconnect(m_lsnOnRecord);
174 if(m_stream.good()) {
175 m_stream << shot[ *lastLine()].to_str()
183 XTime logtime = XTime::now();
185 bool record_log = shot_this[ *logRecording()] &&
186 (logtime - m_loggedTime > shot_this[ *logEvery()]);
187 if(shot_this[ *recording()] || record_log) {
188 if(shot[ *driver].time()) {
191 if( !shot_entries.size())
196 m_loggedTime = logtime;
197 for(
auto it = entries_list.begin(); it != entries_list.end(); it++) {
199 logline.append(shot_entries[ *entry->value()].to_str() + KAME_DATAFILE_DELIMITER);
201 logline.append(m_loggedTime.getTimeFmtStr(
"%Y/%m/%d %H:%M:%S"));
203 if( !shot_this[ *recording()])
206 bool triggered =
false;
207 for(
auto it = entries_list.begin(); it != entries_list.end(); it++) {
209 if( !shot_entries[ *entry->store()])
continue;
210 shared_ptr<XDriver> d(entry->driver());
212 if((d.get() == driver) && shot[ *entry].isTriggered()) {
221 for(
auto it = entries_list.begin(); it != entries_list.end(); it++) {
223 if( !shot_entries[ *entry->store()])
continue;
224 entry->storeValue(tr_entries);
225 buf.append(shot_entries[ *entry->value()].to_str() + KAME_DATAFILE_DELIMITER);
227 buf.append(shot[ *driver].time().getTimeFmtStr(
"%Y/%m/%d %H:%M:%S"));
228 if(tr_entries.commit()) {
229 trans( *lastLine()) = buf;
236 if(m_logStream.good()) {
237 m_logStream << logline
246 if(m_stream.is_open()) m_stream.close();
248 m_stream.open((
const char*)QString(shot[ *filename()].to_str()).toLocal8Bit().data(), OFSMODE);
250 if(m_stream.good()) {
252 m_lsnOnFlush = tr[ *recording()].onValueChanged().connectWeakly(
253 shared_from_this(), &XTextWriter::onFlush);
254 m_lsnOnLastLineChanged = tr[ *lastLine()].onValueChanged().connectWeakly(
255 shared_from_this(), &XTextWriter::onLastLineChanged);
257 lastLine()->setUIEnabled(
true);
262 if(shot_entries.size()) {
264 for(
auto it = entries_list.begin(); it != entries_list.end(); it++) {
266 if( !shot_entries[ *entry->store()])
continue;
267 buf.append(entry->getLabel());
268 buf.append(KAME_DATAFILE_DELIMITER);
271 buf.append(
"Date" KAME_DATAFILE_DELIMITER
"Time" KAME_DATAFILE_DELIMITER
"msec");
272 trans( *lastLine()) = buf;
275 m_lsnOnFlush.reset();
276 m_lsnOnLastLineChanged.reset();
277 lastLine()->setUIEnabled(
false);
278 gErrPrint(i18n(
"Failed to open file."));
283 lastLine()->setUIEnabled( ***recording());
284 if( !***recording()) {
294 if(m_logStream.is_open()) m_logStream.close();
296 m_logStream.open((
const char*)QString(shot[ *logFilename()].to_str()).toLocal8Bit().data(), OFSMODE);
298 if(m_logStream.good()) {
302 if(shot_entries.size()) {
304 for(
auto it = entries_list.begin(); it != entries_list.end(); it++) {
306 buf.append(entry->getLabel());
307 buf.append(KAME_DATAFILE_DELIMITER);
310 buf.append(
"Date" KAME_DATAFILE_DELIMITER
"Time" KAME_DATAFILE_DELIMITER
"msec");
315 gWarnPrint(i18n(
"All-entry logger: Failed to open file."));