알려진 버그 픽스
This commit is contained in:
53
Server/src/Utils/ConfigManager.cpp
Normal file
53
Server/src/Utils/ConfigManager.cpp
Normal file
@@ -0,0 +1,53 @@
|
||||
#include "Utils/ConfigManager.hpp"
|
||||
#include "precomp.hpp"
|
||||
#include <fstream>
|
||||
#include <json/json.h>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
namespace Chattr {
|
||||
ConfigManager::Config ConfigManager::load() {
|
||||
Config config;
|
||||
|
||||
std::ifstream configfile("config.json", std::ifstream::binary);
|
||||
if (!configfile.is_open()) {
|
||||
std::ofstream defaultConfig("config.json", std::ios::out);
|
||||
config.configJsonRoot["IP Version"] = 6;
|
||||
config.configJsonRoot["Listen Port"] = 9010;
|
||||
config.configJsonRoot["LogLevel"] = 1;
|
||||
config.configJsonRoot["LogfileName"] = "Chattering.log";
|
||||
config.configJsonRoot["LogfileSize"] = UINT32_MAX;
|
||||
config.configJsonRoot["LogfileCount"] = 5;
|
||||
defaultConfig << config.configJsonRoot;
|
||||
defaultConfig.close();
|
||||
spdlog::critical("\"config.json\" is missing. Default configuration has been written.");
|
||||
}
|
||||
try {
|
||||
if (configfile.is_open())
|
||||
configfile >> config.configJsonRoot;
|
||||
|
||||
config.ipVersion = config.configJsonRoot["IP Version"].asInt();
|
||||
if (config.ipVersion != 4 && config.ipVersion != 6)
|
||||
throw std::runtime_error("Invalid IP Version.");
|
||||
|
||||
config.listenPort = config.configJsonRoot["Listen Port"].asInt();
|
||||
if (config.listenPort < 0 || config.listenPort > 65535)
|
||||
throw std::runtime_error("Invalid listen port.");
|
||||
|
||||
int ll_ = config.configJsonRoot["LogLevel"].asInt();
|
||||
if (ll_ >= 0 && ll_ < spdlog::level::n_levels)
|
||||
config.logLevel = (spdlog::level::level_enum)ll_;
|
||||
else
|
||||
throw std::runtime_error("Invalid log level.");
|
||||
|
||||
config.logFileName = config.configJsonRoot["LogfileName"].asCString();
|
||||
config.logfileSize = config.configJsonRoot["LogfileSize"].asUInt();
|
||||
config.logfileCount = config.configJsonRoot["LogfileCount"].asUInt();
|
||||
}
|
||||
catch (Json::RuntimeError e) {
|
||||
spdlog::critical(std::string(std::string("[Json Error: ]") + e.what()).c_str());
|
||||
std::exit(EXIT_FAILURE);
|
||||
}
|
||||
return config;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user