Skip to content

Commit

Permalink
Set log levels based on build type
Browse files Browse the repository at this point in the history
  • Loading branch information
mleleszi committed Jun 14, 2024
1 parent c8f83d0 commit 4938798
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 13 deletions.
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ project(cpp_redis)

set(CMAKE_CXX_STANDARD 20)

include(FetchContent)

# TODO: create separate CMAKE for dependencies
FetchContent_Declare(
spdlog
GIT_REPOSITORY https://github.com/gabime/spdlog.git
GIT_TAG v1.14.1
)
FetchContent_MakeAvailable(spdlog)

add_subdirectory(src)
add_subdirectory(tests)

6 changes: 6 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@ add_executable(cpp_redis main.cpp
datastore.cpp)


if (CMAKE_BUILD_TYPE STREQUAL "Debug")
target_compile_definitions(cpp_redis PRIVATE SPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_DEBUG)
elseif (CMAKE_BUILD_TYPE STREQUAL "Release")
target_compile_definitions(cpp_redis PRIVATE SPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_INFO)
endif ()

target_link_libraries(cpp_redis PRIVATE spdlog::spdlog_header_only)
9 changes: 7 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#include <iostream>

#include "spdlog/spdlog.h"
#include "tcp_server.h"

int main() {
#if SPDLOG_ACTIVE_LEVEL == SPDLOG_LEVEL_DEBUG
spdlog::set_level(spdlog::level::debug);
#elif SPDLOG_ACTIVE_LEVEL == SPDLOG_LEVEL_INFO
spdlog::set_level(spdlog::level::info);
#endif

TCPServer server;
server.start("0.0.0.0", 6379);
}
5 changes: 2 additions & 3 deletions src/tcp_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ TCPServer::TCPServer() : controller{} {
}
}

// TODO: why can it start if the port is already taken?
[[noreturn]] void TCPServer::start(const std::string &address = "0.0.0.0", int port = 6379) {
struct sockaddr_in server_addr {};
server_addr.sin_family = AF_INET;
Expand All @@ -36,7 +35,7 @@ TCPServer::TCPServer() : controller{} {
throw std::runtime_error("Port " + std::to_string(port) + " already in use!");
}

int connection_backlog = 5;
int connection_backlog = 128;
if (listen(m_serverFD, connection_backlog) != 0) { throw std::runtime_error("Listen failed!"); }

spdlog::info("Listening on port {}", port);
Expand Down Expand Up @@ -118,7 +117,7 @@ void TCPServer::handleRequest(int connFD) {
// Handle command
RedisType::RedisValue res = controller.handleCommand(command);
auto encoded = encode(res);
spdlog::info("Request: {}, Response: {}", std::get<RedisType::Array>(message), res);
spdlog::debug("Request: {}, Response: {}", std::get<RedisType::Array>(message), res);

// Send response
send(connFD, encoded.data(), encoded.size(), 0);
Expand Down
3 changes: 2 additions & 1 deletion src/tcp_server.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "controller.h"
#include <atomic>
#include <string>


Expand All @@ -13,7 +14,7 @@ class TCPServer {
private:
int m_serverFD;
Controller controller;

static constexpr size_t RECV_SIZE = 2048;
};

Expand Down
7 changes: 0 additions & 7 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@ FetchContent_Declare(

FetchContent_MakeAvailable(googletest)

FetchContent_Declare(
spdlog
GIT_REPOSITORY https://github.com/gabime/spdlog.git
GIT_TAG v1.14.1
)
FetchContent_MakeAvailable(spdlog)

add_library(GTest::GTest INTERFACE IMPORTED)
target_link_libraries(GTest::GTest INTERFACE gtest_main)

Expand Down

0 comments on commit 4938798

Please sign in to comment.