Skip to content

Commit 36bfc67

Browse files
HybridDogSmallJoker
authored andcommittedSep 7, 2019
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.
1 parent 2c9edef commit 36bfc67

File tree

5 files changed

+33
-11
lines changed

5 files changed

+33
-11
lines changed
 

‎builtin/settingtypes.txt

+6
Original file line numberDiff line numberDiff line change
@@ -1319,6 +1319,12 @@ language (Language) enum ,be,ca,cs,da,de,dv,en,eo,es,et,fr,he,hu,id,it,ja,jbo,
13191319
# - verbose
13201320
debug_log_level (Debug log level) enum action ,none,error,warning,action,info,verbose
13211321

1322+
# If the file size of debug.txt exceeds the number of megabytes specified in
1323+
# this setting when it is opened, the file is moved to debug.txt.1,
1324+
# deleting an older debug.txt.1 if it exists.
1325+
# debug.txt is only moved if this setting is positive.
1326+
debug_log_size_max (Debug log file size threshold) int 50
1327+
13221328
# IPv6 support.
13231329
enable_ipv6 (IPv6) bool true
13241330

‎src/defaultsettings.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ void set_default_settings(Settings *settings)
385385
settings->setDefault("ignore_world_load_errors", "false");
386386
settings->setDefault("remote_media", "");
387387
settings->setDefault("debug_log_level", "action");
388+
settings->setDefault("debug_log_size_max", "50");
388389
settings->setDefault("emergequeue_limit_total", "512");
389390
settings->setDefault("emergequeue_limit_diskonly", "64");
390391
settings->setDefault("emergequeue_limit_generate", "64");

‎src/log.cpp

+21-5
Original file line numberDiff line numberDiff line change
@@ -310,16 +310,32 @@ void Logger::logToOutputs(LogLevel lev, const std::string &combined,
310310
//// *LogOutput methods
311311
////
312312

313-
void FileLogOutput::open(const std::string &filename)
313+
void FileLogOutput::setFile(const std::string &filename, s64 file_size_max)
314314
{
315-
m_stream.open(filename.c_str(), std::ios::app | std::ios::ate);
315+
// Only move debug.txt if there is a valid maximum file size
316+
bool is_too_large = false;
317+
if (file_size_max > 0) {
318+
std::ifstream ifile(filename, std::ios::binary | std::ios::ate);
319+
is_too_large = ifile.tellg() > file_size_max;
320+
ifile.close();
321+
}
322+
323+
if (is_too_large) {
324+
std::string filename_secondary = filename + ".1";
325+
actionstream << "The log file grew too big; it is moved to " <<
326+
filename_secondary << std::endl;
327+
remove(filename_secondary.c_str());
328+
rename(filename.c_str(), filename_secondary.c_str());
329+
}
330+
m_stream.open(filename, std::ios::app | std::ios::ate);
331+
316332
if (!m_stream.good())
317333
throw FileNotGoodException("Failed to open log file " +
318334
filename + ": " + strerror(errno));
319335
m_stream << "\n\n"
320-
"-------------" << std::endl
321-
<< " Separator" << std::endl
322-
<< "-------------\n" << std::endl;
336+
"-------------" << std::endl <<
337+
" Separator" << std::endl <<
338+
"-------------\n" << std::endl;
323339
}
324340

325341

‎src/log.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ class StreamLogOutput : public ICombinedLogOutput {
165165

166166
class FileLogOutput : public ICombinedLogOutput {
167167
public:
168-
void open(const std::string &filename);
168+
void setFile(const std::string &filename, s64 file_size_max);
169169

170170
void logRaw(LogLevel lev, const std::string &line)
171171
{

‎src/main.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ int main(int argc, char *argv[])
172172
} else {
173173
errorstream << "Invalid --worldlist value: "
174174
<< cmd_args.get("worldlist") << std::endl;
175-
return 1;
175+
return 1;
176176
}
177177
return 0;
178178
}
@@ -426,7 +426,7 @@ static bool setup_log_params(const Settings &cmd_args)
426426
} else {
427427
errorstream << "Invalid color mode: " << color_mode << std::endl;
428428
return false;
429-
}
429+
}
430430
}
431431

432432
// If trace is enabled, enable logging of certain things
@@ -580,9 +580,8 @@ static void init_log_streams(const Settings &cmd_args)
580580
"using maximum." << std::endl;
581581
}
582582

583-
verbosestream << "log_filename = " << log_filename << std::endl;
584-
585-
file_log_output.open(log_filename);
583+
file_log_output.setFile(log_filename,
584+
g_settings->getU64("debug_log_size_max") * 1000000);
586585
g_logger.addOutputMaxLevel(&file_log_output, log_level);
587586
}
588587

0 commit comments

Comments
 (0)