1#include <framework/global.h>
2#include <framework/core/clock.h>
3#include <framework/core/resourcemanager.h>
4
5#include "packet_recorder.h"
6
7PacketRecorder::PacketRecorder(const std::string& file)
8{
9 m_start = g_clock.millis();
10#ifdef ANDROID
11 g_resources.makeDir("records");
12 m_stream = std::ofstream(std::string("records/") + file);
13#else
14 std::error_code ec;
15 std::filesystem::create_directory("records", ec);
16 m_stream = std::ofstream(std::filesystem::path("records") / file);
17#endif
18}
19
20PacketRecorder::~PacketRecorder()
21{
22
23}
24
25void PacketRecorder::addInputPacket(const InputMessagePtr& packet)
26{
27 m_stream << "< " << (g_clock.millis() - m_start) << " ";
28 for (auto& buffer : packet->getBodyBuffer()) {
29 m_stream << std::setfill('0') << std::setw(2) << std::hex << (uint16_t)(uint8_t)buffer;
30 }
31 m_stream << std::dec << "\n";
32}
33
34void PacketRecorder::addOutputPacket(const OutputMessagePtr& packet)
35{
36 if (m_firstOutput) {
37 // skip packet with login and password
38 m_firstOutput = false;
39 return;
40 }
41
42 m_stream << "> " << (g_clock.millis() - m_start) << " ";
43 for (auto& buffer : packet->getBuffer()) {
44 m_stream << std::setfill('0') << std::setw(2) << std::hex << (uint16_t)(uint8_t)buffer;
45 }
46 m_stream << std::dec << "\n";
47}
48