Skip to content

Commit

Permalink
Refactored os.cpp to os_unix.cpp and os_windows.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
gabime committed Jan 14, 2024
1 parent 3954cac commit 885b796
Show file tree
Hide file tree
Showing 9 changed files with 391 additions and 291 deletions.
21 changes: 15 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,11 @@ set(SPDLOG_HEADERS
"include/spdlog/details/periodic_worker.h"
"include/spdlog/details/registry.h"
"include/spdlog/details/synchronous_factory.h"
"include/spdlog/details/tcp_client-windows.h"
"include/spdlog/details/tcp_client.h"

"include/spdlog/details/tcp_client_unix.h"
"include/spdlog/details/thread_pool.h"
"include/spdlog/details/udp_client-windows.h"
"include/spdlog/details/udp_client.h"
"include/spdlog/details/udp_client_windows.h"
"include/spdlog/details/udp_client_unix.h"
"include/spdlog/details/windows_include.h"
"include/spdlog/fmt/bin_to_hex.h"
"include/spdlog/fmt/fmt.h"
Expand Down Expand Up @@ -236,7 +236,6 @@ set(SPDLOG_SRCS
"src/details/file_helper.cpp"
"src/details/log_msg.cpp"
"src/details/log_msg_buffer.cpp"
"src/details/os.cpp"
"src/details/periodic_worker.cpp"
"src/details/registry.cpp"
"src/details/thread_pool.cpp"
Expand All @@ -249,14 +248,24 @@ set(SPDLOG_SRCS
"src/sinks/stdout_sinks.cpp"
"src/sinks/wincolor_sink.cpp")

if(WIN32)
list(APPEND SPDLOG_SRCS "src/details/os_windows.cpp")
list(APPEND SPDLOG_HEADERS "include/spdlog/details/tcp_client_windows.h")
list(APPEND SPDLOG_HEADERS "include/spdlog/details/udp_client_windows.h")
else()
list(APPEND SPDLOG_SRCS "src/details/os_unix.cpp")
list(APPEND SPDLOG_HEADERS "include/spdlog/details/tcp_client_unix.h")
list(APPEND SPDLOG_HEADERS "include/spdlog/details/udp_client_unix.h")
endif()
# Generate spdlog_config.h based on the current configuration
set(OUT_CONFIG_FILE "${CMAKE_CURRENT_SOURCE_DIR}/include/spdlog/spdlog_config.h")
message(STATUS "Generating ${OUT_CONFIG_FILE}")
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/cmake/spdlog_config.h.in" ${OUT_CONFIG_FILE} @ONLY)
list(APPEND SPDLOG_HEADERS ${OUT_CONFIG_FILE})
if(BUILD_SHARED_LIBS)
if(WIN32)
set(VERSION_RC ${CMAKE_CURRENT_BINARY_DIR}/version.rc )
set(VERSION_RC ${CMAKE_CURRENT_BINARY_DIR}/version.rc
src/details/os_unix.cpp)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/version.rc.in ${CMAKE_CURRENT_BINARY_DIR}/version.rc @ONLY)
endif()
add_library(spdlog SHARED ${SPDLOG_SRCS} ${SPDLOG_HEADERS} ${VERSION_RC})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#pragma once

#ifdef _WIN32
#error include tcp_client-windows.h instead
#error include tcp_client_windows.h instead
#endif

// tcp client helper
Expand All @@ -23,7 +23,7 @@

namespace spdlog {
namespace details {
class tcp_client {
class tcp_client_unix {
int socket_ = -1;

public:
Expand All @@ -38,7 +38,7 @@ class tcp_client {

int fd() const { return socket_; }

~tcp_client() { close(); }
~tcp_client_unix() { close(); }

// try to connect or throw on failure
void connect(const std::string &host, int port) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

namespace spdlog {
namespace details {
class tcp_client {
class tcp_client_unix {
SOCKET socket_ = INVALID_SOCKET;

static void init_winsock_() {
Expand All @@ -42,9 +42,9 @@ class tcp_client {
}

public:
tcp_client() { init_winsock_(); }
tcp_client_unix() { init_winsock_(); }

~tcp_client() {
~tcp_client_unix() {
close();
::WSACleanup();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "./os.h"

#ifdef _WIN32
#error "include udp_client-windows.h instead"
#error "include udp_client_windows.h instead"
#endif

#include <arpa/inet.h>
Expand All @@ -26,7 +26,7 @@
namespace spdlog {
namespace details {

class udp_client {
class udp_client_unix {
static constexpr int TX_BUFFER_SIZE = 1024 * 10;
int socket_ = -1;
struct sockaddr_in sockAddr_;
Expand All @@ -39,7 +39,7 @@ class udp_client {
}

public:
udp_client(const std::string &host, uint16_t port) {
udp_client_unix(const std::string &host, uint16_t port) {
socket_ = ::socket(PF_INET, SOCK_DGRAM, 0);
if (socket_ < 0) {
throw_spdlog_ex("error: Create Socket Failed!");
Expand All @@ -63,7 +63,7 @@ class udp_client {
::memset(sockAddr_.sin_zero, 0x00, sizeof(sockAddr_.sin_zero));
}

~udp_client() { cleanup_(); }
~udp_client_unix() { cleanup_(); }

int fd() const { return socket_; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

namespace spdlog {
namespace details {
class udp_client {
class udp_client_unix {
static constexpr int TX_BUFFER_SIZE = 1024 * 10;
SOCKET socket_ = INVALID_SOCKET;
sockaddr_in addr_ = {};
Expand Down Expand Up @@ -55,7 +55,7 @@ class udp_client {
}

public:
udp_client(const std::string &host, uint16_t port) {
udp_client_unix(const std::string &host, uint16_t port) {
init_winsock_();

addr_.sin_family = PF_INET;
Expand Down Expand Up @@ -83,7 +83,7 @@ class udp_client {
}
}

~udp_client() { cleanup_(); }
~udp_client_unix() { cleanup_(); }

SOCKET fd() const { return socket_; }

Expand Down
4 changes: 2 additions & 2 deletions include/spdlog/sinks/tcp_sink.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#ifdef _WIN32
#include "../details/tcp_client-windows.h"
#else
#include "../details/tcp_client.h"
#include "../details/tcp_client_unix.h"
#endif

#include <chrono>
Expand Down Expand Up @@ -65,7 +65,7 @@ class tcp_sink : public spdlog::sinks::base_sink<Mutex> {

void flush_() override {}
tcp_sink_config config_;
details::tcp_client client_;
details::tcp_client_unix client_;
};

using tcp_sink_mt = tcp_sink<std::mutex>;
Expand Down
4 changes: 2 additions & 2 deletions include/spdlog/sinks/udp_sink.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#ifdef _WIN32
#include "../details/udp_client-windows.h"
#else
#include "../details/udp_client.h"
#include "../details/udp_client_unix.h"
#endif

#include <chrono>
Expand Down Expand Up @@ -49,7 +49,7 @@ class udp_sink : public spdlog::sinks::base_sink<Mutex> {
}

void flush_() override {}
details::udp_client client_;
details::udp_client_unix client_;
};

using udp_sink_mt = udp_sink<std::mutex>;
Expand Down
Loading

0 comments on commit 885b796

Please sign in to comment.