Skip to content

Commit

Permalink
feat: log more detailed info to log files
Browse files Browse the repository at this point in the history
  • Loading branch information
ShelpAm committed Nov 19, 2024
1 parent f2da9c9 commit 18a3cce
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 12 deletions.
16 changes: 10 additions & 6 deletions src/client/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,8 @@ void Application::process_event(Event const &event)
void Application::add_to_show(std::string message)
{
using namespace std::chrono_literals;
_messages.emplace(current_time() + 10s, std::move(message));
constexpr auto lasting_time{10s};
_messages.emplace(current_time() + lasting_time, std::move(message));
}

void Application::async_request(Command const &command,
Expand Down Expand Up @@ -394,12 +395,15 @@ void Application::render_unlogged_in()
_state = State::logging;
}
catch (std::runtime_error const &e) {
if (e.what() == std::string_view{"Connection refused"}) {
add_to_show("Cannot connect to the server. Maybe server is down, or "
"your input is incorrect.");
return;
if (e.what() == "Connection refused"sv) {
add_to_show("Cannot connect to the server. Connection refused.");
}
else if (e.what() == "resolve: Host not found (authoritative)"sv) {
add_to_show(
"Cannot connect to the server. Host not found. Please check "
"whether the server is down, or you mistyped the host.");
}
throw;
spdlog::error(e.what());
}
}
}
4 changes: 2 additions & 2 deletions src/client/little-sb-client.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "application.h"
#include "log.h"
#include "window.h"
#include <spdlog/spdlog.h>

auto main(int argc, char **argv) -> int
{
Expand All @@ -13,7 +13,7 @@ auto main(int argc, char **argv) -> int
return 1;
}

spdlog::set_level(spdlog::level::info);
log_to_console_and_file();
Window::initialize();

#ifdef NDEBUG
Expand Down
2 changes: 1 addition & 1 deletion src/client/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void Window::deinitialize()
}

Window::Window()
: _window{glfwCreateWindow(1440, 960, "Little sb", nullptr, nullptr)}
: _window{glfwCreateWindow(1600, 1200, "Little sb", nullptr, nullptr)}
{
if (_window == nullptr) {
throw "Window or OpenGL context creation failed";
Expand Down
21 changes: 21 additions & 0 deletions src/log.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "log.h"
#include <memory>
#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/spdlog.h>

void log_to_console_and_file()
{
auto console_sink{std::make_shared<spdlog::sinks::stdout_color_sink_mt>()};
console_sink->set_level(spdlog::level::warn);

auto file_sink{std::make_shared<spdlog::sinks::basic_file_sink_mt>(
"logs/multisink.txt", true)};
file_sink->set_level(spdlog::level::trace);

auto logger{std::make_shared<spdlog::logger>(
"", spdlog::sinks_init_list{console_sink, file_sink})};

spdlog::default_logger()->name();
spdlog::set_default_logger(logger);
}
3 changes: 3 additions & 0 deletions src/log.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

void log_to_console_and_file();
15 changes: 12 additions & 3 deletions src/server/little-sb-server.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
#include "log.h"
#include "server.h"
#include <csignal>
#include <spdlog/spdlog.h>
#include <memory>
#include <span>

void signal_handler(int /*signal*/)
{
// Delete entire server
Server::instance().shutdown();
}

auto main(int /*argc*/, char * /*argv*/[]) -> int
auto main(int argc, char **argv) -> int
{
if (argc == 0) {
std::abort();
return -1;
}

std::span args{argv, static_cast<std::size_t>(argc - 1)};

// Signal handler for SIGINT
std::signal(SIGINT, signal_handler);
spdlog::set_level(spdlog::level::info); // Set for debugging
log_to_console_and_file();

#ifdef NDEBUG
// Disable try-catch in DEBUG mode to allow the debugger to catch and display
Expand Down

0 comments on commit 18a3cce

Please sign in to comment.