Added autosave to loghistory

This commit is contained in:
Leon Etienne (ubuntu wsl) 2020-09-27 13:59:13 +02:00
parent a19d27203f
commit feb2d92e38
7 changed files with 47 additions and 7 deletions

View File

@ -105,9 +105,10 @@ void DownloadManager::Update()
DownloadNext(); DownloadNext();
} }
// Check every second, non-blocking // Update every 1-2 seconds, non-blocking, the download progresses
if ((time(0) - lastProgressCheck > 2) && (cachedNumActiveDownloads > 0)) if ((time(0) - lastProgressCheck > 2) && (cachedNumActiveDownloads > 0))
{ {
// Mutex gets reset in Save();
UpdateDownloadProgressPercentages(); UpdateDownloadProgressPercentages();
} }

View File

@ -42,8 +42,7 @@ void Framework::Run()
{ {
while (XGControl::keepServerRunning) while (XGControl::keepServerRunning)
{ {
httpServer->Update(); Update();
DownloadManager::Update();
} }
OnExit(); OnExit();
@ -74,6 +73,15 @@ void Framework::PostInit()
return; return;
} }
void Framework::Update()
{
httpServer->Update();
DownloadManager::Update();
LogHistory::Update();
return;
}
void Framework::OnExit() void Framework::OnExit()
{ {
httpServer->OnExit(); httpServer->OnExit();
@ -86,9 +94,9 @@ void Framework::PostExit()
{ {
XGConfig::PostExit(); XGConfig::PostExit();
RestQueryHandler::PostExit(); RestQueryHandler::PostExit();
LogHistory::PostExit();
DownloadManager::PostExit(); DownloadManager::PostExit();
ConsoleManager::PostExit(); ConsoleManager::PostExit();
LogHistory::PostExit();
return; return;
} }

View File

@ -15,9 +15,10 @@ public:
void Run(); void Run();
private: private:
void PostInit();
void OnExit();
void PreInit(); void PreInit();
void PostInit();
void Update();
void OnExit();
void PostExit(); void PostExit();
Rest::HttpServer* httpServer; Rest::HttpServer* httpServer;

View File

@ -5,6 +5,8 @@ using namespace Logging;
void LogHistory::PreInit() void LogHistory::PreInit()
{ {
history = new std::vector<LogEntry*>(); history = new std::vector<LogEntry*>();
lastSave = time(0);
didHistoryChangeSinceLastSave = false;
return; return;
} }
@ -26,6 +28,17 @@ void LogHistory::PostExit()
return; return;
} }
void LogHistory::Update()
{
if ((time(0) - lastSave > XGConfig::logging.autosave_interval) && (didHistoryChangeSinceLastSave))
{
// Mutex gets reset in Save();
Save();
}
return;
}
void LogHistory::Save() void LogHistory::Save()
{ {
std::stringstream textfile; std::stringstream textfile;
@ -46,17 +59,23 @@ void LogHistory::Save()
ofs << jsonFile.Render(); ofs << jsonFile.Render();
ofs.close(); ofs.close();
lastSave = time(0);
didHistoryChangeSinceLastSave = false;
return; return;
} }
void LogHistory::AddLogToHistory(LogEntry* newEntry) void LogHistory::AddLogToHistory(LogEntry* newEntry)
{ {
history->push_back(newEntry); history->push_back(newEntry);
didHistoryChangeSinceLastSave = true;
return; return;
} }
std::vector<LogEntry*>* LogHistory::history; std::vector<LogEntry*>* LogHistory::history;
time_t LogHistory::lastSave;
bool LogHistory::didHistoryChangeSinceLastSave;
JasonPP::JsonBlock LogEntry::GetAsJson() JasonPP::JsonBlock LogEntry::GetAsJson()
{ {

View File

@ -29,6 +29,7 @@ namespace Logging
public: public:
static void PreInit(); static void PreInit();
static void PostExit(); static void PostExit();
static void Update();
static void Save(); static void Save();
@ -38,6 +39,8 @@ namespace Logging
static void AddLogToHistory(LogEntry* newEntry); static void AddLogToHistory(LogEntry* newEntry);
static std::vector<LogEntry*>* history; static std::vector<LogEntry*>* history;
static time_t lastSave;
static bool didHistoryChangeSinceLastSave;
friend Logger; friend Logger;
}; };

View File

@ -38,6 +38,7 @@ void XGConfig::InitializeDefaultValues()
logging.logfile_text = "log.txt"; logging.logfile_text = "log.txt";
logging.logfile_json = "log.json"; logging.logfile_json = "log.json";
logging.autosave_interval = 60 * 30; // 30 mins
downloader.cachedir = "dlcache"; downloader.cachedir = "dlcache";
downloader.max_dlrate_per_thread = "100M"; downloader.max_dlrate_per_thread = "100M";
@ -65,6 +66,11 @@ void XGConfig::LoadFromJson(const JasonPP::JsonBlock& json)
logging.logfile_text = json.ShorthandGet("logging.logfile_text").AsString; logging.logfile_text = json.ShorthandGet("logging.logfile_text").AsString;
} }
if (IsJsonFieldValid(json, "logging.autosave_interval", JDType::INT))
{
logging.autosave_interval = json.ShorthandGet("logging.autosave_interval").AsInt;
}
if (IsJsonFieldValid(json, "httpServer.port", JDType::STRING)) if (IsJsonFieldValid(json, "httpServer.port", JDType::STRING))
@ -132,7 +138,8 @@ JsonBlock XGConfig::CreateJson()
})), })),
Ele("logging", JsonBlock({ Ele("logging", JsonBlock({
Ele("logfile_text", logging.logfile_text), Ele("logfile_text", logging.logfile_text),
Ele("logfile_json", logging.logfile_json) Ele("logfile_json", logging.logfile_json),
Ele("autosave_interval", logging.autosave_interval)
})), })),
Ele("downloader", JsonBlock({ Ele("downloader", JsonBlock({
Ele("cachedir", downloader.cachedir), Ele("cachedir", downloader.cachedir),

View File

@ -19,6 +19,7 @@ public:
{ {
std::string logfile_text; std::string logfile_text;
std::string logfile_json; std::string logfile_json;
int autosave_interval;
}; };
struct Downloader struct Downloader
{ {