new option to verify syntax and parameters of config file
cdoc for logging class
This commit is contained in:
		@@ -1,6 +1,12 @@
 | 
				
			|||||||
# author Lukas Heiligenbrunner
 | 
					# @author Lukas Heiligenbrunner
 | 
				
			||||||
# main CMake file
 | 
					# 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)
 | 
					cmake_minimum_required(VERSION 3.13)
 | 
				
			||||||
project(iprefresher DESCRIPTION "Dynu ip refresher")
 | 
					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(CMAKE_BUILD_TYPE Release) # manually set build type (Release / Debug)
 | 
				
			||||||
set(LIB_METHOD STATIC) #SHARED / STATIC
 | 
					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)
 | 
					option(BUILD_DOC "Build documentation" ON)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
 | 
					set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										12
									
								
								inc/Config.h
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								inc/Config.h
									
									
									
									
									
								
							@@ -16,7 +16,19 @@ public:
 | 
				
			|||||||
    static std::string telegramApiKey;
 | 
					    static std::string telegramApiKey;
 | 
				
			||||||
    static std::string chatId;
 | 
					    static std::string chatId;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * read configuration out of config file
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @return success of config read
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
    static bool readCredentials();
 | 
					    static bool readCredentials();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * validate config file
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @return validity of config file
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    static bool validateConfig();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
private:
 | 
					private:
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
							
								
								
									
										25
									
								
								inc/Logger.h
									
									
									
									
									
								
							
							
						
						
									
										25
									
								
								inc/Logger.h
									
									
									
									
									
								
							@@ -8,13 +8,38 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
class Logger {
 | 
					class Logger {
 | 
				
			||||||
public:
 | 
					public:
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * a debug message
 | 
				
			||||||
 | 
					     * @param message message
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
    static void debug(std::string message);
 | 
					    static void debug(std::string message);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * a default message
 | 
				
			||||||
 | 
					     * @param message message
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
    static void message(std::string message);
 | 
					    static void message(std::string message);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * a warning message
 | 
				
			||||||
 | 
					     * @param message message
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
    static void warning(std::string message);
 | 
					    static void warning(std::string message);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * a error message
 | 
				
			||||||
 | 
					     * @param message message
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
    static void error(std::string 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 void log(const std::string &message, int level);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    static const int Debug = 4;
 | 
					    static const int Debug = 4;
 | 
				
			||||||
    static const int Message = 3;
 | 
					    static const int Message = 3;
 | 
				
			||||||
    static const int Warning = 2;
 | 
					    static const int Warning = 2;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,11 +3,12 @@
 | 
				
			|||||||
//
 | 
					//
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <Config.h>
 | 
					#include <Config.h>
 | 
				
			||||||
 | 
					#include <Logger.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <iostream>
 | 
					#include <iostream>
 | 
				
			||||||
#include <cstring>
 | 
					#include <cstring>
 | 
				
			||||||
#include <fstream>
 | 
					#include <fstream>
 | 
				
			||||||
 | 
					#include <libconfig.h++>
 | 
				
			||||||
#include "libconfig.h++"
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
std::string Config::dynuapikey;
 | 
					std::string Config::dynuapikey;
 | 
				
			||||||
std::string Config::domainid; //id of the dynu domain
 | 
					std::string Config::domainid; //id of the dynu domain
 | 
				
			||||||
@@ -40,14 +41,13 @@ domainname = ""
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        std::ofstream myfile;
 | 
					        std::ofstream myfile;
 | 
				
			||||||
        myfile.open("/etc/iprefresher.cfg");
 | 
					        myfile.open("/etc/iprefresher.cfg");
 | 
				
			||||||
        if(myfile.is_open()){
 | 
					        if (myfile.is_open()) {
 | 
				
			||||||
            myfile << defaultconf;
 | 
					            myfile << defaultconf;
 | 
				
			||||||
            myfile.close();
 | 
					            myfile.close();
 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
            std::cout << "error creating file" << std::endl;
 | 
					            std::cout << "error creating file" << std::endl;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
        return false;
 | 
					        return false;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    catch (const libconfig::ParseException &pex) {
 | 
					    catch (const libconfig::ParseException &pex) {
 | 
				
			||||||
@@ -56,7 +56,6 @@ domainname = ""
 | 
				
			|||||||
        return false;
 | 
					        return false;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Get the store name.
 | 
					 | 
				
			||||||
    try {
 | 
					    try {
 | 
				
			||||||
        // needed parameters
 | 
					        // needed parameters
 | 
				
			||||||
        dynuapikey = (std::string) cfg.lookup("dynuapikey");
 | 
					        dynuapikey = (std::string) cfg.lookup("dynuapikey");
 | 
				
			||||||
@@ -75,3 +74,48 @@ domainname = ""
 | 
				
			|||||||
    // check if needed values aren't empty
 | 
					    // check if needed values aren't empty
 | 
				
			||||||
    return !(Config::dynuapikey.empty() || Config::domainid.empty() || Config::domainname.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;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -39,6 +39,9 @@ void Logger::log(const std::string &message, int level) {
 | 
				
			|||||||
        case Error:
 | 
					        case Error:
 | 
				
			||||||
            out << "ERROR";
 | 
					            out << "ERROR";
 | 
				
			||||||
            break;
 | 
					            break;
 | 
				
			||||||
 | 
					        default:
 | 
				
			||||||
 | 
					            out << "UNDEFINED";
 | 
				
			||||||
 | 
					            break;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    out << "] ";
 | 
					    out << "] ";
 | 
				
			||||||
    out << message;
 | 
					    out << message;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,10 +3,7 @@
 | 
				
			|||||||
//
 | 
					//
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "api/API.h"
 | 
					#include "api/API.h"
 | 
				
			||||||
#include "api/Hashmap.h"
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <string>
 | 
					 | 
				
			||||||
#include <iostream>
 | 
					 | 
				
			||||||
#include <sstream>
 | 
					#include <sstream>
 | 
				
			||||||
#include <curl/curl.h>
 | 
					#include <curl/curl.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										10
									
								
								src/main.cpp
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								src/main.cpp
									
									
									
									
									
								
							@@ -2,12 +2,12 @@
 | 
				
			|||||||
// Created by lukas on 18.06.19.
 | 
					// Created by lukas on 18.06.19.
 | 
				
			||||||
//
 | 
					//
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <iostream>
 | 
					 | 
				
			||||||
#include <Version.h>
 | 
					#include <Version.h>
 | 
				
			||||||
#include <IPRefresher.h>
 | 
					#include <IPRefresher.h>
 | 
				
			||||||
#include <Logger.h>
 | 
					#include <Logger.h>
 | 
				
			||||||
#include <Config.h>
 | 
					#include <Config.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <iostream>
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * application entry point
 | 
					 * application entry point
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
@@ -19,6 +19,7 @@ int main(int argc, char *argv[]) {
 | 
				
			|||||||
                      << "[-v] [--version] print the software version" << std::endl
 | 
					                      << "[-v] [--version] print the software version" << std::endl
 | 
				
			||||||
                      << "[-f] [--force] force refresh of ip" << std::endl
 | 
					                      << "[-f] [--force] force refresh of ip" << std::endl
 | 
				
			||||||
                      << "[-l] [--loop] infinite loop to refresh ip every five minutes" << 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;
 | 
					                      << "[no argument] normal ip check and refresh" << std::endl;
 | 
				
			||||||
        } else if (firstarg == "-v" || firstarg == "--version") {
 | 
					        } else if (firstarg == "-v" || firstarg == "--version") {
 | 
				
			||||||
            std::cout << "Version " << Version::VERSION << std::endl;
 | 
					            std::cout << "Version " << Version::VERSION << std::endl;
 | 
				
			||||||
@@ -32,6 +33,13 @@ int main(int argc, char *argv[]) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        } else if (firstarg == "-l" || firstarg == "--loop") {
 | 
					        } else if (firstarg == "-l" || firstarg == "--loop") {
 | 
				
			||||||
            IPRefresher(true);
 | 
					            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 {
 | 
					        } else {
 | 
				
			||||||
            Logger::message("wrong arguments!  -h for help");
 | 
					            Logger::message("wrong arguments!  -h for help");
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user