[hermes] Introduce Context class (#3585)
author박종현/On-Device Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Fri, 24 May 2019 01:19:53 +0000 (10:19 +0900)
committer박세희/On-Device Lab(SR)/Principal Engineer/삼성전자 <saehie.park@samsung.com>
Fri, 24 May 2019 01:19:53 +0000 (10:19 +0900)
This commit introduces Context class which serves as a central
controller for hermes logging subsystem.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
contrib/hermes/include/hermes.h
contrib/hermes/include/hermes/core/Context.h [new file with mode: 0644]
contrib/hermes/src/core/Context.cpp [new file with mode: 0644]
contrib/hermes/src/core/Context.test.cpp [new file with mode: 0644]

index b3ec28a..13202e6 100644 (file)
@@ -19,6 +19,7 @@
 
 #include "hermes/core/Severity.h"
 #include "hermes/core/Message.h"
+#include "hermes/core/Context.h"
 // TO BE FILLED
 
 #endif // __HERMES_H__
diff --git a/contrib/hermes/include/hermes/core/Context.h b/contrib/hermes/include/hermes/core/Context.h
new file mode 100644 (file)
index 0000000..4054587
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * Licensed 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 __HERMES_CONTEXT_H__
+#define __HERMES_CONTEXT_H__
+
+#include "hermes/core/Config.h"
+#include "hermes/core/Source.h"
+#include "hermes/core/Sink.h"
+#include "hermes/core/MessageBus.h"
+
+#include <memory>
+#include <set>
+
+namespace hermes
+{
+
+/**
+ * @brief Logging controller
+ *
+ * This "Context" serves as a controller for associated logging source/sink.
+ *
+ * WARNING This "Context" is not yet thread-safe.
+ * TODO Support multi-threaded application logging
+ */
+class Context final : private MessageBus, private Source::Registry, private Sink::Registry
+{
+public:
+  /// @brief Get the global configuration
+  const Config *config(void) const;
+  /// @brief Update the global configuration
+  void config(std::unique_ptr<Config> &&);
+
+public:
+  MessageBus *bus(void) { return this; }
+
+private:
+  /// This implements "post" method that MessageBus interface requires.
+  void post(std::unique_ptr<Message> &&msg) override;
+
+public:
+  Source::Registry *sources(void) { return this; }
+
+private:
+  /// This implements "attach" method that "Source::Registry" interface requires.
+  void attach(Source *source) override;
+  /// This implements "detach" method that "Source::Registry" interface requires.
+  void detach(Source *source) override;
+
+public:
+  Sink::Registry *sinks(void) { return this; }
+
+private:
+  /// This implements "append" method that "Sink::Registry" interface requires.
+  void append(std::unique_ptr<Sink> &&sink) override;
+
+private:
+  std::unique_ptr<Config> _config;
+  std::set<Source *> _sources;
+  std::set<std::unique_ptr<Sink>> _sinks;
+};
+
+} // namespace hermes
+
+#endif // __HERMES_CONTEXT_H__
diff --git a/contrib/hermes/src/core/Context.cpp b/contrib/hermes/src/core/Context.cpp
new file mode 100644 (file)
index 0000000..a6970f0
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * Licensed 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.
+ */
+
+#include "hermes/core/Context.h"
+
+#include <cassert>
+
+namespace hermes
+{
+
+const Config *Context::config(void) const
+{
+  // Return the current configuration
+  return _config.get();
+}
+
+void Context::config(std::unique_ptr<Config> &&config)
+{
+  _config = std::move(config);
+
+  // Apply updated configurations
+  for (auto source : _sources)
+  {
+    source->reload(_config.get());
+  }
+}
+
+void Context::post(std::unique_ptr<Message> &&msg)
+{
+  // Validate message
+  assert((msg != nullptr) && "invalid message");
+  assert((msg->text() != nullptr) && "missing text");
+
+  // Take the ownership of a given message
+  auto m = std::move(msg);
+
+  // Notify appended sinks
+  for (const auto &sink : _sinks)
+  {
+    sink->notify(m.get());
+  }
+
+  // TODO Stop the process if "FATAL" message is posted
+}
+
+void Context::attach(Source *source)
+{
+  // Configure source first
+  source->reload(config());
+  // Insert source
+  _sources.insert(source);
+}
+
+void Context::detach(Source *source)
+{
+  // Remove source
+  _sources.erase(source);
+}
+
+void Context::append(std::unique_ptr<Sink> &&sink)
+{
+  // Append sink
+  _sinks.insert(std::move(sink));
+}
+
+} // namespace hermes
diff --git a/contrib/hermes/src/core/Context.test.cpp b/contrib/hermes/src/core/Context.test.cpp
new file mode 100644 (file)
index 0000000..0c8defd
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * Licensed 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.
+ */
+
+#include "hermes/core/Context.h"
+
+#include <gtest/gtest.h>
+
+TEST(ContextTest, constructor)
+{
+  hermes::Context ctx;
+
+  ASSERT_NE(ctx.bus(), nullptr);
+  ASSERT_NE(ctx.sources(), nullptr);
+  ASSERT_NE(ctx.sinks(), nullptr);
+}