From a38491d5878bf4b0ab96f2ecb162bf8859f310a0 Mon Sep 17 00:00:00 2001 From: Markus Date: Mon, 17 Feb 2020 20:17:19 +0100 Subject: [PATCH] v3.8.0: add subfuncktion: simple log system --- src/S_New4.pro | 6 ++- src/logger.cpp | 84 ++++++++++++++++++++++++++++++++++++++++ src/logger.h | 31 +++++++++++++++ src/parameterManager.cpp | 84 +++++++++++++++++++++++++++++++++++++++- src/parameterManager.h | 10 ++++- src/programManager.cpp | 5 +-- 6 files changed, 212 insertions(+), 8 deletions(-) create mode 100644 src/logger.cpp create mode 100644 src/logger.h diff --git a/src/S_New4.pro b/src/S_New4.pro index 39bfb66..5164207 100644 --- a/src/S_New4.pro +++ b/src/S_New4.pro @@ -26,7 +26,8 @@ SOURCES += \ pageManager.cpp \ parameterManager.cpp \ programManager.cpp \ - accountManager.cpp + accountManager.cpp \ + logger.cpp # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin @@ -37,4 +38,5 @@ HEADERS += \ pageManager.h \ parameterManager.h \ programManager.h \ - accountManager.h + accountManager.h \ + logger.h diff --git a/src/logger.cpp b/src/logger.cpp new file mode 100644 index 0000000..a5e9a56 --- /dev/null +++ b/src/logger.cpp @@ -0,0 +1,84 @@ +#include "logger.h" + + + +Logger::Logger(std::string logFilePath) + : filePath(logFilePath) +{ + if(openFile(filePath)) + exit(234); +} + +Logger::~Logger() +{ + file.close(); +} + +std::string Logger::getFilePath() +{ + return filePath; +} + +int Logger::logCommandLine(std::string logText) +{ + std::time_t now = std::time(nullptr); + struct tm *tm_now = localtime(&now); + std::string date(std::to_string(tm_now->tm_mday) + "." + std::to_string(tm_now->tm_mon +1) + "." + + std::to_string(1900 + tm_now->tm_year) + " - " + std::to_string(tm_now->tm_hour) + ":" + std::to_string(tm_now->tm_min)); + return writetoFile(date + ": \"" + logText + "\""); +} + +int Logger::logSubLine(std::string line) +{ + return writetoFile("\t\t" + line); +} + +std::string Logger::getLogText() +{ + return std::string((std::istreambuf_iterator(file)), std::istreambuf_iterator()); +} + +int Logger::clearFile() +{ + //close it + file.close(); + + //open it empty + close it + std::ofstream ofs; + ofs.open(filePath, std::ofstream::out | std::ofstream::trunc); + if(!ofs.is_open()) { + perror("Open File for clearing failed"); + return 35; + } + ofs.close(); + + //reopen it + return openFile(filePath); +} + +int Logger::writetoFile(std::string t) +{ + file << t << std::endl; + return file.good(); +} + +int Logger::openFile(std::string path) +{ + file.open(path, std::ios::in | std::ios::out | std::ios::app); + if(!file.is_open()) { + //Exestiert nicht => Erstelle Datei + std::ofstream ofs(path); + if(!ofs.is_open()) { + perror((" => Error: Konnte LogDatei nicht erstellen: '" + path + "'").c_str()); + return (5656); + } else { + ofs.close(); + } + file.open(path); + if(!file.is_open()) { + perror((" => Error: Konnte LogDatei nicht öffnen: '" + path + "'").c_str()); + return (5657); + } + } + return 0; +} diff --git a/src/logger.h b/src/logger.h new file mode 100644 index 0000000..03a90d6 --- /dev/null +++ b/src/logger.h @@ -0,0 +1,31 @@ +#ifndef LOGGER_H +#define LOGGER_H + +#include +#include +#include +#include +#include // std::time, localtime ... + + + +class Logger +{ +public: + Logger(std::string logFilePath); + ~Logger(); + std::string getFilePath(); + std::fstream file; + + int logCommandLine(std::string logText); + int logSubLine(std::string line); + std::string getLogText(); + int clearFile(); + +private: + std::string filePath; + int writetoFile(std::string t); + int openFile(std::string path); +}; + +#endif // LOGGER_H diff --git a/src/parameterManager.cpp b/src/parameterManager.cpp index 940f00b..23fc7f9 100644 --- a/src/parameterManager.cpp +++ b/src/parameterManager.cpp @@ -38,6 +38,7 @@ int setPaths(Settings &settings) settings.cookieFilePath = CacheDir + "S_New4_cookies"; settings.accountNumberPath = CacheDir + "Account_Number"; settings.lastUpdateDateFilePath = CacheDir + "LastUpdateDate"; + settings.logFilePath = CacheDir + "LogFile"; settings.accountFilePath = SettingsDir + "Accounts"; settings.serienListPath = SettingsDir + "SerienListe"; @@ -69,7 +70,19 @@ int manageParameter(Settings &settings, int argc, char **argv) return 1; } - std::vector res = compare("help\n--help\ndefault\nurl\n--version\nsearch\ninfo\nclean\nnews\n--update\n--remove", argv[1]); + //LogCommand... + Logger logger(settings.logFilePath); + std::string command; + for (int i = 0; i < argc; ++i) + if(argv[i]) + command += std::string(argv[i]) + " "; + if(command.length() > 0) + command.pop_back(); + logger.logCommandLine(command); + + //Find out right subProgramm + + std::vector res = compare("help\n--help\ndefault\nurl\n--version\nsearch\ninfo\nclean\nnews\n--update\n--remove\nlog", argv[1]); if(res.size() != 1) { if(res.size() == 0) std::cout << " => Error: Keine Unteroption für " << argv[1] << " gefunden." << std::endl; @@ -118,6 +131,12 @@ int manageParameter(Settings &settings, int argc, char **argv) } else if (isSame(argv, "--remove")) { return unterOption_RemoveSettings_or_CacheDir(&settings, argc, argv); + } else if (isSame(argv, "log")) { + if(unterOption_printLogFile(&settings, argc, argv) != -1) + return 49; + logger.logCommandLine(command); + return -1; + } else { std::cout << " => Error: Invalid option '" << argv[1] << "', but not detected in compare-Function" << std::endl; return 3; @@ -353,6 +372,7 @@ int unterOption_help(Settings &settings) << "\t\"info\"\t\tModus um Infos einer Serien zu bekommen." << std::endl << "\t\"clean\"\t\tModus um Cookie-Files zu löschen." << std::endl << "\t\"news\"\t\tModus um neusten 75 Folgen auf s.to zu sehen." << std::endl + << "\t\"log\"\t\tModus um Log Datei zu sehen / bearbeiten." << std::endl << std::endl; std::cout << "Verzeichnisse:" << std::endl @@ -1436,3 +1456,65 @@ bool isSame(char **argv, std::string FunktionName) } return false; } + +int unterOption_printLogFile(Settings *settings, int argc, char **argv) +{ + settings->modus = Modus::PRINT_LOG_FILE_MODUS; + + int c = 0; + const option long_opts[] = { + {"help", no_argument, nullptr, 'h'}, + {"print", no_argument, nullptr, 'p'}, + {"clear", no_argument, nullptr, 'c'}, + {nullptr, no_argument, nullptr, 0} + + }; + + bool printMode = false, ClearMode = false; + + while( ( c = getopt_long (argc, argv, "hpc", long_opts, nullptr) ) != -1 ) { + switch(c) { + + case 'h': + unterOption_printLogFile(settings->programName); + return -1; + case 'p': + printMode = true; + break; + case 'c': + ClearMode = true; + break; + default: + std::cout << "Aufruf: " << settings->programName << " log [OPTION]..." << std::endl; + std::cout << "\"" << settings->programName << " log --help\" liefert weitere Informationen." << std::endl; + return 21; + } + } + + if(!printMode && !ClearMode) { + std::cout << " => Error: Fehlende Parameter: -p / -c." << std::endl; + std::cout << "\"" << settings->programName << " log --help\" liefert weitere Informationen." << std::endl; + return 34; + } + + if(printMode) + std::cout << Logger(settings->logFilePath).getLogText() << std::endl; + if(ClearMode) + if(Logger(settings->logFilePath).clearFile()) + return 12; + return -1; +} + +void unterOption_printLogFile(std::string programName) +{ + std::cout << "Aufruf: " << programName << " log [OPTION]..." << std::endl << std::endl; + + std::cout << "Beschreibung:" << std::endl + << " Mit dieser Unterfunktion kann man die Log Datei sehen/bearbeiten." << std::endl << std::endl; + + std::cout << "OPTIONEN:" << std::endl + << " -p, --print" << std::endl + << " Mit dieser Option wird das LogFile ausgegeben." << std::endl + << " -c, --clear" << std::endl + << " Mit dieser Option wird das LogFile geleert." << std::endl; +} diff --git a/src/parameterManager.h b/src/parameterManager.h index f50388f..dbc4d07 100644 --- a/src/parameterManager.h +++ b/src/parameterManager.h @@ -19,6 +19,7 @@ #include // readlink() #include +#include "logger.h" #ifdef _WIN32 #include @@ -32,7 +33,8 @@ enum Modus { INFO_MODUS = 3, NEWS_MODUS = 4, UPDATE_MODUS = 5, - REMOVE_SETTINGS_AND_CACHE_MODUS = 6 + REMOVE_SETTINGS_AND_CACHE_MODUS = 6, + PRINT_LOG_FILE_MODUS }; @@ -76,12 +78,13 @@ struct Settings { lastUpdateDateFilePath = "", configDir="", cacheDir="", + logFilePath="", defaultsFilePath = "", proxy_ip = "127.0.0.1", languages = "GerDub,GerSub,Eng,", genaueHoster = "", - version = "3.7.8", + version = "3.8.0", defaultFileVersion="1.7", outputFilePath = "", default_checkPath = "", @@ -149,6 +152,9 @@ void unterOption_update_help(std::string programName); int unterOption_RemoveSettings_or_CacheDir(Settings * settings, int argc, char **argv); void unterOption_RemoveSettings_or_CacheDire_help(std::string programName); +int unterOption_printLogFile(Settings * settings, int argc, char **argv); +void unterOption_printLogFile(std::string programName); + int setS5ProxytoSettings(Settings &settings, std::string optarg); diff --git a/src/programManager.cpp b/src/programManager.cpp index 1fa2126..495df45 100644 --- a/src/programManager.cpp +++ b/src/programManager.cpp @@ -423,9 +423,8 @@ int ProgramManager::defaultModus(Settings *settings) //Finde die anzahl der staffel heraus: //download html von der startpage einer serie Reply tmp_reply = pageManager.getServerRequest(pageManager.UrlPraefix + nameInUrl); - if(tmp_reply.html == "-1") { - if(settings->debugMode) - std::cerr << ">>> Debug In " << __FUNCTION__ << ": getServerRequest failed, when download startpage." << std::endl; + if(tmp_reply.html == "-1" || tmp_reply.html == "") { + std::cerr << " => Error: Konnte Seitenquelltext nicht herunterladen." << std::endl; return 32; }