diff --git a/repertory/librepertory/include/events/consumers/console_consumer.hpp b/repertory/librepertory/include/events/consumers/console_consumer.hpp index 7e3c3984..825ffc18 100644 --- a/repertory/librepertory/include/events/consumers/console_consumer.hpp +++ b/repertory/librepertory/include/events/consumers/console_consumer.hpp @@ -23,10 +23,12 @@ #define REPERTORY_INCLUDE_EVENTS_CONSUMERS_CONSOLE_CONSUMER_HPP_ #include "events/event_system.hpp" +#include "events/event_system2.hpp" namespace repertory { class console_consumer final { E_CONSUMER(); + E_CONSUMER2(); public: console_consumer(); @@ -36,7 +38,9 @@ public: ~console_consumer(); private: - void process_event(const event &e) const; + static void process_event(const event &evt); + + static void process_event2(const i_event &evt); }; } // namespace repertory diff --git a/repertory/librepertory/include/events/consumers/logging_consumer.hpp b/repertory/librepertory/include/events/consumers/logging_consumer.hpp index 16c4db85..3b2a8071 100644 --- a/repertory/librepertory/include/events/consumers/logging_consumer.hpp +++ b/repertory/librepertory/include/events/consumers/logging_consumer.hpp @@ -23,10 +23,12 @@ #define REPERTORY_INCLUDE_EVENTS_CONSUMERS_LOGGING_CONSUMER_HPP_ #include "events/event_system.hpp" +#include "events/event_system2.hpp" namespace repertory { class logging_consumer { E_CONSUMER(); + E_CONSUMER2(); public: logging_consumer(event_level level, std::string log_directory); @@ -39,7 +41,9 @@ private: 5ULL}; private: - void process_event(const event &event) const; + static void process_event(const event &evt); + + static void process_event2(const i_event &evt); }; } // namespace repertory diff --git a/repertory/librepertory/include/events/event_system2.hpp b/repertory/librepertory/include/events/event_system2.hpp index a62c5e13..01e219d7 100644 --- a/repertory/librepertory/include/events/event_system2.hpp +++ b/repertory/librepertory/include/events/event_system2.hpp @@ -51,26 +51,26 @@ protected: ~event_system2() { stop(); } public: - class event_consumer final { + class event_consumer2 final { public: - explicit event_consumer(std::function callback) + explicit event_consumer2(std::function callback) : callback_(std::move(callback)) { event_system2::instance().attach(this); } - event_consumer(std::string_view event_name, - std::function callback) + event_consumer2(std::string_view event_name, + std::function callback) : callback_(std::move(callback)) { event_system2::instance().attach(event_name, this); } - ~event_consumer() { event_system2::instance().release(this); } + ~event_consumer2() { event_system2::instance().release(this); } public: - event_consumer(const event_consumer &) = delete; - event_consumer(event_consumer &&) = delete; - auto operator=(const event_consumer &) -> event_consumer & = delete; - auto operator=(event_consumer &&) -> event_consumer & = delete; + event_consumer2(const event_consumer2 &) = delete; + event_consumer2(event_consumer2 &&) = delete; + auto operator=(const event_consumer2 &) -> event_consumer2 & = delete; + auto operator=(event_consumer2 &&) -> event_consumer2 & = delete; private: std::function callback_; @@ -80,13 +80,13 @@ public: }; private: - static event_system2 event_system_; + static event_system2 instance_; public: - static auto instance() -> event_system2 &; + [[nodiscard]] static auto instance() -> event_system2 &; private: - std::unordered_map> + std::unordered_map> event_consumers_; std::recursive_mutex consumer_mutex_; std::vector> event_list_; @@ -104,20 +104,42 @@ private: void queue_event(std::shared_ptr evt); public: - void attach(event_consumer *consumer); + void attach(event_consumer2 *consumer); - void attach(std::string_view event_name, event_consumer *consumer); + void attach(std::string_view event_name, event_consumer2 *consumer); template void raise(arg_t &&...args) { queue_event(std::make_shared(std::forward(args)...)); } - void release(event_consumer *consumer); + void release(event_consumer2 *consumer); void start(); void stop(); }; + +using event_consumer2 = event_system2::event_consumer2; + +#define E_CONSUMER2() \ +private: \ + std::vector> event_consumers2_ + +#define E_CONSUMER2_RELEASE() event_consumers2_.clear() + +#define E_SUBSCRIBE2(name, callback) \ + event_consumers2_.emplace_back(std::make_shared( \ + #name, [this](const i_event &evt) { callback(evt); })) + +#define E_SUBSCRIBE_EXACT2(name, callback) \ + event_consumers2_.emplace_back(std::make_shared( \ + #name, [this](const i_event &evt) { \ + callback(dynamic_cast(evt)); \ + })) + +#define E_SUBSCRIBE_ALL2(callback) \ + event_consumers2_.emplace_back(std::make_shared( \ + [this](const i_event &evt) { callback(evt); })) } // namespace repertory #endif // REPERTORY_INCLUDE_EVENTS_EVENT_SYSTEM2_HPP_ diff --git a/repertory/librepertory/include/events/events.hpp b/repertory/librepertory/include/events/events.hpp index c0e11bb8..fba6fa49 100644 --- a/repertory/librepertory/include/events/events.hpp +++ b/repertory/librepertory/include/events/events.hpp @@ -28,18 +28,6 @@ namespace repertory { // clang-format off -E_SIMPLE1(drive_unmount_pending, info, - std::string, location, loc, E_FROM_STRING -); - -E_SIMPLE1(drive_unmounted, info, - std::string, location, loc, E_FROM_STRING -); - -E_SIMPLE1(event_level_changed, info, - std::string, new_event_level, level, E_FROM_STRING -); - E_SIMPLE1(failed_upload_queued, error, std::string, api_path, ap, E_FROM_STRING ); diff --git a/repertory/librepertory/include/events/types/drive_unmount_pending.hpp b/repertory/librepertory/include/events/types/drive_unmount_pending.hpp new file mode 100644 index 00000000..e7eac508 --- /dev/null +++ b/repertory/librepertory/include/events/types/drive_unmount_pending.hpp @@ -0,0 +1,72 @@ +/* + Copyright <2018-2025> + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +*/ +#ifndef REPERTORY_INCLUDE_EVENTS_TYPES_DRIVE_UNMOUNT_PENDING_HPP_ +#define REPERTORY_INCLUDE_EVENTS_TYPES_DRIVE_UNMOUNT_PENDING_HPP_ + +#include "events/i_event.hpp" +#include "types/repertory.hpp" + +namespace repertory { +struct drive_unmount_pending final : public i_event { + drive_unmount_pending() = default; + drive_unmount_pending(std::string_view function_name_, + std::string mount_location_) + : function_name(std::string(function_name_)), + mount_location(std::move(mount_location_)) {} + + static constexpr const std::string_view name{"drive_unmount_pending"}; + + std::string function_name; + std::string mount_location; + + [[nodiscard]] auto get_event_level() const -> event_level override { + return event_level::info; + } + + [[nodiscard]] auto get_name() const -> std::string_view override { + return name; + } + + [[nodiscard]] auto get_single_line() const -> std::string override { + return fmt::format("{}|func|{}|location|{}", name, function_name, + mount_location); + } +}; +} // namespace repertory + +NLOHMANN_JSON_NAMESPACE_BEGIN +template <> struct adl_serializer { + static void to_json(json &data, + const repertory::drive_unmount_pending &value) { + data["function_name"] = value.function_name; + data["mount_location"] = value.mount_location; + } + + static void from_json(const json &data, + repertory::drive_unmount_pending &value) { + data.at("function_name").get_to(value.function_name); + data.at("mount_location").get_to(value.mount_location); + } +}; +NLOHMANN_JSON_NAMESPACE_END + +#endif // REPERTORY_INCLUDE_EVENTS_TYPES_DRIVE_UNMOUNT_PENDING_HPP_ diff --git a/repertory/librepertory/include/events/types/drive_unmounted.hpp b/repertory/librepertory/include/events/types/drive_unmounted.hpp new file mode 100644 index 00000000..84a72f47 --- /dev/null +++ b/repertory/librepertory/include/events/types/drive_unmounted.hpp @@ -0,0 +1,69 @@ +/* + Copyright <2018-2025> + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +*/ +#ifndef REPERTORY_INCLUDE_EVENTS_TYPES_DRIVE_UNMOUNTED_HPP_ +#define REPERTORY_INCLUDE_EVENTS_TYPES_DRIVE_UNMOUNTED_HPP_ + +#include "events/i_event.hpp" +#include "types/repertory.hpp" + +namespace repertory { +struct drive_unmounted final : public i_event { + drive_unmounted() = default; + drive_unmounted(std::string_view function_name_, std::string mount_location_) + : function_name(std::string(function_name_)), + mount_location(std::move(mount_location_)) {} + + static constexpr const std::string_view name{"drive_unmounted"}; + + std::string function_name; + std::string mount_location; + + [[nodiscard]] auto get_event_level() const -> event_level override { + return event_level::info; + } + + [[nodiscard]] auto get_name() const -> std::string_view override { + return name; + } + + [[nodiscard]] auto get_single_line() const -> std::string override { + return fmt::format("{}|func|{}|location|{}", name, function_name, + mount_location); + } +}; +} // namespace repertory + +NLOHMANN_JSON_NAMESPACE_BEGIN +template <> struct adl_serializer { + static void to_json(json &data, const repertory::drive_unmounted &value) { + data["function_name"] = value.function_name; + data["mount_location"] = value.mount_location; + } + + static void from_json(const json &data, repertory::drive_unmounted &value) { + data.at("function_name").get_to(value.function_name); + data.at("mount_location").get_to(value.mount_location); + } +}; +NLOHMANN_JSON_NAMESPACE_END + +#endif // REPERTORY_INCLUDE_EVENTS_TYPES_DRIVE_UNMOUNTED_HPP_ diff --git a/repertory/librepertory/include/events/types/event_level_changed.hpp b/repertory/librepertory/include/events/types/event_level_changed.hpp new file mode 100644 index 00000000..389c8ba8 --- /dev/null +++ b/repertory/librepertory/include/events/types/event_level_changed.hpp @@ -0,0 +1,70 @@ +/* + Copyright <2018-2025> + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +*/ +#ifndef REPERTORY_INCLUDE_EVENTS_TYPES_EVENT_LEVEL_CHANGED_HPP_ +#define REPERTORY_INCLUDE_EVENTS_TYPES_EVENT_LEVEL_CHANGED_HPP_ + +#include "events/i_event.hpp" +#include "types/repertory.hpp" + +namespace repertory { +struct event_level_changed final : public i_event { + event_level_changed() = default; + event_level_changed(std::string_view function_name_, event_level level_) + : function_name(std::string(function_name_)), level(level_) {} + + static constexpr const std::string_view name{"event_level_changed"}; + + std::string function_name; + event_level level{}; + + [[nodiscard]] auto get_event_level() const -> event_level override { + return event_level::info; + } + + [[nodiscard]] auto get_name() const -> std::string_view override { + return name; + } + + [[nodiscard]] auto get_single_line() const -> std::string override { + return fmt::format("{}|func|{}|level|{}", name, function_name, + event_level_to_string(level)); + } +}; +} // namespace repertory + +NLOHMANN_JSON_NAMESPACE_BEGIN +template <> struct adl_serializer { + static void to_json(json &data, const repertory::event_level_changed &value) { + data["event_level"] = repertory::event_level_to_string(value.level); + data["function_name"] = value.function_name; + } + + static void from_json(const json &data, + repertory::event_level_changed &value) { + value.level = repertory::event_level_from_string( + data.at("event_level").get()); + data.at("function_name").get_to(value.function_name); + } +}; +NLOHMANN_JSON_NAMESPACE_END + +#endif // REPERTORY_INCLUDE_EVENTS_TYPES_EVENT_LEVEL_CHANGED_HPP_ diff --git a/repertory/librepertory/src/app_config.cpp b/repertory/librepertory/src/app_config.cpp index 8af9f113..08627793 100644 --- a/repertory/librepertory/src/app_config.cpp +++ b/repertory/librepertory/src/app_config.cpp @@ -21,8 +21,8 @@ */ #include "app_config.hpp" -#include "events/event_system.hpp" -#include "events/events.hpp" +#include "events/event_system2.hpp" +#include "events/types/event_level_changed.hpp" #include "file_manager/cache_size_mgr.hpp" #include "platform/platform.hpp" #include "types/startup_exception.hpp" @@ -1146,9 +1146,10 @@ void app_config::set_enable_mount_manager(bool value) { #endif // defined(_WIN32) void app_config::set_event_level(const event_level &value) { + REPERTORY_USES_FUNCTION_NAME(); + if (set_value(event_level_, value)) { - event_system::instance().raise( - event_level_to_string(value)); + event_system2::instance().raise(function_name, value); } } diff --git a/repertory/librepertory/src/drives/fuse/fuse_drive.cpp b/repertory/librepertory/src/drives/fuse/fuse_drive.cpp index 04a579e0..465fe138 100644 --- a/repertory/librepertory/src/drives/fuse/fuse_drive.cpp +++ b/repertory/librepertory/src/drives/fuse/fuse_drive.cpp @@ -35,6 +35,8 @@ #include "events/events.hpp" #include "events/types/drive_mount_result.hpp" #include "events/types/drive_mounted.hpp" +#include "events/types/drive_unmount_pending.hpp" +#include "events/types/drive_unmounted.hpp" #include "platform/platform.hpp" #include "providers/i_provider.hpp" #include "rpc/server/full_server.hpp" @@ -293,7 +295,8 @@ void fuse_drive::stop_all() { void fuse_drive::destroy_impl(void *ptr) { REPERTORY_USES_FUNCTION_NAME(); - event_system::instance().raise(get_mount_location()); + event_system2::instance().raise(function_name, + get_mount_location()); stop_all(); @@ -301,7 +304,8 @@ void fuse_drive::destroy_impl(void *ptr) { fuse_base::destroy_impl(ptr); - event_system::instance().raise(get_mount_location()); + event_system2::instance().raise(function_name, + get_mount_location()); } auto fuse_drive::fallocate_impl(std::string /*api_path*/, int mode, diff --git a/repertory/librepertory/src/drives/fuse/remotefuse/remote_fuse_drive.cpp b/repertory/librepertory/src/drives/fuse/remotefuse/remote_fuse_drive.cpp index 16c740bc..de6b83d2 100644 --- a/repertory/librepertory/src/drives/fuse/remotefuse/remote_fuse_drive.cpp +++ b/repertory/librepertory/src/drives/fuse/remotefuse/remote_fuse_drive.cpp @@ -32,6 +32,8 @@ #include "events/events.hpp" #include "events/types/drive_mount_result.hpp" #include "events/types/drive_mounted.hpp" +#include "events/types/drive_unmount_pending.hpp" +#include "events/types/drive_unmounted.hpp" #include "platform/platform.hpp" #include "rpc/server/server.hpp" #include "types/remote.hpp" @@ -93,7 +95,8 @@ auto remote_fuse_drive::create_impl(std::string api_path, mode_t mode, void remote_fuse_drive::destroy_impl(void *ptr) { REPERTORY_USES_FUNCTION_NAME(); - event_system::instance().raise(get_mount_location()); + event_system2::instance().raise(function_name, + get_mount_location()); if (server_) { server_->stop(); @@ -114,7 +117,8 @@ void remote_fuse_drive::destroy_impl(void *ptr) { utils::error::raise_error(function_name, "failed to set mount state"); } - event_system::instance().raise(get_mount_location()); + event_system2::instance().raise(function_name, + get_mount_location()); fuse_base::destroy_impl(ptr); } diff --git a/repertory/librepertory/src/drives/winfsp/remotewinfsp/remote_client.cpp b/repertory/librepertory/src/drives/winfsp/remotewinfsp/remote_client.cpp index 2d6a58fa..52a7a43c 100644 --- a/repertory/librepertory/src/drives/winfsp/remotewinfsp/remote_client.cpp +++ b/repertory/librepertory/src/drives/winfsp/remotewinfsp/remote_client.cpp @@ -26,6 +26,8 @@ #include "events/event_system2.hpp" #include "events/events.hpp" #include "events/types/drive_mounted.hpp" +#include "events/types/drive_unmount_pending.hpp" +#include "events/types/drive_unmounted.hpp" #include "types/repertory.hpp" #include "utils/path.hpp" #include "version.hpp" @@ -572,7 +574,8 @@ auto remote_client::winfsp_unmounted(const std::wstring &location) auto mount_location{ utils::string::to_utf8(location), }; - event_system::instance().raise(mount_location); + event_system2::instance().raise(function_name, + mount_location); packet request; request.encode(location); @@ -580,7 +583,8 @@ auto remote_client::winfsp_unmounted(const std::wstring &location) auto ret{ packet_client_.send(function_name, request, service_flags), }; - event_system::instance().raise(mount_location); + event_system2::instance().raise(function_name, + mount_location); RAISE_REMOTE_WINFSP_CLIENT_EVENT(function_name, mount_location, ret); return ret; diff --git a/repertory/librepertory/src/drives/winfsp/winfsp_drive.cpp b/repertory/librepertory/src/drives/winfsp/winfsp_drive.cpp index 97a2c8f3..792851fa 100644 --- a/repertory/librepertory/src/drives/winfsp/winfsp_drive.cpp +++ b/repertory/librepertory/src/drives/winfsp/winfsp_drive.cpp @@ -32,6 +32,8 @@ #include "events/types/drive_mount_failed.hpp" #include "events/types/drive_mount_result.hpp" #include "events/types/drive_mounted.hpp" +#include "events/types/drive_unmount_pending.hpp" +#include "events/types/drive_unmounted.hpp" #include "platform/platform.hpp" #include "providers/i_provider.hpp" #include "types/repertory.hpp" @@ -1226,11 +1228,13 @@ VOID winfsp_drive::Unmounted(PVOID host) { auto *file_system_host = reinterpret_cast(host); auto mount_location = parse_mount_location(file_system_host->MountPoint()); - event_system::instance().raise(mount_location); + event_system2::instance().raise(function_name, + mount_location); stop_all(); - event_system::instance().raise(mount_location); + event_system2::instance().raise(function_name, + mount_location); config_.save(); } diff --git a/repertory/librepertory/src/events/consumers/console_consumer.cpp b/repertory/librepertory/src/events/consumers/console_consumer.cpp index 9db09965..18389beb 100644 --- a/repertory/librepertory/src/events/consumers/console_consumer.cpp +++ b/repertory/librepertory/src/events/consumers/console_consumer.cpp @@ -21,7 +21,8 @@ */ #include "events/consumers/console_consumer.hpp" -#include "events/events.hpp" +#include "events/i_event.hpp" +#include "events/types/event_level_changed.hpp" #include "spdlog/async.h" #include "spdlog/sinks/stdout_color_sinks.h" #include "spdlog/spdlog.h" @@ -62,35 +63,57 @@ console_consumer::console_consumer(event_level level) { set_level(level); E_SUBSCRIBE_ALL(process_event); - E_SUBSCRIBE_EXACT(event_level_changed, - [](const event_level_changed &changed) { - set_level(event_level_from_string( - changed.get_new_event_level().get())); - }); + E_SUBSCRIBE2_ALL(process_event2); + E_SUBSCRIBE2_EXACT(event_level_changed, + [](auto &&evt) { set_level(evt.level); }); } console_consumer::~console_consumer() { E_CONSUMER_RELEASE(); } -void console_consumer::process_event(const event &event) const { - switch (event.get_event_level()) { +void console_consumer::process_event(const event &event) { + switch (evt.get_event_level()) { case event_level::critical: - spdlog::get("console")->critical(event.get_single_line()); + spdlog::get("console")->critical(evt.get_single_line()); break; case event_level::error: - spdlog::get("console")->error(event.get_single_line()); + spdlog::get("console")->error(evt.get_single_line()); break; case event_level::warn: - spdlog::get("console")->warn(event.get_single_line()); + spdlog::get("console")->warn(evt.get_single_line()); break; case event_level::info: - spdlog::get("console")->info(event.get_single_line()); + spdlog::get("console")->info(evt.get_single_line()); break; case event_level::debug: - spdlog::get("console")->debug(event.get_single_line()); + spdlog::get("console")->debug(evt.get_single_line()); break; case event_level::trace: default: - spdlog::get("console")->trace(event.get_single_line()); + spdlog::get("console")->trace(evt.get_single_line()); + break; + } +} + +void console_consumer::process_event(const i_event &evt) const { + switch (evt.get_event_level()) { + case event_level::critical: + spdlog::get("console")->critical(evt.get_single_line()); + break; + case event_level::error: + spdlog::get("console")->error(evt.get_single_line()); + break; + case event_level::warn: + spdlog::get("console")->warn(evt.get_single_line()); + break; + case event_level::info: + spdlog::get("console")->info(evt.get_single_line()); + break; + case event_level::debug: + spdlog::get("console")->debug(evt.get_single_line()); + break; + case event_level::trace: + default: + spdlog::get("console")->trace(evt.get_single_line()); break; } } diff --git a/repertory/librepertory/src/events/consumers/logging_consumer.cpp b/repertory/librepertory/src/events/consumers/logging_consumer.cpp index 6944d023..0678ca0b 100644 --- a/repertory/librepertory/src/events/consumers/logging_consumer.cpp +++ b/repertory/librepertory/src/events/consumers/logging_consumer.cpp @@ -22,6 +22,8 @@ #include "events/consumers/logging_consumer.hpp" #include "events/events.hpp" +#include "events/i_event.hpp" +#include "events/types/event_level_changed.hpp" #include "spdlog/async.h" #include "spdlog/sinks/rotating_file_sink.h" #include "spdlog/spdlog.h" @@ -66,35 +68,60 @@ logging_consumer::logging_consumer(event_level level, set_level(level); E_SUBSCRIBE_ALL(process_event); - E_SUBSCRIBE_EXACT(event_level_changed, - [](const event_level_changed &changed) { - set_level(event_level_from_string( - changed.get_new_event_level().get())); - }); + E_SUBSCRIBE2_ALL(process_event2); + E_SUBSCRIBE2_EXACT(event_level_changed, + [](auto &&evt) { set_level(evt.level); }); } -logging_consumer::~logging_consumer() { E_CONSUMER_RELEASE(); } +logging_consumer::~logging_consumer() { + E_CONSUMER_RELEASE(); + E_CONSUMER2_RELEASE(); +} -void logging_consumer::process_event(const event &event) const { - switch (event.get_event_level()) { +void logging_consumer::process_event(const event &evt) { + switch (evt.get_event_level()) { case event_level::critical: - spdlog::get("file")->critical(event.get_single_line()); + spdlog::get("file")->critical(evt.get_single_line()); break; case event_level::error: - spdlog::get("file")->error(event.get_single_line()); + spdlog::get("file")->error(evt.get_single_line()); break; case event_level::warn: - spdlog::get("file")->warn(event.get_single_line()); + spdlog::get("file")->warn(evt.get_single_line()); break; case event_level::info: - spdlog::get("file")->info(event.get_single_line()); + spdlog::get("file")->info(evt.get_single_line()); break; case event_level::debug: - spdlog::get("file")->debug(event.get_single_line()); + spdlog::get("file")->debug(evt.get_single_line()); break; case event_level::trace: default: - spdlog::get("file")->trace(event.get_single_line()); + spdlog::get("file")->trace(evt.get_single_line()); + break; + } +} + +void logging_consumer::process_event2(const i_event &evt) { + switch (evt.get_event_level()) { + case event_level::critical: + spdlog::get("file")->critical(evt.get_single_line()); + break; + case event_level::error: + spdlog::get("file")->error(evt.get_single_line()); + break; + case event_level::warn: + spdlog::get("file")->warn(evt.get_single_line()); + break; + case event_level::info: + spdlog::get("file")->info(evt.get_single_line()); + break; + case event_level::debug: + spdlog::get("file")->debug(evt.get_single_line()); + break; + case event_level::trace: + default: + spdlog::get("file")->trace(evt.get_single_line()); break; } } diff --git a/repertory/librepertory/src/events/event_system_2.cpp b/repertory/librepertory/src/events/event_system_2.cpp index 0eb37ed9..236b170b 100644 --- a/repertory/librepertory/src/events/event_system_2.cpp +++ b/repertory/librepertory/src/events/event_system_2.cpp @@ -26,24 +26,26 @@ #include "utils/collection.hpp" namespace repertory { -void event_system2::attach(event_consumer *consumer) { +event_system2 event_system2::instance_{}; + +auto event_system2::instance() -> event_system2 & { return instance_; } + +void event_system2::attach(event_consumer2 *consumer) { recur_mutex_lock lock(consumer_mutex_); event_consumers_[""].push_back(consumer); } void event_system2::attach(std::string_view event_name, - event_consumer *consumer) { + event_consumer2 *consumer) { recur_mutex_lock lock(consumer_mutex_); event_consumers_[std::string{event_name}].push_back(consumer); } -[[nodiscard]] auto event_system2::get_stop_requested() const -> bool { +auto event_system2::get_stop_requested() const -> bool { return stop_requested_ || app_config::get_stop_requested(); } void event_system2::process_events() { - std::vector> events; - unique_mutex_lock lock(event_mutex_); const auto lock_and_notify = [this, &lock]() { lock.lock(); @@ -55,14 +57,11 @@ void event_system2::process_events() { event_notify_.wait_for(lock, queue_wait_secs); } - if (not event_list_.empty()) { - events.insert(events.end(), event_list_.begin(), event_list_.end()); - event_list_.clear(); - } - + std::vector> event_list; + std::swap(event_list, event_list_); lock.unlock(); - if (events.empty()) { + if (event_list.empty()) { lock_and_notify(); return; } @@ -83,7 +82,7 @@ void event_system2::process_events() { } }; - for (const auto &evt : events) { + for (const auto &evt : event_list) { notify_events("", *evt.get()); notify_events(evt->get_name(), *evt.get()); } @@ -114,15 +113,15 @@ void event_system2::queue_event(std::shared_ptr evt) { } } -void event_system2::release(event_consumer *consumer) { +void event_system2::release(event_consumer2 *consumer) { recur_mutex_lock lock(consumer_mutex_); auto iter = - std::ranges::find_if(event_consumers_, [&](const auto &item) -> bool { + std::ranges::find_if(event_consumers_, [&consumer](auto &&item) -> bool { return utils::collection::includes(item.second, consumer); }); if (iter != event_consumers_.end()) { - utils::collection::remove_element((*iter).second, consumer); + utils::collection::remove_element(iter->second, consumer); } }