diff --git a/CMakeLists.txt b/CMakeLists.txt index 7ab48d9..bad983a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,11 +1,12 @@ cmake_minimum_required(VERSION 3.13) -project(iprefresher VERSION 1.2.0 DESCRIPTION "Dynu ip refresher") +project(iprefresher DESCRIPTION "Dynu ip refresher") set(CMAKE_CXX_STANDARD 17) set(CMAKE_BUILD_TYPE Release) # manually set build type (Release / Debug) set(libmethod STATIC) #SHARED / STATIC set(WinBuild false) +set(PROJECT_VERSION 1.2.1) set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) @@ -60,6 +61,11 @@ else () endif () include_directories(${CURL_INCLUDE_DIR} inc) +#add version header +FILE(WRITE ${CMAKE_SOURCE_DIR}/inc/Version.h + "\#pragma once\nclass Version {\npublic:\n static const std::string VERSION;\n};\n\n std::string const Version::VERSION = \"${PROJECT_VERSION}\";" + ) + add_library(api ${libmethod} src/api/API.cpp @@ -71,13 +77,15 @@ add_library(api ${libmethod} ) add_library(logger ${libmethod} + src/FileLogger.cpp src/Logger.cpp ) set(SOURCE src/main.cpp - src/IPRefresher.cpp inc/IPRefresher.h) + src/IPRefresher.cpp + ) add_executable(iprefresher ${SOURCE}) @@ -124,8 +132,9 @@ IF (UNIX) SET(CPACK_PACKAGE_VENDOR "Lukas Heilgienbrunner") SET(CPACK_PACKAGE_VERSION_MAJOR "1") SET(CPACK_PACKAGE_VERSION_MINOR "2") - SET(CPACK_PACKAGE_VERSION "${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}") - SET(CPACK_SOURCE_PACKAGE_FILE_NAME "${CMAKE_PROJECT_NAME}-${CPACK_PACKAGE_VERSION}") + set(CPACK_PACKAGE_VERSION_PATCH "1") + SET(CPACK_PACKAGE_VERSION "${PROJECT_VERSION}") + SET(CPACK_SOURCE_PACKAGE_FILE_NAME "${CMAKE_PROJECT_NAME}-${PROJECT_VERSION}") SET(CPACK_PACKAGE_CONTACT "Lukas Heiligenbrunner ") SET(CPACK_PACKAGE_SECTION "games") set(CPACK_PACKAGE_DEPENDS "libcurl (>= 7.0.0-1)") diff --git a/inc/FileLogger.h b/inc/FileLogger.h new file mode 100644 index 0000000..187863b --- /dev/null +++ b/inc/FileLogger.h @@ -0,0 +1,27 @@ +// +// Created by lukas on 05.05.19. +// +#pragma once + +#include +class FileLogger { +public: + /** + * log messages to logfile + * @param text message + */ + void logToLogfile(std::string text); + + /** + * safe ip to temp file + * @param ip ip address to save + */ + void safeip(std::string ip); + + /** + * read ip from file + * @return read ip + */ + std::string readip(); + +}; diff --git a/inc/Logger.h b/inc/Logger.h index 465dffb..85080b1 100644 --- a/inc/Logger.h +++ b/inc/Logger.h @@ -1,27 +1,22 @@ // -// Created by lukas on 05.05.19. +// Created by lukas on 26.10.19. // + #pragma once #include + class Logger { public: - /** - * log messages to logfile - * @param text message - */ - void logToLogfile(std::string text); + static void debug(std::string message); + static void message(std::string message); + static void warning(std::string message); + static void error(std::string message); - /** - * safe ip to temp file - * @param ip ip address to save - */ - void safeip(std::string ip); - - /** - * read ip from file - * @return read ip - */ - std::string readip(); + static void log(std::string message, int level); + static const int Debug; + static const int Message; + static const int Warning; + static const int Error; }; diff --git a/src/FileLogger.cpp b/src/FileLogger.cpp new file mode 100644 index 0000000..96c64d0 --- /dev/null +++ b/src/FileLogger.cpp @@ -0,0 +1,49 @@ +// +// Created by lukas on 05.05.19. +// + +#include +#include +#include +#include + +#include "FileLogger.h" + +void FileLogger::safeip(std::string ip) { + std::ofstream out; + out.open("ip.txt", std::ios::out); + + out << ip; + + out.close(); +} + +std::string FileLogger::readip() { + std::ifstream in; + in.open("ip.txt", std::ios::in); + + std::string ip; + + in >> ip; + + return ip; +} + +void FileLogger::logToLogfile(std::string text) { + std::ofstream out; + out.open("dynurefresher.log", std::ios::out | std::ios::app); + + + std::time_t t = std::time(0); // get time now + std::tm *now = std::localtime(&t); + + std::stringstream logline; + + logline << "[ " << (now->tm_year + 1900) << '-' << (now->tm_mon + 1) << '-' << now->tm_mday + << "_" << now->tm_hour << ":" << now->tm_min << ":" << now->tm_sec << " ] " << '\t' << text << std::endl; + + + out << logline.str(); + + out.close(); +} diff --git a/src/IPRefresher.cpp b/src/IPRefresher.cpp index 843c3a2..30e2484 100644 --- a/src/IPRefresher.cpp +++ b/src/IPRefresher.cpp @@ -4,7 +4,7 @@ #include -#include +#include #include #include #include @@ -12,10 +12,10 @@ #include #include -#include "IPRefresher.h" +#include void IPRefresher::checkIPAdress(bool force) { - Logger logger; + FileLogger logger; IPAPI ipapi; std::string ip = ipapi.getGlobalIp(); @@ -55,8 +55,10 @@ IPRefresher::IPRefresher() { } IPRefresher::IPRefresher(bool loop) { + std::cout << "[INFO] startup of service" << std::endl; while(true){ - std::this_thread::sleep_for(std::chrono::milliseconds(300000)); + std::cout << "[INFO] starting check" << std::endl; checkIPAdress(false); + std::this_thread::sleep_for(std::chrono::milliseconds(300000)); } } diff --git a/src/Logger.cpp b/src/Logger.cpp index ec592c5..a59ce7a 100644 --- a/src/Logger.cpp +++ b/src/Logger.cpp @@ -1,49 +1,55 @@ // -// Created by lukas on 05.05.19. +// Created by lukas on 26.10.19. // -#include -#include -#include #include +#include #include "Logger.h" -void Logger::safeip(std::string ip) { - std::ofstream out; - out.open("ip.txt", std::ios::out); - out << ip; - out.close(); + +const int Logger::Warning = 1; +const int Logger::Debug = 2; +const int Logger::Message = 3; +const int Logger::Error = 4; + +void Logger::debug(std::string message) { + log(message,Logger::Debug); } -std::string Logger::readip() { - std::ifstream in; - in.open("ip.txt", std::ios::in); - - std::string ip; - - in >> ip; - - return ip; +void Logger::message(std::string message) { + log(message,Logger::Message); } -void Logger::logToLogfile(std::string text) { - std::ofstream out; - out.open("dynurefresher.log", std::ios::out | std::ios::app); - - - std::time_t t = std::time(0); // get time now - std::tm *now = std::localtime(&t); - - std::stringstream logline; - - logline << "[ " << (now->tm_year + 1900) << '-' << (now->tm_mon + 1) << '-' << now->tm_mday - << "_" << now->tm_hour << ":" << now->tm_min << ":" << now->tm_sec << " ] " << '\t' << text << std::endl; - - - out << logline.str(); - - out.close(); +void Logger::warning(std::string message) { + log(message,Logger::Warning); } + +void Logger::error(std::string message) { + log(message,Logger::Error); +} + +void Logger::log(std::string message, int level) { + std::stringstream out; + out << "["; + switch (level){ + case Debug: + out << "DEBUG"; + break; + case Message: + out << "MESSAGE"; + break; + case Warning: + out << "WARNING"; + break; + case Error: + out << "ERROR"; + break; + } + out << "] "; + out << message; + std::cout << out.str() << std::endl; +} + diff --git a/src/main.cpp b/src/main.cpp index 171f532..5ecb3f1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,8 +1,9 @@ #include #include +#include +#include int main(int argc, char *argv[]) { - if (argc > 1) { std::string firstarg(argv[1]); if (firstarg == "-h" || firstarg == "--help") { @@ -12,18 +13,18 @@ int main(int argc, char *argv[]) { << "[-l] [--loop] infinite loop to refresh ip every five minutes" << std::endl << "[no argument] normal ip check and refresh" << std::endl; } else if (firstarg == "-v" || firstarg == "--version") { - std::cout << "Version 1.2" << std::endl; + Logger::message("Version "+Version::VERSION); } else if (firstarg == "-f" || firstarg == "--force") { IPRefresher ipr; ipr.checkIPAdress(true); } else if (firstarg == "-l" || firstarg == "--loop") { IPRefresher ipr(true); - ipr.checkIPAdress(false); } else { - std::cout << "wrong arguments! -h for help" << std::endl; + Logger::message("wrong arguments! -h for help"); } } else { IPRefresher ipr; + Logger::message("starting check"); ipr.checkIPAdress(false); }