56 lines
1.9 KiB
C++
56 lines
1.9 KiB
C++
#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["IP"] = "::1";
|
|
config.configJsonRoot["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.IP = config.configJsonRoot["IP"].asCString();
|
|
|
|
config.Port = config.configJsonRoot["Port"].asInt();
|
|
if (config.Port < 0 || config.Port > 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;
|
|
}
|
|
} |