new option to verify syntax and parameters of config file

cdoc for logging class
This commit is contained in:
Lukas Heiligenbrunner 2020-05-01 15:33:20 +02:00
parent cf49bf7bc5
commit 2505e2cbf4
7 changed files with 106 additions and 11 deletions

View File

@ -1,6 +1,12 @@
# author Lukas Heiligenbrunner
# @author Lukas Heiligenbrunner
# main CMake file
#
# Build lib dependencies:
## libcurl (with sources)
## libconfig (with sources)
#
# documenation build needs doxygen to be installed.
cmake_minimum_required(VERSION 3.13)
project(iprefresher DESCRIPTION "Dynu ip refresher")
@ -8,7 +14,7 @@ set(CMAKE_CXX_STANDARD 17)
set(CMAKE_BUILD_TYPE Release) # manually set build type (Release / Debug)
set(LIB_METHOD STATIC) #SHARED / STATIC
set(PROJECT_VERSION 1.3.1)
set(PROJECT_VERSION 1.3.1-dev)
option(BUILD_DOC "Build documentation" ON)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

View File

@ -16,7 +16,19 @@ public:
static std::string telegramApiKey;
static std::string chatId;
/**
* read configuration out of config file
*
* @return success of config read
*/
static bool readCredentials();
/**
* validate config file
*
* @return validity of config file
*/
static bool validateConfig();
private:
};

View File

@ -8,13 +8,38 @@
class Logger {
public:
/**
* a debug message
* @param message message
*/
static void debug(std::string message);
/**
* a default message
* @param message message
*/
static void message(std::string message);
/**
* a warning message
* @param message message
*/
static void warning(std::string message);
/**
* a error message
* @param message message
*/
static void error(std::string message);
/**
* a log message with manual level set
* @param message message
* @param level loglevel (1-4) or predefined labels
*/
static void log(const std::string &message, int level);
static const int Debug = 4;
static const int Message = 3;
static const int Warning = 2;

View File

@ -3,11 +3,12 @@
//
#include <Config.h>
#include <Logger.h>
#include <iostream>
#include <cstring>
#include <fstream>
#include "libconfig.h++"
#include <libconfig.h++>
std::string Config::dynuapikey;
std::string Config::domainid; //id of the dynu domain
@ -40,14 +41,13 @@ domainname = ""
std::ofstream myfile;
myfile.open("/etc/iprefresher.cfg");
if(myfile.is_open()){
if (myfile.is_open()) {
myfile << defaultconf;
myfile.close();
} else {
std::cout << "error creating file" << std::endl;
}
return false;
}
catch (const libconfig::ParseException &pex) {
@ -56,7 +56,6 @@ domainname = ""
return false;
}
// Get the store name.
try {
// needed parameters
dynuapikey = (std::string) cfg.lookup("dynuapikey");
@ -75,3 +74,48 @@ domainname = ""
// check if needed values aren't empty
return !(Config::dynuapikey.empty() || Config::domainid.empty() || Config::domainname.empty());
}
bool Config::validateConfig() {
libconfig::Config cfg;
try {
Logger::message("reading config file");
cfg.readFile("/etc/iprefresher.cfg");
}
catch (const libconfig::FileIOException &fioex) {
Logger::warning("config file doesn't exist or permission denied!");
return false;
}
catch (const libconfig::ParseException &pex) {
std::cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine()
<< " - " << pex.getError() << std::endl;
return false;
}
Logger::message("Syntax and Permission is OK");
try {
// needed parameters
if (((std::string) cfg.lookup("dynuapikey")).empty()) {
Logger::warning("required parameter dynuapikey seems to be empty.");
return false;
}
if (((std::string) cfg.lookup("domainid")).empty()) {
Logger::warning("required parameter domainid seems to be empty.");
return false;
}
if (((std::string) cfg.lookup("domainname")).empty()) {
Logger::warning("required parameter domainname seems to be empty.");
return false;
}
// optional parameters
cfg.lookup("telegramApiKey");
cfg.lookup("chatId");
}
catch (const libconfig::SettingNotFoundException &nfex) {
// triggered if setting is missing in config
if (!(std::strcmp("telegramApiKey", nfex.getPath()) == 0 || std::strcmp("chatId", nfex.getPath()) == 0)) {
std::cerr << "No '" << nfex.getPath() << "' setting in configuration file." << std::endl;
return false;
}
}
return true;
}

View File

@ -39,6 +39,9 @@ void Logger::log(const std::string &message, int level) {
case Error:
out << "ERROR";
break;
default:
out << "UNDEFINED";
break;
}
out << "] ";
out << message;

View File

@ -3,10 +3,7 @@
//
#include "api/API.h"
#include "api/Hashmap.h"
#include <string>
#include <iostream>
#include <sstream>
#include <curl/curl.h>

View File

@ -2,12 +2,12 @@
// Created by lukas on 18.06.19.
//
#include <iostream>
#include <Version.h>
#include <IPRefresher.h>
#include <Logger.h>
#include <Config.h>
#include <iostream>
/**
* application entry point
*/
@ -19,6 +19,7 @@ int main(int argc, char *argv[]) {
<< "[-v] [--version] print the software version" << std::endl
<< "[-f] [--force] force refresh of ip" << std::endl
<< "[-l] [--loop] infinite loop to refresh ip every five minutes" << std::endl
<< "[-c] [--checkconfig] validate configuration" << std::endl
<< "[no argument] normal ip check and refresh" << std::endl;
} else if (firstarg == "-v" || firstarg == "--version") {
std::cout << "Version " << Version::VERSION << std::endl;
@ -32,6 +33,13 @@ int main(int argc, char *argv[]) {
} else if (firstarg == "-l" || firstarg == "--loop") {
IPRefresher(true);
} else if (firstarg == "-c" || firstarg == "--checkconfig") {
if (Config::validateConfig()) {
Logger::message("Config file is OK");
} else {
Logger::warning("There are errors in config file!");
return -1;
}
} else {
Logger::message("wrong arguments! -h for help");
}