Skip to content

Commit d45fe3c

Browse files
committed
Save work
1 parent 1e69c44 commit d45fe3c

9 files changed

Lines changed: 224 additions & 97 deletions

File tree

libs/logger/CMakeLists.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
add_library(spectator-logger INTERFACE)
1+
add_library(spectator-logger logger.cpp)
22

33
target_include_directories(spectator-logger
4-
INTERFACE
4+
PUBLIC
55
${CMAKE_CURRENT_SOURCE_DIR}
66
)
77

88
target_link_libraries(spectator-logger
9-
INTERFACE
9+
PUBLIC
1010
spectator-utils
11+
PRIVATE
1112
spdlog::spdlog
1213
)

libs/logger/logger.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include "logger.h"
2+
3+
#include <iostream>
4+
#include <stdexcept>
5+
#include <spdlog/spdlog.h>
6+
#include <spdlog/sinks/stdout_color_sinks.h>
7+
#include <spdlog/async.h>
8+
9+
constexpr const char* kMainLogger = "spectator";
10+
11+
class Logger::LoggerImpl
12+
{
13+
public:
14+
std::shared_ptr<spdlog::logger> m_logger;
15+
16+
LoggerImpl()
17+
{
18+
try
19+
{
20+
m_logger = spdlog::create_async_nb<spdlog::sinks::ansicolor_stdout_sink_mt>(kMainLogger);
21+
if (m_logger == nullptr)
22+
{
23+
throw std::runtime_error("Failed to create logger: spdlog returned null");
24+
}
25+
}
26+
catch (const spdlog::spdlog_ex& ex)
27+
{
28+
throw std::runtime_error("Log initialization failed: " + std::string(ex.what()));
29+
}
30+
}
31+
32+
~LoggerImpl() = default;
33+
34+
spdlog::logger* GetLogger() const
35+
{
36+
return m_logger.get();
37+
}
38+
};
39+
40+
Logger::Logger() : m_impl(std::make_unique<LoggerImpl>())
41+
{
42+
}
43+
44+
Logger::~Logger() = default;
45+
46+
void Logger::debug(const std::string& msg)
47+
{
48+
auto* logger = GetInstance().m_impl->GetLogger();
49+
logger->debug(msg);
50+
}
51+
52+
void Logger::info(const std::string& msg)
53+
{
54+
auto* logger = GetInstance().m_impl->GetLogger();
55+
logger->info(msg);
56+
}
57+
58+
void Logger::warn(const std::string& msg)
59+
{
60+
auto* logger = GetInstance().m_impl->GetLogger();
61+
logger->warn(msg);
62+
}
63+
64+
void Logger::error(const std::string& msg)
65+
{
66+
auto* logger = GetInstance().m_impl->GetLogger();
67+
logger->error(msg);
68+
}

libs/logger/logger.h

Lines changed: 19 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,87 +2,53 @@
22

33
#include <singleton.h>
44

5-
#include <iostream>
65
#include <memory>
76
#include <string>
8-
9-
#include <spdlog/spdlog.h>
10-
#include <spdlog/sinks/stdout_color_sinks.h>
11-
#include <spdlog/async.h>
12-
#include <fmt/core.h>
13-
14-
constexpr const char* kMainLogger = "spectator";
7+
#include <format>
158

169
class Logger final : public Singleton<Logger>
1710
{
1811
private:
19-
std::shared_ptr<spdlog::logger> m_logger;
12+
// Forward declaration for pimpl
13+
class LoggerImpl;
14+
std::unique_ptr<LoggerImpl> m_impl;
2015

2116
friend class Singleton<Logger>;
2217

23-
Logger()
24-
{
25-
try
26-
{
27-
m_logger = spdlog::create_async_nb<spdlog::sinks::ansicolor_stdout_sink_mt>(kMainLogger);
28-
}
29-
catch (const spdlog::spdlog_ex& ex)
30-
{
31-
std::cerr << "Log initialization failed: " << ex.what() << "\n";
32-
m_logger = nullptr;
33-
}
34-
}
35-
36-
~Logger() = default;
18+
Logger();
19+
~Logger();
3720
Logger(const Logger&) = delete;
3821
Logger& operator=(const Logger&) = delete;
3922
Logger(Logger&&) = delete;
4023
Logger& operator=(Logger&&) = delete;
4124

4225
public:
43-
static spdlog::logger* GetLogger() { return GetInstance().m_logger.get(); }
44-
45-
static void debug(const std::string& msg)
46-
{
47-
GetLogger()->debug(msg);
48-
}
49-
50-
static void info(const std::string& msg)
51-
{
52-
GetLogger()->info(msg);
53-
}
54-
55-
static void warn(const std::string& msg)
56-
{
57-
GetLogger()->warn(msg);
58-
}
59-
60-
static void error(const std::string& msg)
61-
{
62-
GetLogger()->error(msg);
63-
}
26+
static void debug(const std::string& msg);
27+
static void info(const std::string& msg);
28+
static void warn(const std::string& msg);
29+
static void error(const std::string& msg);
6430

6531
template <typename... Args>
66-
static void debug(fmt::format_string<Args...> fmt, Args&&... args)
32+
static void debug(std::format_string<Args...> fmt, Args&&... args)
6733
{
68-
GetLogger()->debug(fmt, std::forward<Args>(args)...);
34+
debug(std::format(fmt, std::forward<Args>(args)...));
6935
}
7036

7137
template <typename... Args>
72-
static void info(fmt::format_string<Args...> fmt, Args&&... args)
38+
static void info(std::format_string<Args...> fmt, Args&&... args)
7339
{
74-
GetLogger()->info(fmt, std::forward<Args>(args)...);
40+
info(std::format(fmt, std::forward<Args>(args)...));
7541
}
7642

7743
template <typename... Args>
78-
static void warn(fmt::format_string<Args...> fmt, Args&&... args)
44+
static void warn(std::format_string<Args...> fmt, Args&&... args)
7945
{
80-
GetLogger()->warn(fmt, std::forward<Args>(args)...);
46+
warn(std::format(fmt, std::forward<Args>(args)...));
8147
}
8248

8349
template <typename... Args>
84-
static void error(fmt::format_string<Args...> fmt, Args&&... args)
50+
static void error(std::format_string<Args...> fmt, Args&&... args)
8551
{
86-
GetLogger()->error(fmt, std::forward<Args>(args)...);
52+
error(std::format(fmt, std::forward<Args>(args)...));
8753
}
88-
};
54+
};

libs/writer/writer_types/include/udp_writer.h

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
#include <memory>
66
#include <string>
7-
#include <boost/asio.hpp>
87

98
class UDPWriter final : public BaseWriter
109
{
@@ -15,13 +14,6 @@ class UDPWriter final : public BaseWriter
1514
void Close() override;
1615

1716
private:
18-
std::string m_host;
19-
int m_port;
20-
std::unique_ptr<boost::asio::io_context> m_io_context;
21-
std::unique_ptr<boost::asio::ip::udp::socket> m_socket;
22-
boost::asio::ip::udp::endpoint m_endpoint;
23-
bool m_socketEstablished;
24-
25-
bool CreateSocket();
26-
bool TryToSend(const std::string& message);
17+
class Impl;
18+
std::unique_ptr<Impl> m_pImpl;
2719
};

libs/writer/writer_types/include/uds_writer.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include <base_writer.h>
44

55
#include <string>
6-
#include <boost/asio.hpp>
76
#include <memory>
87

98
class UDSWriter final : public BaseWriter
@@ -15,12 +14,6 @@ class UDSWriter final : public BaseWriter
1514
void Close() override;
1615

1716
private:
18-
std::string m_socketPath;
19-
std::unique_ptr<boost::asio::io_context> m_ioContext;
20-
std::unique_ptr<boost::asio::local::datagram_protocol::socket> m_socket;
21-
boost::asio::local::datagram_protocol::endpoint m_endpoint;
22-
bool m_socketEstablished;
23-
24-
bool CreateSocket();
25-
bool TryToSend(const std::string& message);
17+
class Impl;
18+
std::unique_ptr<Impl> m_pImpl;
2619
};

libs/writer/writer_types/src/udp_writer.cpp

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,47 @@
11
#include <udp_writer.h>
22

33
#include <logger.h>
4+
#include <boost/asio.hpp>
45

5-
UDPWriter::UDPWriter(const std::string& host, int port) :
6+
class UDPWriter::Impl
7+
{
8+
public:
9+
Impl(const std::string& host, int port);
10+
~Impl() = default;
11+
12+
bool CreateSocket();
13+
bool TryToSend(const std::string& message);
14+
void Close();
15+
16+
std::string m_host;
17+
int m_port;
18+
std::unique_ptr<boost::asio::io_context> m_io_context;
19+
std::unique_ptr<boost::asio::ip::udp::socket> m_socket;
20+
boost::asio::ip::udp::endpoint m_endpoint;
21+
bool m_socketEstablished;
22+
};
23+
24+
UDPWriter::Impl::Impl(const std::string& host, int port) :
625
m_host(host),
726
m_port(port),
827
m_io_context(std::make_unique<boost::asio::io_context>()),
928
m_socket(nullptr),
1029
m_socketEstablished(false)
1130
{
12-
if (false == CreateSocket())
31+
}
32+
33+
UDPWriter::UDPWriter(const std::string& host, int port) :
34+
m_pImpl(std::make_unique<Impl>(host, port))
35+
{
36+
if (false == m_pImpl->CreateSocket())
1337
{
14-
Logger::error("UDPWriter: Failed to create socket for {}:{} during construction", m_host, m_port);
38+
Logger::error("UDPWriter: Failed to create socket for {}:{} during construction", m_pImpl->m_host, m_pImpl->m_port);
1539
}
1640
}
1741

1842
UDPWriter::~UDPWriter() { Close(); }
1943

20-
bool UDPWriter::CreateSocket() try
44+
bool UDPWriter::Impl::CreateSocket() try
2145
{
2246
if (m_socketEstablished)
2347
{
@@ -44,7 +68,7 @@ catch (const boost::system::system_error& ex)
4468
return false;
4569
}
4670

47-
bool UDPWriter::TryToSend(const std::string& message) try
71+
bool UDPWriter::Impl::TryToSend(const std::string& message) try
4872
{
4973
boost::system::error_code ec;
5074
for (int i = 0; i < 3; i++)
@@ -67,20 +91,20 @@ catch (const boost::system::system_error& ex)
6791

6892
void UDPWriter::Write(const std::string& message)
6993
{
70-
if (false == this->m_socketEstablished && false == this->CreateSocket())
94+
if (false == m_pImpl->m_socketEstablished && false == m_pImpl->CreateSocket())
7195
{
72-
Logger::error("UDPWriter: Failed to write message, socket not established {}:{}", m_host, m_port);
96+
Logger::error("UDPWriter: Failed to write message, socket not established {}:{}", m_pImpl->m_host, m_pImpl->m_port);
7397
return;
7498
}
7599

76-
if (TryToSend(message) == false)
100+
if (m_pImpl->TryToSend(message) == false)
77101
{
78102
Logger::error("UDP Writer: Failed to send message: {}", message);
79-
this->Close();
103+
m_pImpl->Close();
80104
}
81105
}
82106

83-
void UDPWriter::Close() try
107+
void UDPWriter::Impl::Close() try
84108
{
85109
this->m_socketEstablished = false;
86110
if(m_socket && m_socket->is_open())
@@ -96,4 +120,12 @@ void UDPWriter::Close() try
96120
catch (const boost::system::system_error& ex)
97121
{
98122
Logger::error("UDP Writer: Boost exception: {}", ex.what());
123+
}
124+
125+
void UDPWriter::Close()
126+
{
127+
if (m_pImpl)
128+
{
129+
m_pImpl->Close();
130+
}
99131
}

0 commit comments

Comments
 (0)