bakaid commented on a change in pull request #681: MINIFICPP-1085
URL: https://github.com/apache/nifi-minifi-cpp/pull/681#discussion_r346427599
 
 

 ##########
 File path: extensions/windows-event-log/Bookmark.cpp
 ##########
 @@ -0,0 +1,217 @@
+#include "Bookmark.h"
+
+#include <direct.h>
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace processors {
+
+Bookmark::Bookmark(const std::string& uuid, std::shared_ptr<logging::Logger> 
logger)
+  :logger_(logger) {
+  if (createUUIDDir(uuid, filePath_)) {
+    filePath_ += "Bookmark.txt";
+  }
+
+  if (!filePath_.empty()) {
+    if (!getBookmarkXmlFromFile()) {
+      return;
+    }
+  }
+
+  if (bookmarkXml_.empty()) {
+    if (!(hBookmark_ = EvtCreateBookmark(0))) {
+      logger_->log_error("!EvtCreateBookmark error: %d", GetLastError());
+      return;
+    }
+  } else {
+    if (!(hBookmark_ = EvtCreateBookmark(bookmarkXml_.c_str()))) {
+      logger_->log_error("!EvtCreateBookmark error: %d bookmarkXml_ '%s'", 
GetLastError(), bookmarkXml_.c_str());
+      return;
+    }
+  }
+
+  ok_ = true;
+}
+
+Bookmark::~Bookmark() {
+  if (file_.is_open()) {
+    file_.close();
+  }
+
+  if (hBookmark_) {
+    EvtClose(hBookmark_);
+  }
+}
+
+Bookmark::operator bool() {
+  return ok_;
+}
+  
+bool Bookmark::hasBookmarkXml() {
+  return !bookmarkXml_.empty();
+}
+
+EVT_HANDLE Bookmark::bookmarkHandle() {
+  return hBookmark_;
+}
+
+bool Bookmark::saveBookmark(EVT_HANDLE hEvent)
+{
+  if (!EvtUpdateBookmark(hBookmark_, hEvent)) {
+    logger_->log_error("!EvtUpdateBookmark error: %d.", GetLastError());
+    return false;
+  }
+
+  // Render the bookmark as an XML string that can be persisted.
+  DWORD bufferSize{};
+  DWORD bufferUsed{};
+  DWORD propertyCount{};
+  if (!EvtRender(0, hBookmark_, EvtRenderBookmark, bufferSize, 0, &bufferUsed, 
&propertyCount)) {
+    DWORD status = ERROR_SUCCESS;
+    if (ERROR_INSUFFICIENT_BUFFER == (status = GetLastError())) {
+      bufferSize = bufferUsed;
+
+      std::vector<wchar_t> buf(bufferSize / 2 + 1);
+
+      if (!EvtRender(0, hBookmark_, EvtRenderBookmark, bufferSize, &buf[0], 
&bufferUsed, &propertyCount)) {
+        logger_->log_error("!EvtRender error: %d.", GetLastError());
+        return false;
+      }
+
+      file_.seekp(std::ios::beg);
+
+      // Write new bookmark over old and in the end write '!'. Then new 
bookmark is read until '!'.
 
 Review comment:
   Why do we need this? Why can't we just truncate the file and write the new 
bookmark?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to