#ifndef DEBUG_DATA_SAVER_H #define DEBUG_DATA_SAVER_H #include #include #include #include #include #include #include #include #include #include #include "VZNL_Types.h" class DebugDataSaver { public: explicit DebugDataSaver(int maxThreads = 4); ~DebugDataSaver(); void SaveAsync(const std::string& basePath, const std::string& timestamp, const std::vector>& data, const std::string& deviceName = ""); void Stop(); void SetMaxStorageSize(int64_t bytes) { m_maxStorageSize = bytes; } void SetMaxQueueSize(int maxTasks); void PrepareStorageCache(const std::string& basePath); private: struct SaveTask { std::string basePath; std::string filePath; std::string deviceName; std::vector> data; }; struct StorageFileInfo { std::string path; int64_t size = 0; int64_t lastModifiedMSecs = 0; }; struct StorageCache { bool initialized = false; int64_t totalSize = 0; std::vector files; }; void WorkerThread(); static std::vector> DeepCopyData( const std::vector>& src); static void FreeData(std::vector>& data); std::string GenerateSavePath(const std::string& basePath, const std::string& timestamp, const std::string& deviceName = ""); void EnsureStorageLimit(const std::string& basePath); void EnsureStorageLimitLocked(const std::string& basePath, StorageCache& cache); void BuildStorageCacheLocked(const std::string& basePath, StorageCache& cache); void AddFileToCacheLocked(StorageCache& cache, const StorageFileInfo& fileInfo); void RecordSavedFile(const std::string& basePath, const std::string& filePath); static bool RemoveFileFromCache(StorageCache& cache, const std::string& filePath); static bool IsOlderFile(const StorageFileInfo& lhs, const StorageFileInfo& rhs); static int64_t ComputeDirSize(const std::string& rootPath); static std::string FindOldestFile(const std::string& rootPath); int m_maxThreads; std::vector m_workers; std::queue m_taskQueue; std::mutex m_queueMutex; std::condition_variable m_queueCondition; std::atomic m_running; std::atomic m_maxQueueSize{10}; std::mutex m_diskSpaceMutex; std::atomic m_maxStorageSize{10LL * 1024 * 1024 * 1024}; std::map m_storageCaches; }; #endif // DEBUG_DATA_SAVER_H