10 return std::chrono::duration_cast<std::chrono::microseconds>(ts).count();
24 std::atomic_bool
loop =
true;
30 if(filepath.filename().extension() !=
".btlog")
32 throw RuntimeError(
"FileLogger2: the file extension must be [.btlog]");
40 if(!
_p->file_stream.is_open())
42 throw RuntimeError(
"problem opening file in FileLogger2");
45 _p->file_stream <<
"BTCPP4-FileLogger2";
47 const uint8_t protocol = 1;
49 _p->file_stream << protocol;
55 flatbuffers::WriteScalar(write_buffer,
static_cast<int32_t
>(xml.size()));
56 _p->file_stream.write(write_buffer, 4);
59 _p->file_stream.write(xml.data(),
int(xml.size()));
61 _p->first_timestamp = std::chrono::system_clock::now().time_since_epoch();
64 int64_t timestamp_usec =
ToUsec(
_p->first_timestamp);
65 flatbuffers::WriteScalar(write_buffer, timestamp_usec);
66 _p->file_stream.write(write_buffer, 8);
74 _p->queue_cv.notify_one();
75 _p->writer_thread.join();
76 _p->file_stream.close();
85 trans.
status =
static_cast<uint64_t
>(status);
87 std::scoped_lock lock(
_p->queue_mutex);
88 _p->transitions_queue.push_back(trans);
90 _p->queue_cv.notify_one();
95 _p->file_stream.flush();
101 std::deque<Transition> transitions;
107 std::unique_lock lock(
_p->queue_mutex);
108 _p->queue_cv.wait_for(lock, std::chrono::milliseconds(10), [
this]() {
109 return !
_p->transitions_queue.empty() &&
_p->loop;
114 while(!transitions.empty())
116 const auto trans = transitions.front();
117 std::array<char, 9> write_buffer;
118 std::memcpy(write_buffer.data(), &trans.timestamp_usec, 6);
119 std::memcpy(write_buffer.data() + 6, &trans.node_uid, 2);
120 std::memcpy(write_buffer.data() + 8, &trans.status, 1);
122 _p->file_stream.write(write_buffer.data(), 9);
123 transitions.pop_front();
125 _p->file_stream.flush();