| 31 | static const std::string BOOKMARK_KEY = "bookmark"; |
| 32 | |
| 33 | Bookmark::Bookmark(const std::wstring& channel, const std::wstring& query, const std::string& bookmarkRootDir, const utils::Identifier& uuid, bool processOldEvents, std::shared_ptr<core::CoreComponentStateManager> state_manager, std::shared_ptr<logging::Logger> logger) |
| 34 | : logger_(logger) |
| 35 | , state_manager_(state_manager) { |
| 36 | std::unordered_map<std::string, std::string> state_map; |
| 37 | if (state_manager_->get(state_map) && state_map.count(BOOKMARK_KEY) == 1U) { |
| 38 | bookmarkXml_ = wel::to_wstring(state_map[BOOKMARK_KEY].c_str()); |
| 39 | } else if (!bookmarkRootDir.empty()) { |
| 40 | filePath_ = utils::file::FileUtils::concat_path( |
| 41 | utils::file::FileUtils::concat_path( |
| 42 | utils::file::FileUtils::concat_path(bookmarkRootDir, "uuid"), uuid.to_string()), "Bookmark.txt"); |
| 43 | |
| 44 | std::wstring bookmarkXml; |
| 45 | if (getBookmarkXmlFromFile(bookmarkXml)) { |
| 46 | if (saveBookmarkXml(bookmarkXml) && state_manager_->persist()) { |
| 47 | logger_->log_info("State migration successful"); |
| 48 | rename(filePath_.c_str(), (filePath_ + "-migrated").c_str()); |
| 49 | } else { |
| 50 | logger_->log_warn("Could not migrate state from specified State Directory %s", bookmarkRootDir); |
| 51 | } |
| 52 | } |
| 53 | } else { |
| 54 | logger_->log_info("Found no stored state"); |
| 55 | } |
| 56 | |
| 57 | if (!bookmarkXml_.empty()) { |
| 58 | if (hBookmark_ = unique_evt_handle{ EvtCreateBookmark(bookmarkXml_.c_str()) }) { |
| 59 | ok_ = true; |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | LOG_LAST_ERROR(EvtCreateBookmark); |
| 64 | |
| 65 | bookmarkXml_.clear(); |
| 66 | state_map.erase(BOOKMARK_KEY); |
| 67 | state_manager_->set(state_map); |
| 68 | } |
| 69 | |
| 70 | if (!(hBookmark_ = unique_evt_handle{ EvtCreateBookmark(0) })) { |
| 71 | LOG_LAST_ERROR(EvtCreateBookmark); |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | const auto hEventResults = unique_evt_handle{ EvtQuery(0, channel.c_str(), query.c_str(), EvtQueryChannelPath) }; |
| 76 | if (!hEventResults) { |
| 77 | LOG_LAST_ERROR(EvtQuery); |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | if (!EvtSeek(hEventResults.get(), 0, 0, 0, processOldEvents? EvtSeekRelativeToFirst : EvtSeekRelativeToLast)) { |
| 82 | LOG_LAST_ERROR(EvtSeek); |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | const unique_evt_handle hEvent = [this,&hEventResults] { |
| 87 | DWORD dwReturned{}; |
| 88 | EVT_HANDLE hEvent{ nullptr }; |
| 89 | if (!EvtNext(hEventResults.get(), 1, &hEvent, INFINITE, 0, &dwReturned)) { |
| 90 | LOG_LAST_ERROR(EvtNext); |
nothing calls this directly
no test coverage detected