added loggin methods
This commit is contained in:
		@@ -43,7 +43,6 @@ std::string API::request(std::string myurl, bool post, Hashmap<std::string,std::
 | 
			
		||||
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
 | 
			
		||||
 | 
			
		||||
        if(post){
 | 
			
		||||
            std::cout << "add post data\n";
 | 
			
		||||
            std::stringstream poststring;
 | 
			
		||||
            poststring <<  "{";
 | 
			
		||||
            for (int i = 0; i < map.size(); i++) {
 | 
			
		||||
@@ -54,7 +53,7 @@ std::string API::request(std::string myurl, bool post, Hashmap<std::string,std::
 | 
			
		||||
            }
 | 
			
		||||
            poststring << "}";
 | 
			
		||||
 | 
			
		||||
            std::cout << "post string: " << poststring.str() << "\n";
 | 
			
		||||
//            std::cout << "post string: " << poststring.str() << "\n";
 | 
			
		||||
            curl_easy_setopt(curl, CURLOPT_POST, 1);
 | 
			
		||||
            curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, poststring.str().c_str());
 | 
			
		||||
        }else{
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										49
									
								
								src/Logger.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								src/Logger.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,49 @@
 | 
			
		||||
//
 | 
			
		||||
// Created by lukas on 05.05.19.
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
#include <fstream>
 | 
			
		||||
#include <ctime>
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <sstream>
 | 
			
		||||
 | 
			
		||||
#include "Logger.h"
 | 
			
		||||
 | 
			
		||||
void Logger::safeip(std::string ip) {
 | 
			
		||||
    std::ofstream out;
 | 
			
		||||
    out.open("ip.txt",std::ios::out);
 | 
			
		||||
 | 
			
		||||
    out << ip;
 | 
			
		||||
 | 
			
		||||
    out.close();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
std::string Logger::readip() {
 | 
			
		||||
    std::ifstream in;
 | 
			
		||||
    in.open("ip.txt",std::ios::in);
 | 
			
		||||
 | 
			
		||||
    std::string ip;
 | 
			
		||||
 | 
			
		||||
    in >> ip;
 | 
			
		||||
 | 
			
		||||
    return ip;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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();
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										18
									
								
								src/Logger.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								src/Logger.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,18 @@
 | 
			
		||||
//
 | 
			
		||||
// Created by lukas on 05.05.19.
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
#ifndef IPREFRESHER_LOGGER_H
 | 
			
		||||
#define IPREFRESHER_LOGGER_H
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Logger {
 | 
			
		||||
public:
 | 
			
		||||
    void logToLogfile(std::string text);
 | 
			
		||||
    void safeip(std::string ip);
 | 
			
		||||
    std::string readip();
 | 
			
		||||
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#endif //IPREFRESHER_LOGGER_H
 | 
			
		||||
							
								
								
									
										42
									
								
								src/main.cpp
									
									
									
									
									
								
							
							
						
						
									
										42
									
								
								src/main.cpp
									
									
									
									
									
								
							@@ -1,28 +1,44 @@
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <ctime>
 | 
			
		||||
#include "API.h"
 | 
			
		||||
#include "Logger.h"
 | 
			
		||||
 | 
			
		||||
int main() {
 | 
			
		||||
    std::cout << "Hello, World!" << std::endl;
 | 
			
		||||
 | 
			
		||||
    API api;
 | 
			
		||||
    Logger logger;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    std::string ip = api.request("https://api.ipify.org");
 | 
			
		||||
    std::cout << ip <<std::endl;
 | 
			
		||||
    std::cout << "ip is: " << ip <<std::endl;
 | 
			
		||||
 | 
			
		||||
    //api key: 88vNpMfDhMM2YYDNfWR1DNYfRX9W6fYg
 | 
			
		||||
    std::string oldip = logger.readip();
 | 
			
		||||
 | 
			
		||||
    Hashmap<std::string, std::string> args;
 | 
			
		||||
    if(oldip == ip){
 | 
			
		||||
        std::cout << "no change -- ip: " << ip << std::endl;
 | 
			
		||||
        logger.logToLogfile("no change -- ip: "+ip);
 | 
			
		||||
    } else{
 | 
			
		||||
        logger.logToLogfile("ip changed! -- from :" + oldip + "to: "+ip);
 | 
			
		||||
        std::cout << "ip changed! -- from :" << oldip << "to: " << ip << std::endl;
 | 
			
		||||
 | 
			
		||||
    args.add("name","luki.dynu.net");
 | 
			
		||||
    args.add("ipv4Address",ip);
 | 
			
		||||
        //api key: 88vNpMfDhMM2YYDNfWR1DNYfRX9W6fYg
 | 
			
		||||
 | 
			
		||||
    std::vector<std::string> headers;
 | 
			
		||||
    headers.push_back("accept: application/json");
 | 
			
		||||
    headers.push_back("User-Agent: Mozilla/5.0 (compatible; Rigor/1.0.0; http://rigor.com)");
 | 
			
		||||
    headers.push_back("API-Key: 88vNpMfDhMM2YYDNfWR1DNYfRX9W6fYg");
 | 
			
		||||
        Hashmap<std::string, std::string> args;
 | 
			
		||||
 | 
			
		||||
    std::string dynurepl = api.request("https://api.dynu.com/v2/dns/8506047",true, args,headers);
 | 
			
		||||
        args.add("name","luki.dynu.net");
 | 
			
		||||
        args.add("ipv4Address",ip);
 | 
			
		||||
 | 
			
		||||
        std::vector<std::string> headers;
 | 
			
		||||
        headers.push_back("accept: application/json");
 | 
			
		||||
        headers.push_back("User-Agent: Mozilla/5.0 (compatible; Rigor/1.0.0; http://rigor.com)");
 | 
			
		||||
        headers.push_back("API-Key: 88vNpMfDhMM2YYDNfWR1DNYfRX9W6fYg");
 | 
			
		||||
 | 
			
		||||
        std::string dynurepl = api.request("https://api.dynu.com/v2/dns/8506047",true, args,headers);
 | 
			
		||||
 | 
			
		||||
        std::cout << "---" << dynurepl << std::endl;
 | 
			
		||||
 | 
			
		||||
        logger.safeip(ip);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    std::cout << "---" << dynurepl << "\n";
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user