From 9d33964b938b8e76972ee2b53185877dda57eba1 Mon Sep 17 00:00:00 2001 From: lukas Date: Thu, 30 Apr 2020 12:16:29 +0200 Subject: [PATCH] remove logging to file added some doc --- inc/FileLogger.h | 8 ++------ inc/IPRefresher.h | 6 ++++++ inc/Logger.h | 2 +- src/Credentials.cpp | 1 + src/FileLogger.cpp | 21 --------------------- src/IPRefresher.cpp | 8 +------- src/Logger.cpp | 10 +++++----- 7 files changed, 16 insertions(+), 40 deletions(-) diff --git a/inc/FileLogger.h b/inc/FileLogger.h index 187863b..ee0c6ca 100644 --- a/inc/FileLogger.h +++ b/inc/FileLogger.h @@ -1,17 +1,13 @@ // // 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 diff --git a/inc/IPRefresher.h b/inc/IPRefresher.h index 7dcaf9c..c3e4b03 100644 --- a/inc/IPRefresher.h +++ b/inc/IPRefresher.h @@ -12,5 +12,11 @@ public: void checkIPAdress(bool force); IPRefresher(); + + /** + * start the service in loop mode + * every 5 minutes the ip is checked an refreshed (needed for .service) + * @param loop true->loopmode on + */ explicit IPRefresher(bool loop); }; diff --git a/inc/Logger.h b/inc/Logger.h index 2e8b042..9bfc56a 100644 --- a/inc/Logger.h +++ b/inc/Logger.h @@ -13,7 +13,7 @@ public: static void warning(std::string message); static void error(std::string message); - static void log(std::string &message, int level); + static void log(const std::string &message, int level); static const int Debug; static const int Message; diff --git a/src/Credentials.cpp b/src/Credentials.cpp index b944d3a..c1c124d 100644 --- a/src/Credentials.cpp +++ b/src/Credentials.cpp @@ -48,5 +48,6 @@ bool Credentials::readCredentials() { std::cerr << "No '" << nfex.getPath() << "' setting in configuration file." << std::endl; } } + // check if needed values aren't empty return !(Credentials::dynuapikey.empty() || Credentials::domainid.empty() || Credentials::domainname.empty()); } diff --git a/src/FileLogger.cpp b/src/FileLogger.cpp index 96c64d0..79b1a5d 100644 --- a/src/FileLogger.cpp +++ b/src/FileLogger.cpp @@ -3,9 +3,7 @@ // #include -#include #include -#include #include "FileLogger.h" @@ -28,22 +26,3 @@ std::string FileLogger::readip() { 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 6c22b99..c031ba6 100644 --- a/src/IPRefresher.cpp +++ b/src/IPRefresher.cpp @@ -24,16 +24,13 @@ void IPRefresher::checkIPAdress(bool force) { if (ip.empty()) { //no internet connection (or other error) - logger.logToLogfile("[WARNING] no internet connection"); Logger::warning("no internet connection"); } else { std::string oldip = logger.readip(); if (oldip == ip && !force) { Logger::message("no change -- ip: " + ip); - logger.logToLogfile(" [INFO] no change -- ip: " + ip); } else { - logger.logToLogfile(" [INFO] ip changed! -- from :" + oldip + "to: " + ip); Logger::message("ip changed! -- from :" + oldip + "to: " + ip); DynuAPI dynu; @@ -45,7 +42,6 @@ void IPRefresher::checkIPAdress(bool force) { tele.sendMessage(oldip + " moved to " + ip); } else { //error - logger.logToLogfile(" [ERROR] failed to write ip to dynu api!"); Logger::error("failed to write ip to dynu api!"); } @@ -54,9 +50,7 @@ void IPRefresher::checkIPAdress(bool force) { } } -IPRefresher::IPRefresher() { - // default constructor -} +IPRefresher::IPRefresher() = default; IPRefresher::IPRefresher(bool loop) { if (Credentials::readCredentials()) { diff --git a/src/Logger.cpp b/src/Logger.cpp index c6f11ff..5653cb3 100644 --- a/src/Logger.cpp +++ b/src/Logger.cpp @@ -13,23 +13,23 @@ const int Logger::Debug = 2; const int Logger::Message = 3; const int Logger::Error = 4; -void Logger::debug(std::string message) { +void Logger::debug(const std::string message) { log(message, Logger::Debug); } -void Logger::message(std::string message) { +void Logger::message(const std::string message) { log(message, Logger::Message); } -void Logger::warning(std::string message) { +void Logger::warning(const std::string message) { log(message, Logger::Warning); } -void Logger::error(std::string message) { +void Logger::error(const std::string message) { log(message, Logger::Error); } -void Logger::log(std::string &message, int level) { +void Logger::log(const std::string &message, int level) { std::stringstream out; out << "["; switch (level) {