James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 1 | // Copyright (c) FIRST and other WPILib contributors. |
| 2 | // Open Source Software; you can modify and/or share it under the terms of |
| 3 | // the WPILib BSD license file in the root directory of this project. |
| 4 | |
| 5 | #include "DataLogThread.h" |
| 6 | |
| 7 | #include <fmt/format.h> |
| 8 | |
| 9 | DataLogThread::~DataLogThread() { |
| 10 | if (m_thread.joinable()) { |
| 11 | m_active = false; |
| 12 | m_thread.join(); |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | void DataLogThread::ReadMain() { |
| 17 | for (auto record : m_reader) { |
| 18 | if (!m_active) { |
| 19 | break; |
| 20 | } |
| 21 | ++m_numRecords; |
| 22 | if (record.IsStart()) { |
| 23 | wpi::log::StartRecordData data; |
| 24 | if (record.GetStartData(&data)) { |
| 25 | std::scoped_lock lock{m_mutex}; |
| 26 | if (m_entries.find(data.entry) != m_entries.end()) { |
| 27 | fmt::print("...DUPLICATE entry ID, overriding\n"); |
| 28 | } |
| 29 | m_entries[data.entry] = data; |
| 30 | m_entryNames.emplace(data.name, data); |
| 31 | sigEntryAdded(data); |
| 32 | } else { |
| 33 | fmt::print("Start(INVALID)\n"); |
| 34 | } |
| 35 | } else if (record.IsFinish()) { |
| 36 | int entry; |
| 37 | if (record.GetFinishEntry(&entry)) { |
| 38 | std::scoped_lock lock{m_mutex}; |
| 39 | auto it = m_entries.find(entry); |
| 40 | if (it == m_entries.end()) { |
| 41 | fmt::print("...ID not found\n"); |
| 42 | } else { |
| 43 | m_entries.erase(it); |
| 44 | } |
| 45 | } else { |
| 46 | fmt::print("Finish(INVALID)\n"); |
| 47 | } |
| 48 | } else if (record.IsSetMetadata()) { |
| 49 | wpi::log::MetadataRecordData data; |
| 50 | if (record.GetSetMetadataData(&data)) { |
| 51 | std::scoped_lock lock{m_mutex}; |
| 52 | auto it = m_entries.find(data.entry); |
| 53 | if (it == m_entries.end()) { |
| 54 | fmt::print("...ID not found\n"); |
| 55 | } else { |
| 56 | it->second.metadata = data.metadata; |
| 57 | auto nameIt = m_entryNames.find(it->second.name); |
| 58 | if (nameIt != m_entryNames.end()) { |
| 59 | nameIt->second.metadata = data.metadata; |
| 60 | } |
| 61 | } |
| 62 | } else { |
| 63 | fmt::print("SetMetadata(INVALID)\n"); |
| 64 | } |
| 65 | } else if (record.IsControl()) { |
| 66 | fmt::print("Unrecognized control record\n"); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | sigDone(); |
| 71 | m_done = true; |
| 72 | } |