Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,6 @@ jobs:
steps:
- uses: actions/checkout@v4

- name: Restore Conan Cache
id: conan-cache-restore
uses: actions/cache/restore@v4
with:
path: |
/home/runner/.conan2
/home/runner/work/spectator-cpp/spectator-cpp/cmake-build
key: ${{ runner.os }}-conan

- name: Install System Dependencies
run: |
sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
Expand All @@ -32,12 +23,3 @@ jobs:
./setup-venv.sh
source venv/bin/activate
./build.sh

- name: Save Conan Cache
id: conan-cache-save
uses: actions/cache/save@v4
with:
path: |
/home/runner/.conan2
/home/runner/work/spectator-cpp/spectator-cpp/cmake-build
key: ${{ steps.conan-cache-restore.outputs.cache-primary-key }}
4 changes: 4 additions & 0 deletions Dockerfiles/Ubuntu.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ RUN apt-get update && apt-get install -y \
cmake \
build-essential

# Set up alternatives to make gcc-13 and g++-13 the default
RUN update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-13 100 && \
update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-13 100

# Create a default working directory
WORKDIR /home/ubuntu/spectator-cpp

Expand Down
2 changes: 2 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ echo "CXX=$CXX"
if [[ ! -f "$HOME/.conan2/profiles/default" ]]; then
echo -e "${BLUE}==== create default profile ====${NC}"
conan profile detect
# Set C++ standard to 20 so spdlog uses std_format
sed -i 's/compiler\.cppstd=.*/compiler.cppstd=20/' "$HOME/.conan2/profiles/default"
fi

if [[ ! -d $BUILD_DIR ]]; then
Expand Down
6 changes: 6 additions & 0 deletions conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ class SpectatorCppConan(ConanFile):
)
tool_requires = ()
generators = "CMakeDeps", "CMakeToolchain"

def configure(self):
# Configure spdlog to be header-only
self.options["spdlog"].header_only = True
self.options["spdlog"].use_std_fmt = True

9 changes: 5 additions & 4 deletions libs/logger/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
add_library(spectator-logger INTERFACE)
add_library(spectator-logger logger.cpp)

target_include_directories(spectator-logger
INTERFACE
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
)

target_link_libraries(spectator-logger
INTERFACE
PUBLIC
spectator-utils
spdlog::spdlog
PRIVATE
spdlog::spdlog_header_only
)
68 changes: 68 additions & 0 deletions libs/logger/logger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include "logger.h"

#include <iostream>
#include <stdexcept>
#include <spdlog/spdlog.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/async.h>

constexpr const char* kMainLogger = "spectator";

class Logger::LoggerImpl
{
public:
std::shared_ptr<spdlog::logger> m_logger;

LoggerImpl()
{
try
{
m_logger = spdlog::create_async_nb<spdlog::sinks::ansicolor_stdout_sink_mt>(kMainLogger);
if (m_logger == nullptr)
{
throw std::runtime_error("Failed to create logger: spdlog returned null");
}
}
catch (const spdlog::spdlog_ex& ex)
{
throw std::runtime_error("Log initialization failed: " + std::string(ex.what()));
}
}

~LoggerImpl() = default;

spdlog::logger* GetLogger() const
{
return m_logger.get();
}
};

Logger::Logger() : m_impl(std::make_unique<LoggerImpl>())
{
}

Logger::~Logger() = default;

void Logger::debug(const std::string& msg)
{
auto* logger = GetInstance().m_impl->GetLogger();
logger->debug(msg);
}

void Logger::info(const std::string& msg)
{
auto* logger = GetInstance().m_impl->GetLogger();
logger->info(msg);
}

void Logger::warn(const std::string& msg)
{
auto* logger = GetInstance().m_impl->GetLogger();
logger->warn(msg);
}

void Logger::error(const std::string& msg)
{
auto* logger = GetInstance().m_impl->GetLogger();
logger->error(msg);
}
72 changes: 19 additions & 53 deletions libs/logger/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,87 +2,53 @@

#include <singleton.h>

#include <iostream>
#include <memory>
#include <string>

#include <spdlog/spdlog.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/async.h>
#include <fmt/core.h>

constexpr const char* kMainLogger = "spectator";
#include <format>

class Logger final : public Singleton<Logger>
{
private:
std::shared_ptr<spdlog::logger> m_logger;
// Forward declaration for pimpl
class LoggerImpl;
std::unique_ptr<LoggerImpl> m_impl;

friend class Singleton<Logger>;

Logger()
{
try
{
m_logger = spdlog::create_async_nb<spdlog::sinks::ansicolor_stdout_sink_mt>(kMainLogger);
}
catch (const spdlog::spdlog_ex& ex)
{
std::cerr << "Log initialization failed: " << ex.what() << "\n";
m_logger = nullptr;
}
}

~Logger() = default;
Logger();
~Logger();
Logger(const Logger&) = delete;
Logger& operator=(const Logger&) = delete;
Logger(Logger&&) = delete;
Logger& operator=(Logger&&) = delete;

public:
static spdlog::logger* GetLogger() { return GetInstance().m_logger.get(); }

static void debug(const std::string& msg)
{
GetLogger()->debug(msg);
}

static void info(const std::string& msg)
{
GetLogger()->info(msg);
}

static void warn(const std::string& msg)
{
GetLogger()->warn(msg);
}

static void error(const std::string& msg)
{
GetLogger()->error(msg);
}
static void debug(const std::string& msg);
static void info(const std::string& msg);
static void warn(const std::string& msg);
static void error(const std::string& msg);

template <typename... Args>
static void debug(fmt::format_string<Args...> fmt, Args&&... args)
static void debug(std::format_string<Args...> fmt, Args&&... args)
{
GetLogger()->debug(fmt, std::forward<Args>(args)...);
debug(std::format(fmt, std::forward<Args>(args)...));
}

template <typename... Args>
static void info(fmt::format_string<Args...> fmt, Args&&... args)
static void info(std::format_string<Args...> fmt, Args&&... args)
{
GetLogger()->info(fmt, std::forward<Args>(args)...);
info(std::format(fmt, std::forward<Args>(args)...));
}

template <typename... Args>
static void warn(fmt::format_string<Args...> fmt, Args&&... args)
static void warn(std::format_string<Args...> fmt, Args&&... args)
{
GetLogger()->warn(fmt, std::forward<Args>(args)...);
warn(std::format(fmt, std::forward<Args>(args)...));
}

template <typename... Args>
static void error(fmt::format_string<Args...> fmt, Args&&... args)
static void error(std::format_string<Args...> fmt, Args&&... args)
{
GetLogger()->error(fmt, std::forward<Args>(args)...);
error(std::format(fmt, std::forward<Args>(args)...));
}
};
};
12 changes: 2 additions & 10 deletions libs/writer/writer_types/include/udp_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include <memory>
#include <string>
#include <boost/asio.hpp>

class UDPWriter final : public BaseWriter
{
Expand All @@ -15,13 +14,6 @@ class UDPWriter final : public BaseWriter
void Close() override;

private:
std::string m_host;
int m_port;
std::unique_ptr<boost::asio::io_context> m_io_context;
std::unique_ptr<boost::asio::ip::udp::socket> m_socket;
boost::asio::ip::udp::endpoint m_endpoint;
bool m_socketEstablished;

bool CreateSocket();
bool TryToSend(const std::string& message);
class Impl;
std::unique_ptr<Impl> m_pImpl;
};
11 changes: 2 additions & 9 deletions libs/writer/writer_types/include/uds_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <base_writer.h>

#include <string>
#include <boost/asio.hpp>
#include <memory>

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

private:
std::string m_socketPath;
std::unique_ptr<boost::asio::io_context> m_ioContext;
std::unique_ptr<boost::asio::local::datagram_protocol::socket> m_socket;
boost::asio::local::datagram_protocol::endpoint m_endpoint;
bool m_socketEstablished;

bool CreateSocket();
bool TryToSend(const std::string& message);
class Impl;
std::unique_ptr<Impl> m_pImpl;
};
Loading