Skip to content

Commit

Permalink
better logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jstkdng committed Feb 9, 2023
1 parent 6d00543 commit 936005f
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 22 deletions.
4 changes: 1 addition & 3 deletions include/display.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#ifndef __DISPLAY__
#define __DISPLAY__

#include "logging.hpp"
#include "image.hpp"
#include "terminal.hpp"

Expand All @@ -31,7 +30,7 @@
class Display
{
public:
Display(Logging &logger);
Display();
~Display();

void load_image(std::string const& filename, int width, int height);
Expand All @@ -53,7 +52,6 @@ class Display
xcb_connection_t *connection;
xcb_screen_t *screen;

Logging &logger;
std::unique_ptr<Image> image;
std::unique_ptr<std::thread> event_handler;
std::unordered_map<int, std::unique_ptr<Terminal>> terminals;
Expand Down
29 changes: 19 additions & 10 deletions include/logging.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,31 @@

class Logging
{
private:
std::ofstream logfile;
using endl_type = std::ostream&(std::ostream&);

public:
Logging();
~Logging();

template <class T>
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 <class T>
Logging& operator<<(const T& t)
{
this->logfile << t;
std::cout << t;
return *this;
}
};

template <class T>
void Logging::log(T t)
{
std::cout << t << std::endl;
this->logfile << t << std::endl;
}
static Logging logger;

#endif
11 changes: 5 additions & 6 deletions src/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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"]);
Expand All @@ -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();
Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

#include "image.hpp"
#include "logging.hpp"

#include <memory>
#include <stdexcept>
Expand All @@ -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();
}
Expand Down
3 changes: 1 addition & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
1 change: 0 additions & 1 deletion src/terminal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include <unistd.h>
#include <sys/ioctl.h>
#include <cmath>
#include <iostream>

Terminal::Terminal(ProcessInfo pid,
xcb_window_t const& parent,
Expand Down

0 comments on commit 936005f

Please sign in to comment.