로거 완성

This commit is contained in:
2025-04-22 04:04:43 +09:00
parent 819bd5362e
commit 3a3cdc130a
11 changed files with 206 additions and 91 deletions

View File

@@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.5)
set(PROJECT_NAME "Server")
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
project(${PROJECT_NAME} CXX)
@@ -23,6 +24,14 @@ FetchContent_Declare(
)
FetchContent_MakeAvailable(GSL)
FetchContent_Declare(
JSONCPP
GIT_REPOSITORY "https://github.com/open-source-parsers/jsoncpp.git"
GIT_TAG "1.9.6"
GIT_SHALLOW ON
)
FetchContent_MakeAvailable(JSONCPP)
file(GLOB_RECURSE additional_sources CONFIGURE_DEPENDS
"${CMAKE_CURRENT_SOURCE_DIR}/../impl/*.cpp"
)
@@ -33,6 +42,7 @@ add_executable(${PROJECT_NAME}
)
target_link_libraries(${PROJECT_NAME} PRIVATE Microsoft.GSL::GSL)
target_link_libraries(${PROJECT_NAME} PRIVATE spdlog)
target_link_libraries(${PROJECT_NAME} PRIVATE jsoncpp_lib)
if(WIN32)
target_link_libraries(${PROJECT_NAME} PRIVATE ws2_32)

View File

@@ -1,15 +1,68 @@
#include <iostream>
#include "Socket/Socket_Init.hpp"
#include "Socket/Socket.hpp"
#include "Socket/Log.hpp"
#include "precomp.hpp"
#include <fstream>
#include <json/json.h>
#include <iostream>
#include <string>
int main() {
struct Chattr::Socket_Init sock(AF_INET, SOCK_DGRAM, 0);
Json::Value configJsonRoot;
std::uint32_t ipVersion = 0;
std::uint32_t listenPort = 0;
spdlog::level::level_enum logLevel;
gsl::czstring logFileName;
std::uint32_t logfileSize;
std::uint32_t logfileCount;
std::ifstream config("config.json", std::ifstream::binary);
if (!config.is_open()) {
std::ofstream defaultConfig("config.json", std::ios::out);
configJsonRoot["IP Version"] = 6;
configJsonRoot["Listen Port"] = 9010;
configJsonRoot["LogLevel"] = 1;
configJsonRoot["LogfileName"] = "Chattering.log";
configJsonRoot["LogfileSize"] = UINT32_MAX;
configJsonRoot["LogfileCount"] = 5;
defaultConfig << configJsonRoot;
defaultConfig.close();
spdlog::critical("\"config.json\" is missing. Default configuration has been written.");
std::exit(EXIT_FAILURE);
}
try {
config >> configJsonRoot;
ipVersion = configJsonRoot["IP Version"].asInt();
if (ipVersion != 4 && ipVersion != 6)
throw std::runtime_error("Invalid IP Version.");
listenPort = configJsonRoot["Listen Port"].asInt();
if (listenPort < 0 || listenPort > 65535)
throw std::runtime_error("Invalid listen port.");
int ll_ = configJsonRoot["LogLevel"].asInt();
if (ll_ >= 0 && ll_ < spdlog::level::n_levels)
logLevel = (spdlog::level::level_enum)ll_;
else
throw std::runtime_error("Invalid log level.");
logFileName = configJsonRoot["LogfileName"].asCString();
logfileSize = configJsonRoot["LogfileSize"].asUInt();
logfileCount = configJsonRoot["LogfileCount"].asUInt();
}
catch (Json::RuntimeError e) {
spdlog::critical(std::string(std::string("[Json Error: ]") + e.what()).c_str());
std::exit(EXIT_FAILURE);
}
Chattr::log::setDefaultLogger(logLevel, logFileName, logfileSize, logfileCount);
struct Chattr::Socket sock(AF_INET, SOCK_DGRAM, 0);
struct sockaddr_in serveraddr = {};
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
serveraddr.sin_port = htons(9010);
sock.bind(sock, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
std::cout << sock << std::endl;
sock.bind(sock, &serveraddr);
}