From 936005ffb07e04a8b82f644d1b3f4896dac6f587 Mon Sep 17 00:00:00 2001 From: JustKidding Date: Thu, 9 Feb 2023 13:27:34 -0500 Subject: [PATCH] better logging --- include/display.hpp | 4 +--- include/logging.hpp | 29 +++++++++++++++++++---------- src/display.cpp | 11 +++++------ src/image.cpp | 2 ++ src/main.cpp | 3 +-- src/terminal.cpp | 1 - 6 files changed, 28 insertions(+), 22 deletions(-) diff --git a/include/display.hpp b/include/display.hpp index f148019..65543be 100644 --- a/include/display.hpp +++ b/include/display.hpp @@ -17,7 +17,6 @@ #ifndef __DISPLAY__ #define __DISPLAY__ -#include "logging.hpp" #include "image.hpp" #include "terminal.hpp" @@ -31,7 +30,7 @@ class Display { public: - Display(Logging &logger); + Display(); ~Display(); void load_image(std::string const& filename, int width, int height); @@ -53,7 +52,6 @@ class Display xcb_connection_t *connection; xcb_screen_t *screen; - Logging &logger; std::unique_ptr image; std::unique_ptr event_handler; std::unordered_map> terminals; diff --git a/include/logging.hpp b/include/logging.hpp index 6bc02ef..93ef967 100644 --- a/include/logging.hpp +++ b/include/logging.hpp @@ -22,22 +22,31 @@ class Logging { +private: + std::ofstream logfile; + using endl_type = std::ostream&(std::ostream&); + public: Logging(); ~Logging(); - template - void log(T t); + //Overload for std::endl only: + Logging& operator<<(endl_type endl) + { + this->logfile << endl; + std::cout << endl; + return *this; + } -private: - std::ofstream logfile; + template + Logging& operator<<(const T& t) + { + this->logfile << t; + std::cout << t; + return *this; + } }; -template -void Logging::log(T t) -{ - std::cout << t << std::endl; - this->logfile << t << std::endl; -} +static Logging logger; #endif diff --git a/src/display.cpp b/src/display.cpp index fcd169a..a7d161a 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -24,11 +24,11 @@ #include "os.hpp" #include "util.hpp" #include "free_delete.hpp" +#include "logging.hpp" using json = nlohmann::json; -Display::Display(Logging &logger): -logger(logger) +Display::Display() { // connect to server this->connection = xcb_connect(nullptr, nullptr); @@ -61,10 +61,10 @@ auto Display::action(std::string const& cmd) -> void try { j = json::parse(cmd); } catch (json::parse_error const& e) { - logger.log("There was an error parsing the command."); + logger << "There was an error parsing the command." << std::endl; return; } - logger.log(j.dump()); + logger << "Command received: " << j.dump() << std::endl; if (j["action"] == "add") { for (const auto& [key, value]: this->terminals) { value->create_window(j["x"], j["y"], j["max_width"], j["max_height"]); @@ -79,7 +79,7 @@ auto Display::action(std::string const& cmd) -> void try { this->load_image(j["path"], dimensions.first, dimensions.second); } catch (const std::runtime_error& error) { - logger.log(error.what()); + logger << error.what() << std::endl;; } } else { this->destroy_image(); @@ -104,7 +104,6 @@ auto Display::set_parent_terminals() -> void return; } - std::cout << client_pids.size() << std::endl; for (const auto& pid: client_pids) { // calculate a map with parent's pid and window id auto ppids = util::get_parent_pids(pid); diff --git a/src/image.cpp b/src/image.cpp index 1d2ad28..9c4c96b 100644 --- a/src/image.cpp +++ b/src/image.cpp @@ -15,6 +15,7 @@ // along with this program. If not, see . #include "image.hpp" +#include "logging.hpp" #include #include @@ -31,6 +32,7 @@ screen(screen), max_width(width), max_height(height) { + logger << "Loading file " << filename << std::endl; this->load(filename); this->create_xcb_image(); } diff --git a/src/main.cpp b/src/main.cpp index 8bf8620..38cf1a4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -54,8 +54,7 @@ int main(int argc, char *argv[]) freopen("/dev/null", "w", stdout); } - Logging logger; - Display display(logger); + Display display; std::string cmd; diff --git a/src/terminal.cpp b/src/terminal.cpp index 716526c..c890ff6 100644 --- a/src/terminal.cpp +++ b/src/terminal.cpp @@ -21,7 +21,6 @@ #include #include #include -#include Terminal::Terminal(ProcessInfo pid, xcb_window_t const& parent,