From 627e82a84c8522549e02f8919db0ae9c0ff926b7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=A2=85=ED=98=84/On-Device=20Lab=28SR=29/Staff?= =?utf8?q?=20Engineer/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Fri, 24 May 2019 10:19:53 +0900 Subject: [PATCH] [hermes] Introduce Context class (#3585) This commit introduces Context class which serves as a central controller for hermes logging subsystem. Signed-off-by: Jonghyun Park --- contrib/hermes/include/hermes.h | 1 + contrib/hermes/include/hermes/core/Context.h | 78 +++++++++++++++++++++++++++ contrib/hermes/src/core/Context.cpp | 79 ++++++++++++++++++++++++++++ contrib/hermes/src/core/Context.test.cpp | 28 ++++++++++ 4 files changed, 186 insertions(+) create mode 100644 contrib/hermes/include/hermes/core/Context.h create mode 100644 contrib/hermes/src/core/Context.cpp create mode 100644 contrib/hermes/src/core/Context.test.cpp diff --git a/contrib/hermes/include/hermes.h b/contrib/hermes/include/hermes.h index b3ec28a..13202e6 100644 --- a/contrib/hermes/include/hermes.h +++ b/contrib/hermes/include/hermes.h @@ -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 index 0000000..4054587 --- /dev/null +++ b/contrib/hermes/include/hermes/core/Context.h @@ -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 +#include + +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 &&); + +public: + MessageBus *bus(void) { return this; } + +private: + /// This implements "post" method that MessageBus interface requires. + void post(std::unique_ptr &&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) override; + +private: + std::unique_ptr _config; + std::set _sources; + std::set> _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 index 0000000..a6970f0 --- /dev/null +++ b/contrib/hermes/src/core/Context.cpp @@ -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 + +namespace hermes +{ + +const Config *Context::config(void) const +{ + // Return the current configuration + return _config.get(); +} + +void Context::config(std::unique_ptr &&config) +{ + _config = std::move(config); + + // Apply updated configurations + for (auto source : _sources) + { + source->reload(_config.get()); + } +} + +void Context::post(std::unique_ptr &&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) +{ + // 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 index 0000000..0c8defd --- /dev/null +++ b/contrib/hermes/src/core/Context.test.cpp @@ -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 + +TEST(ContextTest, constructor) +{ + hermes::Context ctx; + + ASSERT_NE(ctx.bus(), nullptr); + ASSERT_NE(ctx.sources(), nullptr); + ASSERT_NE(ctx.sinks(), nullptr); +} -- 2.7.4