diff options
| author | Felix Morgner <felix.morgner@gmail.com> | 2018-12-08 11:54:28 +0100 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@gmail.com> | 2018-12-08 11:54:28 +0100 |
| commit | 7b940a39dfef6f19846fe357d4a5167c66c79e85 (patch) | |
| tree | e83fef50b0125ff8034305d71821d6857522d60a | |
| parent | 0c1d1a0f24655c22a373c19aac90a6a225a353e6 (diff) | |
| download | wanda-7b940a39dfef6f19846fe357d4a5167c66c79e85.tar.xz wanda-7b940a39dfef6f19846fe357d4a5167c66c79e85.zip | |
wanda: extract logging
| -rw-r--r-- | CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/commander.cpp | 20 | ||||
| -rw-r--r-- | src/commander.hpp | 4 | ||||
| -rw-r--r-- | src/control_interface.cpp | 28 | ||||
| -rw-r--r-- | src/control_interface.hpp | 7 | ||||
| -rw-r--r-- | src/logging.cpp | 21 | ||||
| -rw-r--r-- | src/logging.hpp | 18 | ||||
| -rw-r--r-- | src/wallpaper.cpp | 9 | ||||
| -rw-r--r-- | src/wallpaper.hpp | 2 | ||||
| -rw-r--r-- | src/wandac.cpp | 16 | ||||
| -rw-r--r-- | src/wandad.cpp | 30 | ||||
| -rw-r--r-- | src/xdg.hpp | 9 |
12 files changed, 105 insertions, 61 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 6d8a867..d805943 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,6 +26,7 @@ set(WANDA_CORE_SOURCES "src/command.cpp" "src/control_connection.cpp" "src/environment.cpp" + "src/logging.cpp" "src/message.cpp" "src/xdg.cpp" ) @@ -37,6 +38,7 @@ set(WANDA_CORE_HEADERS "src/environment.hpp" "src/expected.hpp" "src/keyed.hpp" + "src/logging.hpp" "src/message.hpp" "src/optional.hpp" "src/type_wrapper.hpp" diff --git a/src/commander.cpp b/src/commander.cpp index 36d9fba..a67d06a 100644 --- a/src/commander.cpp +++ b/src/commander.cpp @@ -1,4 +1,5 @@ #include "commander.hpp" +#include "logging.hpp" #include "message.hpp" #include "optional.hpp" @@ -6,12 +7,11 @@ namespace wanda { - commander::commander(asio::io_service & service, std::filesystem::path socket, listener & listener, std::shared_ptr<spdlog::logger> logger) + commander::commander(asio::io_service & service, std::filesystem::path socket, listener & listener) : m_service{service} , m_endpoint{socket.string()} , m_socket{service} , m_listener{listener} - , m_logger{logger} { } @@ -20,11 +20,11 @@ namespace wanda m_socket.async_connect(m_endpoint, [&](auto const & error) { if (error) { - m_logger->error("error while connecting to control interface: '{}'", error.message()); + get_logger()->error("error while connecting to control interface: '{}'", error.message()); } else { - m_logger->info("establishing connection to wanda deamon"); + get_logger()->info("establishing connection to wanda deamon"); m_connection = wanda::make_control_connection(std::move(m_socket)); m_connection->add(this); m_connection->start(); @@ -35,7 +35,7 @@ namespace wanda void commander::stop() { - m_logger->info("closing control connection"); + get_logger()->info("closing control connection"); m_connection->close(); } @@ -45,31 +45,31 @@ namespace wanda if (!m_connection || m_connection->current_state() != control_connection::state::established) { - m_logger->error("tried to send command without an established connection"); + get_logger()->error("tried to send command without an established connection"); m_listener.on_error(*this, "tried to send command without an established connection"); return; } with(command.message(), [&](auto const & message) { m_connection->send(message); }) || - [&] { m_logger->error("unknown command"); }; + [&] { get_logger()->error("unknown command"); }; } void commander::on_error(control_connection::pointer connection, std::error_code error) { - m_logger->error("control interface communication error: '{}'", error.message()); + get_logger()->error("control interface communication error: '{}'", error.message()); } void commander::on_received(wanda::control_connection::pointer connection, message message) { if (auto state = connection->current_state(); message.command == "HELLO" && state == control_connection::state::fresh) { - m_logger->info("connection to wanda deamon successfully established"); + get_logger()->info("connection to wanda deamon successfully established"); connection->update(control_connection::state::established); m_listener.on_connected(*this); } else { - m_logger->error("unexpected message: '{}'", message); + get_logger()->error("unexpected message: '{}'", message); m_listener.on_error(*this, "unexpected message '" + static_cast<std::string>(message) + '\''); } } diff --git a/src/commander.hpp b/src/commander.hpp index 122b07d..131022d 100644 --- a/src/commander.hpp +++ b/src/commander.hpp @@ -6,7 +6,6 @@ #include "message.hpp" #include <asio.hpp> -#include <spdlog/logger.h> #include <filesystem> #include <memory> @@ -25,7 +24,7 @@ namespace wanda virtual void on_error(commander & commander, std::string error){}; }; - commander(asio::io_service & service, std::filesystem::path socket, listener & listener, std::shared_ptr<spdlog::logger> logger); + commander(asio::io_service & service, std::filesystem::path socket, listener & listener); void start(); void stop(); @@ -40,7 +39,6 @@ namespace wanda wanda::control_connection::protocol::socket m_socket; wanda::control_connection::pointer m_connection; listener & m_listener; - std::shared_ptr<spdlog::logger> m_logger; }; } // namespace wanda diff --git a/src/control_interface.cpp b/src/control_interface.cpp index c847f94..ff96cd3 100644 --- a/src/control_interface.cpp +++ b/src/control_interface.cpp @@ -1,4 +1,5 @@ #include "control_interface.hpp" +#include "logging.hpp" #include "optional.hpp" #include <spdlog/fmt/ostr.h> @@ -25,14 +26,13 @@ namespace wanda // 'control_interface' implementation - control_interface::control_interface(control_interface::key key, asio::io_service & service, control_interface::protocol::endpoint endpoint, listener & listener, std::shared_ptr<spdlog::logger> logger) + control_interface::control_interface(control_interface::key key, asio::io_service & service, control_interface::protocol::endpoint endpoint, listener & listener) : keyed{key} , m_service{service} , m_endpoint{std::move(endpoint)} , m_socket{m_service} , m_acceptor{m_service} , m_listener{listener} - , m_logger{logger} { } @@ -75,11 +75,11 @@ namespace wanda m_acceptor.async_accept(m_socket, [that = shared_from_this(), this](auto const & error) { if (error && error != asio::error::operation_aborted) { - m_logger->error("failed to accept connection because '{}'", error); + get_logger()->error("failed to accept connection because '{}'", error); } else { - m_logger->info("new incoming controller connection"); + get_logger()->info("new incoming controller connection"); auto [connection, inserted] = m_connections.insert(make_control_connection(std::move(m_socket))); if (inserted) { @@ -95,11 +95,11 @@ namespace wanda { if (static_cast<char>(connection->current_state()) >= static_cast<char>(control_connection::state::established)) { - m_logger->info("controller connection closed"); + get_logger()->info("controller connection closed"); } else { - m_logger->info("controller connection aborted before it could be established"); + get_logger()->info("controller connection aborted before it could be established"); } m_connections.erase(connection); } @@ -110,22 +110,22 @@ namespace wanda if (m_connections.find(connection) == m_connections.cend()) { - m_logger->error("received message from an unknown connection"); + get_logger()->error("received message from an unknown connection"); return; } if (message.source != message_source_controller) { - m_logger->error("received a deamon message"); + get_logger()->error("received a deamon message"); return; } if (auto state = connection->current_state(); message.command == message_command_hello && state == control_connection::state::fresh) { - m_logger->info("controller connection established"); + get_logger()->info("controller connection established"); if (message.argument.has_value()) { - m_logger->info("remote controller version '{}'", *message.argument); + get_logger()->info("remote controller version '{}'", *message.argument); } connection->send({message_source_daemon, message_command_hello, message_argument_hello}); connection->update(control_connection::state::established); @@ -135,20 +135,20 @@ namespace wanda with(make_command(message), [&](auto const & command) { m_listener.on_received(*this, command); }) || - [&] { m_logger->warn("ignoring unknown message '{}'", message); }; + [&] { get_logger()->warn("ignoring unknown message '{}'", message); }; } } - control_interface::pointer make_interface(asio::io_service & service, std::filesystem::path file, control_interface::listener & listener, std::shared_ptr<spdlog::logger> logger) + control_interface::pointer make_interface(asio::io_service & service, std::filesystem::path file, control_interface::listener & listener) { if (std::filesystem::exists(file)) { - logger->error("file '{}' exists", file.native()); + get_logger()->error("file '{}' exists", file.native()); return {}; } control_interface::protocol::endpoint endpoint{file.string()}; - return std::make_shared<control_interface>(control_interface::key{}, service, std::move(endpoint), listener, logger); + return std::make_shared<control_interface>(control_interface::key{}, service, std::move(endpoint), listener); } } // namespace wanda
\ No newline at end of file diff --git a/src/control_interface.hpp b/src/control_interface.hpp index 9dc461c..82cc2af 100644 --- a/src/control_interface.hpp +++ b/src/control_interface.hpp @@ -36,7 +36,7 @@ namespace wanda virtual void on_received(control_interface & interface, command command){}; }; - control_interface(key, asio::io_service & service, protocol::endpoint endpoint, listener & listener, std::shared_ptr<spdlog::logger> logger); + control_interface(key, asio::io_service & service, protocol::endpoint endpoint, listener & listener); std::error_code start(); std::error_code shutdown(); @@ -47,7 +47,7 @@ namespace wanda private: void perform_accept(); - friend pointer make_interface(asio::io_service & service, std::filesystem::path file, control_interface::listener & listener, std::shared_ptr<spdlog::logger> logger); + friend pointer make_interface(asio::io_service & service, std::filesystem::path file, control_interface::listener & listener); asio::io_service & m_service; protocol::endpoint m_endpoint; @@ -56,10 +56,9 @@ namespace wanda listener & m_listener; socket_deleter m_deleter{m_endpoint.path()}; std::set<control_connection::pointer> m_connections; - std::shared_ptr<spdlog::logger> m_logger; }; - control_interface::pointer make_interface(asio::io_service & service, std::filesystem::path file, control_interface::listener & listener, std::shared_ptr<spdlog::logger> logger); + control_interface::pointer make_interface(asio::io_service & service, std::filesystem::path file, control_interface::listener & listener); } // namespace wanda diff --git a/src/logging.cpp b/src/logging.cpp new file mode 100644 index 0000000..fc8bf8b --- /dev/null +++ b/src/logging.cpp @@ -0,0 +1,21 @@ +#include "logging.hpp" + +namespace wanda +{ + std::function<void(spdlog::sink_ptr)> initializer = [](spdlog::sink_ptr sink) { + spdlog::register_logger(std::make_shared<spdlog::logger>("wanda", sink)); + initializer = [](auto) {}; + }; + + void initialize_logger(spdlog::sink_ptr sink) + { + initializer(sink); + } + + logger_ptr get_logger() + { + initialize_logger(); + return spdlog::get("wanda"); + } + +} // namespace wanda
\ No newline at end of file diff --git a/src/logging.hpp b/src/logging.hpp new file mode 100644 index 0000000..46432b8 --- /dev/null +++ b/src/logging.hpp @@ -0,0 +1,18 @@ +#ifndef WANDA_LOGGING_HPP +#define WANDA_LOGGING_HPP + +#include <spdlog/sinks/null_sink.h> +#include <spdlog/spdlog.h> + +#include <memory> + +namespace wanda +{ + using logger_ptr = std::shared_ptr<spdlog::logger>; + + void initialize_logger(spdlog::sink_ptr sink = std::make_shared<spdlog::sinks::null_sink_st>()); + + logger_ptr get_logger(); +} // namespace wanda + +#endif
\ No newline at end of file diff --git a/src/wallpaper.cpp b/src/wallpaper.cpp index f0ae468..b4e0b76 100644 --- a/src/wallpaper.cpp +++ b/src/wallpaper.cpp @@ -1,10 +1,11 @@ -#include "wallpaper.hpp" +#include "logging.hpp" #include "optional.hpp" #include "setting.hpp" +#include "wallpaper.hpp" namespace wanda { - void set_wallpaper(std::filesystem::path wallpaper, std::shared_ptr<spdlog::logger> logger) + void set_wallpaper(std::filesystem::path wallpaper) { using namespace wanda::literals; using namespace wanda::std_ext; @@ -14,9 +15,9 @@ namespace wanda with(setting["picture-uri"_key], [&](auto & value) { value = "file://" + wallpaper.native(); }) || - [&] { logger->error("invalid settings key"); }; + [&] { get_logger()->error("invalid settings key"); }; }) || - [&] { logger->error("invalid setting"); }; + [&] { get_logger()->error("invalid setting"); }; } } // namespace wanda
\ No newline at end of file diff --git a/src/wallpaper.hpp b/src/wallpaper.hpp index 059a2f8..988ab78 100644 --- a/src/wallpaper.hpp +++ b/src/wallpaper.hpp @@ -9,7 +9,7 @@ namespace wanda { - void set_wallpaper(std::filesystem::path wallpaper, std::shared_ptr<spdlog::logger> logger); + void set_wallpaper(std::filesystem::path wallpaper); } // namespace wanda #endif
\ No newline at end of file diff --git a/src/wandac.cpp b/src/wandac.cpp index 3a9531a..e8be1ef 100644 --- a/src/wandac.cpp +++ b/src/wandac.cpp @@ -1,6 +1,7 @@ #include "command.hpp" #include "commander.hpp" #include "environment.hpp" +#include "logging.hpp" #include "xdg.hpp" #include <asio.hpp> @@ -50,9 +51,8 @@ struct cli struct listener : wanda::commander::listener { - listener(::cli & cli, std::shared_ptr<spdlog::logger> logger) - : m_logger{logger} - , m_cli{cli} + listener(::cli & cli) + : m_cli{cli} { } @@ -66,7 +66,6 @@ struct listener : wanda::commander::listener } private: - std::shared_ptr<spdlog::logger> m_logger; ::cli & m_cli; }; @@ -83,14 +82,15 @@ int main(int argc, char const * const * argv) return EXIT_SUCCESS; } - auto log = spdlog::stdout_color_mt("wandac"); + wanda::initialize_logger(std::make_shared<spdlog::sinks::stderr_color_sink_st>()); + auto interface = wanda::xdg_path_for(wanda::xdg_directory::runtime_dir, wanda::environment{}) / ".wanda_interface"; auto service = asio::io_service{}; - auto listener = ::listener{cli, log}; + auto listener = ::listener{cli}; - auto commander = wanda::commander{service, interface, listener, log}; + auto commander = wanda::commander{service, interface, listener}; - log->info("trying to connect to wanda control interface on '{}'", interface.native()); + wanda::get_logger()->info("trying to connect to wanda control interface on '{}'", interface.native()); commander.start(); service.run(); diff --git a/src/wandad.cpp b/src/wandad.cpp index e955beb..7321f04 100644 --- a/src/wandad.cpp +++ b/src/wandad.cpp @@ -2,15 +2,14 @@ #include "control_interface.hpp" #include "environment.hpp" #include "filesystem.hpp" +#include "logging.hpp" #include "optional.hpp" #include "setting.hpp" #include "wallpaper.hpp" #include "xdg.hpp" #include <asio.hpp> - #include <spdlog/sinks/stdout_color_sinks.h> -#include <spdlog/spdlog.h> #include <csignal> #include <set> @@ -34,9 +33,8 @@ namespace struct listener : wanda::control_interface::listener { - listener(std::vector<std::filesystem::path> const & wallpapers, std::shared_ptr<spdlog::logger> logger) + listener(std::vector<std::filesystem::path> const & wallpapers) : m_wallpapers{wallpapers} - , m_logger{logger} { } @@ -47,18 +45,17 @@ namespace case wanda::command_id::change: { auto wallpaper = wanda::random_pick(m_wallpapers); - m_logger->info("changing wallpaper to '{}'", wallpaper.native()); - wanda::set_wallpaper(wallpaper, m_logger); + wanda::get_logger()->info("changing wallpaper to '{}'", wallpaper.native()); + wanda::set_wallpaper(wallpaper); break; } default: - m_logger->error("received unknown command '{}'", static_cast<int>(command.id)); + wanda::get_logger()->error("received unknown command '{}'", static_cast<int>(command.id)); } } private: std::vector<std::filesystem::path> const & m_wallpapers; - std::shared_ptr<spdlog::logger> m_logger; }; } // namespace @@ -67,20 +64,20 @@ int main() { using namespace wanda::std_ext; - auto log = spdlog::stdout_color_mt("wandad"); - log->info("wanda is starting up"); + wanda::initialize_logger(std::make_shared<spdlog::sinks::stdout_color_sink_st>()); + wanda::get_logger()->info("wanda is starting up"); with(wanda::scan({"/usr/share/backgrounds"}, image_filter), [&](auto const & list) { auto service = asio::io_service{}; auto socket_path = wanda::xdg_path_for(wanda::xdg_directory::runtime_dir, wanda::environment{}) / ".wanda_interface"; - log->info("starting control interface on '{}'", socket_path.native()); - auto listener = ::listener{list, log}; - auto interface = wanda::make_interface(service, socket_path, listener, log); + wanda::get_logger()->info("starting control interface on '{}'", socket_path.native()); + auto listener = ::listener{list}; + auto interface = wanda::make_interface(service, socket_path, listener); if (!interface) { - log->error("failed to start control interface"); + wanda::get_logger()->error("failed to start control interface"); return; } @@ -99,9 +96,8 @@ int main() }); auto wallpaper = wanda::random_pick(list); - wanda::set_wallpaper(wallpaper, log); + wanda::set_wallpaper(wallpaper); service.run(); - }) || - [&] { log->error("wallpaper directory does not exist"); }; + }) || [&] { wanda::get_logger()->error("wallpaper directory does not exist"); }; } diff --git a/src/xdg.hpp b/src/xdg.hpp index b57a34f..8010b7b 100644 --- a/src/xdg.hpp +++ b/src/xdg.hpp @@ -9,6 +9,9 @@ namespace wanda { + /** + * @brief An @p enum to represet the standardized XDG directories + */ enum struct xdg_directory : std::underlying_type_t<std::byte> { data_home, @@ -17,8 +20,14 @@ namespace wanda runtime_dir, }; + /** + * @brief Get the name of the environment variable associated with the given XDG directory + */ std::string xdg_variable(xdg_directory directory); + /** + * @brief Get the path to the given @p directory given the provided @p environment + */ std::filesystem::path xdg_path_for(xdg_directory directory, environment const & environment); } // namespace wanda |
