created logging class
This commit is contained in:
		
							
								
								
									
										49
									
								
								src/FileLogger.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								src/FileLogger.cpp
									
									
									
									
									
										Normal 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();
 | 
			
		||||
}
 | 
			
		||||
@@ -4,7 +4,7 @@
 | 
			
		||||
 | 
			
		||||
#include <string>
 | 
			
		||||
 | 
			
		||||
#include <Logger.h>
 | 
			
		||||
#include <FileLogger.h>
 | 
			
		||||
#include <api/IPAPI.h>
 | 
			
		||||
#include <api/DynuAPI.h>
 | 
			
		||||
#include <api/TelegramAPI.h>
 | 
			
		||||
@@ -12,10 +12,10 @@
 | 
			
		||||
#include <chrono>
 | 
			
		||||
#include <thread>
 | 
			
		||||
 | 
			
		||||
#include "IPRefresher.h"
 | 
			
		||||
#include <IPRefresher.h>
 | 
			
		||||
 | 
			
		||||
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));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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 <iostream>
 | 
			
		||||
 | 
			
		||||
#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;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,8 +1,9 @@
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <IPRefresher.h>
 | 
			
		||||
#include <Version.h>
 | 
			
		||||
#include <Logger.h>
 | 
			
		||||
 | 
			
		||||
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);
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user