Use seperate spdlog loggers for components

Later on base/ might have a function to create loggers for it, but for now using the global logger for base/ components is fine.
This commit is contained in:
Lily Tsuru 2024-03-10 06:51:20 -04:00
parent 919490a8c0
commit 315a3927f3
8 changed files with 31 additions and 15 deletions

View File

@ -3,7 +3,6 @@
#include <boost/asio/read.hpp> #include <boost/asio/read.hpp>
#include <boost/asio/write.hpp> #include <boost/asio/write.hpp>
#include <spdlog/spdlog.h>
#include "DirtySockServer.hpp" #include "DirtySockServer.hpp"
@ -32,7 +31,7 @@ namespace ls {
// Sanity check. I don't expect game payloa // Sanity check. I don't expect game payloa
if(header.payloadSize > MAX_PAYLOAD_SIZE) { if(header.payloadSize > MAX_PAYLOAD_SIZE) {
spdlog::error("WOAH! Message size {} MB larger than {}MB..", (static_cast<float>(header.payloadSize) / 1024 / 1024), (static_cast<float>(MAX_PAYLOAD_SIZE) / 1024 / 1024)); logger->error("WOAH! Message size {} MB larger than {}MB..", (static_cast<float>(header.payloadSize) / 1024 / 1024), (static_cast<float>(MAX_PAYLOAD_SIZE) / 1024 / 1024));
co_return nullptr; co_return nullptr;
} }
@ -50,7 +49,7 @@ namespace ls {
co_return MessageFactory::CreateAndParseMessage(header, propertyBuffer); co_return MessageFactory::CreateAndParseMessage(header, propertyBuffer);
} catch(bsys::system_error& ec) { } catch(bsys::system_error& ec) {
if(ec.code() != asio::error::operation_aborted) if(ec.code() != asio::error::operation_aborted)
spdlog::error("Error in DirtySockClient::WriteMessage(): {}", ec.what()); logger->error("Error in DirtySockClient::WriteMessage(): {}", ec.what());
co_return nullptr; co_return nullptr;
} }
} }
@ -64,7 +63,7 @@ namespace ls {
co_await asio::async_write(stream, asio::buffer(buf), asio::deferred); co_await asio::async_write(stream, asio::buffer(buf), asio::deferred);
} catch(bsys::system_error& ec) { } catch(bsys::system_error& ec) {
if(ec.code() != asio::error::operation_aborted) if(ec.code() != asio::error::operation_aborted)
spdlog::error("Error in DirtySockClient::WriteMessage(): {}", ec.what()); logger->error("Error in DirtySockClient::WriteMessage(): {}", ec.what());
} }
} }
@ -78,14 +77,14 @@ namespace ls {
co_await message->Process(shared_from_this()); co_await message->Process(shared_from_this());
} else { } else {
// This will occur if parsing fails or etc. // This will occur if parsing fails or etc.
spdlog::error("Error parsing message, closing connection"); logger->error("Error parsing message, closing connection");
Close(); Close();
co_return; co_return;
} }
} }
} catch(bsys::system_error& ec) { } catch(bsys::system_error& ec) {
if(ec.code() != asio::error::operation_aborted) if(ec.code() != asio::error::operation_aborted)
spdlog::error("Error in DirtySockClient::Run(): {}", ec.what()); logger->error("Error in DirtySockClient::Run(): {}", ec.what());
} }
} }

View File

@ -1,4 +1,6 @@
#pragma once #pragma once
#include <spdlog/spdlog.h>
#include <base/types.hpp> #include <base/types.hpp>
#include <deque> #include <deque>
#include <impl/asio_config.hpp> #include <impl/asio_config.hpp>
@ -29,11 +31,12 @@ namespace ls {
// internal // internal
base::Awaitable<MessagePtr> ReadMessage(); base::Awaitable<MessagePtr> ReadMessage();
base::Awaitable<void> Run(); base::Awaitable<void> Run();
Stream stream; Stream stream;
base::Ref<DirtySockServer> server; base::Ref<DirtySockServer> server;
base::Ref<spdlog::logger> logger = spdlog::get("ls_dsock_client");
}; };
} // namespace ls } // namespace ls

View File

@ -11,7 +11,7 @@ namespace ls {
} }
void DirtySockServer::Start(const Protocol::endpoint& ep) { void DirtySockServer::Start(const Protocol::endpoint& ep) {
asio::co_spawn(exec, Listener(ep), base::DefCoroCompletion("EaServer listener")); asio::co_spawn(exec, Listener(ep), base::DefCoroCompletion("DirtySockServer listener"));
} }
bool DirtySockServer::Listening() const { bool DirtySockServer::Listening() const {
@ -34,7 +34,7 @@ namespace ls {
acceptor.bind(endpoint); acceptor.bind(endpoint);
acceptor.listen(asio::socket_base::max_listen_connections); acceptor.listen(asio::socket_base::max_listen_connections);
spdlog::info("DirtySockServer listening on {}:{}", endpoint.address().to_string(), endpoint.port()); logger->info("DirtySockServer listening on {}:{}", endpoint.address().to_string(), endpoint.port());
while(true) { while(true) {
auto socket = co_await acceptor.async_accept(asio::deferred); auto socket = co_await acceptor.async_accept(asio::deferred);
@ -44,7 +44,7 @@ namespace ls {
} }
} catch(bsys::system_error& ec) { } catch(bsys::system_error& ec) {
if(ec.code() != asio::error::operation_aborted) if(ec.code() != asio::error::operation_aborted)
spdlog::error("Error in DirtySockServer::Listener(): {}", ec.what()); logger->error("Error in DirtySockServer::Listener(): {}", ec.what());
} }
co_return; co_return;

View File

@ -4,6 +4,7 @@
#include <impl/asio_config.hpp> #include <impl/asio_config.hpp>
#include <memory> #include <memory>
#include <set> #include <set>
#include <spdlog/spdlog.h>
namespace ls { namespace ls {
struct DirtySockClient; struct DirtySockClient;
@ -38,7 +39,7 @@ namespace ls {
std::set<base::Ref<DirtySockClient>> clientSet; std::set<base::Ref<DirtySockClient>> clientSet;
// i'm moving to spdlog fuck this base::Ref<spdlog::logger> logger = spdlog::get("ls_dsock_server");
}; };

View File

@ -22,12 +22,14 @@ namespace ls {
if(!lobbyServer->Listening()) { if(!lobbyServer->Listening()) {
// uh oh worm.. // uh oh worm..
spdlog::error("for some reason lobby server isnt listening.."); logger->error("for some reason lobby server isnt listening..");
co_return; co_return;
} }
// TODO: http server? there's apparently some stuff we can have that uses it // TODO: http server? there's apparently some stuff we can have that uses it
logger->info("SSX3LobbyServer started successfully!");
// wait to stop // wait to stop
co_await stopCv.Wait([&]() { return stopping; }); co_await stopCv.Wait([&]() { return stopping; });

View File

@ -4,6 +4,8 @@
#include <impl/asio_config.hpp> #include <impl/asio_config.hpp>
#include <set> #include <set>
#include <spdlog/spdlog.h>
namespace ls { namespace ls {
struct DirtySockServer; struct DirtySockServer;
@ -32,8 +34,7 @@ namespace ls {
Config config; Config config;
base::Ref<spdlog::logger> logger = spdlog::get("ls_server");
}; };
} // namespace ls } // namespace ls

View File

@ -6,8 +6,10 @@
#include <toml++/toml.hpp> #include <toml++/toml.hpp>
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
#include <spdlog/cfg/env.h>
#include "Server.hpp" #include "Server.hpp"
#include "spdlog/sinks/stdout_color_sinks.h"
asio::io_context ioc(1); asio::io_context ioc(1);
base::Unique<ls::Server> server; base::Unique<ls::Server> server;
@ -36,6 +38,14 @@ base::Awaitable<void> CoMain(const ls::Server::Config& config) {
} }
int main() { int main() {
// create spdlog loggers
spdlog::create<spdlog::sinks::stdout_color_sink_mt>("ls_server");
spdlog::create<spdlog::sinks::stdout_color_sink_mt>("ls_dsock_client");
spdlog::create<spdlog::sinks::stdout_color_sink_mt>("ls_dsock_server");
spdlog::cfg::load_env_levels();
auto config = ls::Server::Config {}; auto config = ls::Server::Config {};
try { try {

View File

@ -1,5 +1,5 @@
add_subdirectory(boost) add_subdirectory(boost)
add_subdirectory(tomlplusplus) add_subdirectory(tomlplusplus)
#set(SPDLOG_USE_STD_FORMAT ON) set(SPDLOG_USE_STD_FORMAT ON)
add_subdirectory(spdlog) add_subdirectory(spdlog)