arpadboda commented on a change in pull request #560: MINIFICPP-852: Add 
details to restful response. CoAP will not include…
URL: https://github.com/apache/nifi-minifi-cpp/pull/560#discussion_r285506747
 
 

 ##########
 File path: libminifi/include/core/logging/LoggingInterceptor.h
 ##########
 @@ -0,0 +1,117 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef LIBMINIFI_INCLUDE_CORE_LOGGING_LOGGINGINTERCEPTOR_H_
+#define LIBMINIFI_INCLUDE_CORE_LOGGING_LOGGINGINTERCEPTOR_H_
+
+#include <vector>
+#include <mutex>
+#include <algorithm>
+#include "core/expect.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace core {
+namespace logging {
+
+class Interceptor {
+ public:
+  void intercept_error(const std::string &msg) {
+    std::lock_guard<std::mutex> lock(guard_);
+    errors_.emplace_back(msg);
+  }
+
+  const std::vector<std::string> &getErrors() const {
+    return errors_;
+  }
+
+ protected:
+  std::mutex guard_;
+  std::vector<std::string> errors_;
+};
+
+class LoggingInterceptors {
+ public:
+  static LoggingInterceptors *getInstance() {
+    static LoggingInterceptors interceptors;
+    return &interceptors;
+  }
+
+  void addInterceptor(Interceptor * interceptor) {
+    std::lock_guard<std::mutex> lock(guard_);
+    interceptors_.emplace_back(interceptor);
+  }
+
+  void removeInterceptor(const Interceptor * const interceptor) {
+    std::lock_guard<std::mutex> lock(guard_);
+    interceptors_.erase(std::remove(interceptors_.begin(), 
interceptors_.end(), interceptor), interceptors_.end());
+  }
+
+  void intercept_error(const std::string &message) {
+    std::lock_guard<std::mutex> lock(guard_);
+    if (UNLIKELY(!interceptors_.empty())) {
+      for (auto &interceptor : interceptors_) {
+        interceptor->intercept_error(message);
+      }
+    }
+  }
+
+  bool empty() const{
+    std::lock_guard<std::mutex> lock(guard_);
+    return interceptors_.empty();
+  }
+
+ private:
+  LoggingInterceptors() {
+
+  }
+
+  mutable std::mutex guard_;
+
+  std::vector<Interceptor*> interceptors_;
+};
+
+/**
+ * Logging interceptor gives us a way to temporarily retrieve certain log 
levels
+ * and provide them elsewhere. Currently this is only used for error.
+ */
+class LoggingInterceptor : public Interceptor {
+
+ public:
+
+  LoggingInterceptor() {
+    LoggingInterceptors::getInstance()->addInterceptor(this);
+  }
+
+  ~LoggingInterceptor() {
+    std::lock_guard<std::mutex> lock(guard_);
 
 Review comment:
   This is a possible deadlock. 
   LoggingInterceptor is being destructed while 
LoggingInterceptors::intercept_error is being executed. 
   Both of these grab's it's own lock before trying to call a member of the 
other to lock that, too. 

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to