Skip to content
Permalink
Browse files

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.
  • Loading branch information...
HybridDog authored and SmallJoker committed Sep 7, 2019
1 parent 2c9edef commit 36bfc67574aaa54c3e7f814ec7c5336e9dbc1ac4
Showing with 33 additions and 11 deletions.
  1. +6 −0 builtin/settingtypes.txt
  2. +1 −0 src/defaultsettings.cpp
  3. +21 −5 src/log.cpp
  4. +1 −1 src/log.h
  5. +4 −5 src/main.cpp
@@ -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

@@ -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");
@@ -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";

This comment has been minimized.

Copy link
@theFox6

theFox6 Sep 8, 2019

Something like debug.1.txt would have probably been better because this changes the file's extension.

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;
}


@@ -165,7 +165,7 @@ class StreamLogOutput : public ICombinedLogOutput {

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)
{
@@ -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);
}

0 comments on commit 36bfc67

Please sign in to comment.
You can’t perform that action at this time.