created logging class

This commit is contained in:
lukas-heiligenbrunner 2019-10-26 14:41:43 +02:00
parent cc8c2b0497
commit 73b6754806
7 changed files with 153 additions and 64 deletions

View File

@ -1,11 +1,12 @@
cmake_minimum_required(VERSION 3.13) 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_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(libmethod STATIC) #SHARED / STATIC set(libmethod STATIC) #SHARED / STATIC
set(WinBuild false) set(WinBuild false)
set(PROJECT_VERSION 1.2.1)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
@ -60,6 +61,11 @@ else ()
endif () endif ()
include_directories(${CURL_INCLUDE_DIR} inc) 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} add_library(api ${libmethod}
src/api/API.cpp src/api/API.cpp
@ -71,13 +77,15 @@ add_library(api ${libmethod}
) )
add_library(logger ${libmethod} add_library(logger ${libmethod}
src/FileLogger.cpp
src/Logger.cpp src/Logger.cpp
) )
set(SOURCE set(SOURCE
src/main.cpp src/main.cpp
src/IPRefresher.cpp inc/IPRefresher.h) src/IPRefresher.cpp
)
add_executable(iprefresher ${SOURCE}) add_executable(iprefresher ${SOURCE})
@ -124,8 +132,9 @@ IF (UNIX)
SET(CPACK_PACKAGE_VENDOR "Lukas Heilgienbrunner") SET(CPACK_PACKAGE_VENDOR "Lukas Heilgienbrunner")
SET(CPACK_PACKAGE_VERSION_MAJOR "1") SET(CPACK_PACKAGE_VERSION_MAJOR "1")
SET(CPACK_PACKAGE_VERSION_MINOR "2") SET(CPACK_PACKAGE_VERSION_MINOR "2")
SET(CPACK_PACKAGE_VERSION "${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}") set(CPACK_PACKAGE_VERSION_PATCH "1")
SET(CPACK_SOURCE_PACKAGE_FILE_NAME "${CMAKE_PROJECT_NAME}-${CPACK_PACKAGE_VERSION}") SET(CPACK_PACKAGE_VERSION "${PROJECT_VERSION}")
SET(CPACK_SOURCE_PACKAGE_FILE_NAME "${CMAKE_PROJECT_NAME}-${PROJECT_VERSION}")
SET(CPACK_PACKAGE_CONTACT "Lukas Heiligenbrunner <ultimategameingcookie@gmail.com>") SET(CPACK_PACKAGE_CONTACT "Lukas Heiligenbrunner <ultimategameingcookie@gmail.com>")
SET(CPACK_PACKAGE_SECTION "games") SET(CPACK_PACKAGE_SECTION "games")
set(CPACK_PACKAGE_DEPENDS "libcurl (>= 7.0.0-1)") set(CPACK_PACKAGE_DEPENDS "libcurl (>= 7.0.0-1)")

27
inc/FileLogger.h Normal file
View File

@ -0,0 +1,27 @@
//
// Created by lukas on 05.05.19.
//
#pragma once
#include <string>
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();
};

View File

@ -1,27 +1,22 @@
// //
// Created by lukas on 05.05.19. // Created by lukas on 26.10.19.
// //
#pragma once #pragma once
#include <string> #include <string>
class Logger { class Logger {
public: public:
/** static void debug(std::string message);
* log messages to logfile static void message(std::string message);
* @param text message static void warning(std::string message);
*/ static void error(std::string message);
void logToLogfile(std::string text);
/** static void log(std::string message, int level);
* 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 const int Debug;
static const int Message;
static const int Warning;
static const int Error;
}; };

49
src/FileLogger.cpp Normal file
View File

@ -0,0 +1,49 @@
//
// Created by lukas on 05.05.19.
//
#include <fstream>
#include <ctime>
#include <iostream>
#include <sstream>
#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();
}

View File

@ -4,7 +4,7 @@
#include <string> #include <string>
#include <Logger.h> #include <FileLogger.h>
#include <api/IPAPI.h> #include <api/IPAPI.h>
#include <api/DynuAPI.h> #include <api/DynuAPI.h>
#include <api/TelegramAPI.h> #include <api/TelegramAPI.h>
@ -12,10 +12,10 @@
#include <chrono> #include <chrono>
#include <thread> #include <thread>
#include "IPRefresher.h" #include <IPRefresher.h>
void IPRefresher::checkIPAdress(bool force) { void IPRefresher::checkIPAdress(bool force) {
Logger logger; FileLogger logger;
IPAPI ipapi; IPAPI ipapi;
std::string ip = ipapi.getGlobalIp(); std::string ip = ipapi.getGlobalIp();
@ -55,8 +55,10 @@ IPRefresher::IPRefresher() {
} }
IPRefresher::IPRefresher(bool loop) { IPRefresher::IPRefresher(bool loop) {
std::cout << "[INFO] startup of service" << std::endl;
while(true){ while(true){
std::this_thread::sleep_for(std::chrono::milliseconds(300000)); std::cout << "[INFO] starting check" << std::endl;
checkIPAdress(false); checkIPAdress(false);
std::this_thread::sleep_for(std::chrono::milliseconds(300000));
} }
} }

View File

@ -1,49 +1,55 @@
// //
// Created by lukas on 05.05.19. // Created by lukas on 26.10.19.
// //
#include <fstream>
#include <ctime>
#include <iostream>
#include <sstream> #include <sstream>
#include <iostream>
#include "Logger.h" #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() { void Logger::message(std::string message) {
std::ifstream in; log(message,Logger::Message);
in.open("ip.txt", std::ios::in);
std::string ip;
in >> ip;
return ip;
} }
void Logger::logToLogfile(std::string text) { void Logger::warning(std::string message) {
std::ofstream out; log(message,Logger::Warning);
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::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;
}

View File

@ -1,8 +1,9 @@
#include <iostream> #include <iostream>
#include <IPRefresher.h> #include <IPRefresher.h>
#include <Version.h>
#include <Logger.h>
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
if (argc > 1) { if (argc > 1) {
std::string firstarg(argv[1]); std::string firstarg(argv[1]);
if (firstarg == "-h" || firstarg == "--help") { 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 << "[-l] [--loop] infinite loop to refresh ip every five minutes" << 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 1.2" << std::endl; Logger::message("Version "+Version::VERSION);
} else if (firstarg == "-f" || firstarg == "--force") { } else if (firstarg == "-f" || firstarg == "--force") {
IPRefresher ipr; IPRefresher ipr;
ipr.checkIPAdress(true); ipr.checkIPAdress(true);
} else if (firstarg == "-l" || firstarg == "--loop") { } else if (firstarg == "-l" || firstarg == "--loop") {
IPRefresher ipr(true); IPRefresher ipr(true);
ipr.checkIPAdress(false);
} else { } else {
std::cout << "wrong arguments! -h for help" << std::endl; Logger::message("wrong arguments! -h for help");
} }
} else { } else {
IPRefresher ipr; IPRefresher ipr;
Logger::message("starting check");
ipr.checkIPAdress(false); ipr.checkIPAdress(false);
} }