로거 완성
This commit is contained in:
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user