#ifndef DEVICEUSBLOG_H #define DEVICEUSBLOG_H #include "../../VNA_embedded/Application/Communication/Protocol.hpp" #include "savable.h" #include #include #include #include #include class DeviceUSBLog : public QObject, public Savable { Q_OBJECT public: static DeviceUSBLog& getInstance() { static DeviceUSBLog instance; return instance; } DeviceUSBLog(const DeviceUSBLog&) = delete; virtual ~DeviceUSBLog(); void reset(); void addPacket(Protocol::PacketInfo &p, QString serial = ""); void addInvalidBytes(const uint8_t *bytes, uint16_t len, QString serial = ""); virtual nlohmann::json toJSON() override; virtual void fromJSON(nlohmann::json j) override; class LogEntry : public Savable { public: LogEntry() : type(Type::InvalidBytes), timestamp(QDateTime()), serial(""), p(nullptr) {} ~LogEntry() { delete p; } LogEntry(const LogEntry &e); enum class Type { Packet, InvalidBytes, }; Type type; QDateTime timestamp; QString serial; std::vector bytes; Protocol::PacketInfo *p; unsigned int storageSize() const { unsigned long size = sizeof(type) + sizeof(timestamp) + serial.size(); switch(type) { case Type::InvalidBytes: size += bytes.size(); break; case Type::Packet: size += sizeof(Protocol::PacketInfo); break; } return size; } virtual nlohmann::json toJSON() override; virtual void fromJSON(nlohmann::json j) override; }; LogEntry getEntry(unsigned int index); unsigned long getUsedStorageSize() const; unsigned long getMaxStorageSize() const; signals: void entryAdded(const LogEntry &e); private: DeviceUSBLog(); void addEntry(const LogEntry &e); unsigned long maxStorageSize; unsigned long usedStorageSize; std::deque entries; std::mutex access; }; #endif // DEVICEUSBLOG_H