am-c-p-p commented on a change in pull request #681: MINIFICPP-1085
URL: https://github.com/apache/nifi-minifi-cpp/pull/681#discussion_r346933936
 
 

 ##########
 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 '!'.
+      file_ << &buf[0] << L'!';
+
+      file_.flush();
+    }
+    else if (ERROR_SUCCESS != (status = GetLastError())) {
+      logger_->log_error("!EvtRender error: %d.", GetLastError());
+      return false;
+    }
+  }
+
+  return true;
+}
+
+bool Bookmark::createEmptyBookmarkXmlFile() {
+  if (file_.is_open()) {
+    file_.close();
+  }
+
+  file_.open(filePath_, std::ios::out);
+
+  auto ret = file_.is_open();
+  if (!ret) {
+    logger_->log_error("Cannot open %s", filePath_.c_str());
+  }
+
+  return ret;
+}
+
+// Creates directory "processor_repository\ConsumeWindowsEventLog\uuid\{uuid}" 
under "bin" directory.
+bool Bookmark::createUUIDDir(const std::string& uuid, std::string& dir)
+{
+  auto getRootDirectory = [](std::string& rootDir, 
std::shared_ptr<logging::Logger> logger)
 
 Review comment:
   It is similar to breaking a large function in small functions even though 
they are not used anywhere other than in the large function.

----------------------------------------------------------------
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