From 36bfc67574aaa54c3e7f814ec7c5336e9dbc1ac4 Mon Sep 17 00:00:00 2001 From: HybridDog Date: Sat, 7 Sep 2019 19:38:54 +0200 Subject: [PATCH] Move debug.txt after it grows too big (#8904) Before opening the file for writing, its file size is tested. If it exceeds 50 MB, it is moved to debut.txt.1, otherwise the log is appended to the old messages. An old debut.txt.1 is removed if it already exists. --- builtin/settingtypes.txt | 6 ++++++ src/defaultsettings.cpp | 1 + src/log.cpp | 26 +++++++++++++++++++++----- src/log.h | 2 +- src/main.cpp | 9 ++++----- 5 files changed, 33 insertions(+), 11 deletions(-) diff --git a/builtin/settingtypes.txt b/builtin/settingtypes.txt index 4254603e2..eefd3490c 100644 --- a/builtin/settingtypes.txt +++ b/builtin/settingtypes.txt @@ -1319,6 +1319,12 @@ language (Language) enum ,be,ca,cs,da,de,dv,en,eo,es,et,fr,he,hu,id,it,ja,jbo, # - verbose debug_log_level (Debug log level) enum action ,none,error,warning,action,info,verbose +# If the file size of debug.txt exceeds the number of megabytes specified in +# this setting when it is opened, the file is moved to debug.txt.1, +# deleting an older debug.txt.1 if it exists. +# debug.txt is only moved if this setting is positive. +debug_log_size_max (Debug log file size threshold) int 50 + # IPv6 support. enable_ipv6 (IPv6) bool true diff --git a/src/defaultsettings.cpp b/src/defaultsettings.cpp index 7756bc45d..0c51610c9 100644 --- a/src/defaultsettings.cpp +++ b/src/defaultsettings.cpp @@ -385,6 +385,7 @@ void set_default_settings(Settings *settings) settings->setDefault("ignore_world_load_errors", "false"); settings->setDefault("remote_media", ""); settings->setDefault("debug_log_level", "action"); + settings->setDefault("debug_log_size_max", "50"); settings->setDefault("emergequeue_limit_total", "512"); settings->setDefault("emergequeue_limit_diskonly", "64"); settings->setDefault("emergequeue_limit_generate", "64"); diff --git a/src/log.cpp b/src/log.cpp index c84a847a5..30344b4df 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -310,16 +310,32 @@ void Logger::logToOutputs(LogLevel lev, const std::string &combined, //// *LogOutput methods //// -void FileLogOutput::open(const std::string &filename) +void FileLogOutput::setFile(const std::string &filename, s64 file_size_max) { - m_stream.open(filename.c_str(), std::ios::app | std::ios::ate); + // Only move debug.txt if there is a valid maximum file size + bool is_too_large = false; + if (file_size_max > 0) { + std::ifstream ifile(filename, std::ios::binary | std::ios::ate); + is_too_large = ifile.tellg() > file_size_max; + ifile.close(); + } + + if (is_too_large) { + std::string filename_secondary = filename + ".1"; + actionstream << "The log file grew too big; it is moved to " << + filename_secondary << std::endl; + remove(filename_secondary.c_str()); + rename(filename.c_str(), filename_secondary.c_str()); + } + m_stream.open(filename, std::ios::app | std::ios::ate); + if (!m_stream.good()) throw FileNotGoodException("Failed to open log file " + filename + ": " + strerror(errno)); m_stream << "\n\n" - "-------------" << std::endl - << " Separator" << std::endl - << "-------------\n" << std::endl; + "-------------" << std::endl << + " Separator" << std::endl << + "-------------\n" << std::endl; } diff --git a/src/log.h b/src/log.h index 1427d1364..6350d8a86 100644 --- a/src/log.h +++ b/src/log.h @@ -165,7 +165,7 @@ private: class FileLogOutput : public ICombinedLogOutput { public: - void open(const std::string &filename); + void setFile(const std::string &filename, s64 file_size_max); void logRaw(LogLevel lev, const std::string &line) { diff --git a/src/main.cpp b/src/main.cpp index bd83f9638..6323fa50e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -172,7 +172,7 @@ int main(int argc, char *argv[]) } else { errorstream << "Invalid --worldlist value: " << cmd_args.get("worldlist") << std::endl; - return 1; + return 1; } return 0; } @@ -426,7 +426,7 @@ static bool setup_log_params(const Settings &cmd_args) } else { errorstream << "Invalid color mode: " << color_mode << std::endl; return false; - } + } } // If trace is enabled, enable logging of certain things @@ -580,9 +580,8 @@ static void init_log_streams(const Settings &cmd_args) "using maximum." << std::endl; } - verbosestream << "log_filename = " << log_filename << std::endl; - - file_log_output.open(log_filename); + file_log_output.setFile(log_filename, + g_settings->getU64("debug_log_size_max") * 1000000); g_logger.addOutputMaxLevel(&file_log_output, log_level); }