diff options
author | HybridDog <ovvv@web.de> | 2019-09-07 19:38:54 +0200 |
---|---|---|
committer | SmallJoker <SmallJoker@users.noreply.github.com> | 2019-09-07 19:38:54 +0200 |
commit | 36bfc67574aaa54c3e7f814ec7c5336e9dbc1ac4 (patch) | |
tree | ec840ecfa24a1353b01cf5a477d111a7c8b8ea78 /src/log.cpp | |
parent | 2c9edefde3d941b1efa5c93a087814d6bc3509e6 (diff) | |
download | minetest-36bfc67574aaa54c3e7f814ec7c5336e9dbc1ac4.tar.gz minetest-36bfc67574aaa54c3e7f814ec7c5336e9dbc1ac4.tar.bz2 minetest-36bfc67574aaa54c3e7f814ec7c5336e9dbc1ac4.zip |
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.
Diffstat (limited to 'src/log.cpp')
-rw-r--r-- | src/log.cpp | 26 |
1 files changed, 21 insertions, 5 deletions
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; } |