Skip to content

Commit

Permalink
add LogFlush config var for instant writes (disabled as default) (#2796)
Browse files Browse the repository at this point in the history
 - remove deprecated LogSubsystems config var
 - config descriptions
  • Loading branch information
abma committed Jun 16, 2012
1 parent 2919f4e commit 32a6242
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 20 deletions.
2 changes: 2 additions & 0 deletions doc/changelog.txt
Expand Up @@ -17,6 +17,8 @@ Changes:
! remove selectkeys.txt support (use uikeys.txt instead)
! remove TASServer.jar (outdated, use http://springrts.com/wiki/Spring_on_a_LAN )
! send gameID and SDF filename with SERVER_STARTPLAYING to autohost
! remove deprecated LogSubsystems config var
- add LogFlush config var for instant writes (disabled as default)

Bugfixes:
- dedicated server no longer consumes 100% CPU while waiting for gameID
Expand Down
21 changes: 12 additions & 9 deletions rts/System/Log/FileSink.cpp
Expand Up @@ -20,10 +20,11 @@ namespace {

struct LogFileDetails {
LogFileDetails(FILE* outStream = NULL, const std::string& sections = "",
int minLevel = LOG_LEVEL_ALL)
int minLevel = LOG_LEVEL_ALL, bool flush = true)
: outStream(outStream)
, sections(sections)
, minLevel(minLevel)
, flush(flush)
{}

FILE* GetOutStream() const {
Expand All @@ -35,11 +36,15 @@ namespace {
|| (sections.find("," + std::string(section) + ",")
!= std::string::npos)));
}
bool IsFlush() const{
return flush;
}

private:
FILE* outStream;
std::string sections;
int minLevel;
bool flush;
};
typedef std::map<std::string, LogFileDetails> logFiles_t;

Expand Down Expand Up @@ -105,17 +110,15 @@ namespace {
return (!log_file_getLogFiles().empty());
}

void log_file_writeToFile(FILE* outStream, const char* record) {
void log_file_writeToFile(FILE* outStream, const char* record, bool flush) {

char framePrefix[128] = {'\0'};
log_framePrefixer_createPrefix(framePrefix, sizeof(framePrefix));

FPRINTF(outStream, "%s%s\n", framePrefix, record);

// We never flush, but only close the stream before process exit.
// This decision was made in two engine dev meetings, the last one was
// at 26. September 2011.
//fflush(outStream);
if (flush)
fflush(outStream);
}

/**
Expand All @@ -130,7 +133,7 @@ namespace {
if (lfi->second.IsLogging(section, level)
&& (lfi->second.GetOutStream() != NULL))
{
log_file_writeToFile(lfi->second.GetOutStream(), record);
log_file_writeToFile(lfi->second.GetOutStream(), record, lfi->second.IsFlush());
}
}
}
Expand Down Expand Up @@ -182,7 +185,7 @@ namespace {
extern "C" {
#endif

void log_file_addLogFile(const char* filePath, const char* sections, int minLevel) {
void log_file_addLogFile(const char* filePath, const char* sections, int minLevel, bool flush) {

assert(filePath != NULL);

Expand All @@ -209,7 +212,7 @@ void log_file_addLogFile(const char* filePath, const char* sections, int minLeve
setvbuf(tmpStream, NULL, _IOFBF, (BUFSIZ < 8192) ? BUFSIZ : 8192); // limit buffer to 8kB

const std::string sectionsStr = (sections == NULL) ? "" : sections;
logFiles[filePathStr] = LogFileDetails(tmpStream, sectionsStr, minLevel);
logFiles[filePathStr] = LogFileDetails(tmpStream, sectionsStr, minLevel, flush);
}

void log_file_removeLogFile(const char* filePath) {
Expand Down
3 changes: 2 additions & 1 deletion rts/System/Log/FileSink.h
Expand Up @@ -26,10 +26,11 @@ extern "C" {
* @param filePath the path to the log file
* @param sections A list of comma separated sections to log to the file or
* NULL to log everything. To include the default section, you have to include
* @param flush instantly flush the logfile after data was written, will cause a slowdown
* ",,".
*/
void log_file_addLogFile(const char* filePath, const char* sections = NULL,
int minLevel = LOG_LEVEL_ALL);
int minLevel = LOG_LEVEL_ALL, bool flush = false);

void log_file_removeLogFile(const char* filePath);

Expand Down
18 changes: 8 additions & 10 deletions rts/System/LogOutput.cpp
Expand Up @@ -34,9 +34,12 @@
/******************************************************************************/
/******************************************************************************/

CONFIG(std::string, RotateLogFiles).defaultValue("auto");
CONFIG(std::string, LogSections).defaultValue("");
CONFIG(std::string, LogSubsystems).defaultValue(""); // XXX deprecated on 22. August 2011, before the 0.83 release
CONFIG(std::string, RotateLogFiles).defaultValue("auto")
.description("rotate logfiles, valid values are \"always\" (default in debug builds) and \"never\" (default in release builds).");
CONFIG(std::string, LogSections).defaultValue("")
.description("Comma seperated list of enabled logsections, see infolog.txt / console output for possible values");
CONFIG(bool, LogFlush).defaultValue(false)
.description("Instantly write to the logfile, use only for debugging as it will cause a slowdown");

/******************************************************************************/
/******************************************************************************/
Expand Down Expand Up @@ -158,7 +161,8 @@ void CLogOutput::Initialize()
/*filelog = new std::ofstream(filePath.c_str());
if (filelog->bad())
SafeDelete(filelog);*/
log_file_addLogFile(filePath.c_str());
const bool flush = configHandler->GetBool("LogFlush");
log_file_addLogFile(filePath.c_str(), NULL, LOG_LEVEL_ALL, flush);

initialized = true;
InitializeSections();
Expand Down Expand Up @@ -214,20 +218,14 @@ void CLogOutput::InitializeSections()
enabledSections += "Sound,";
#endif
enabledSections += StringToLower(configHandler->GetString("LogSections")) + ",";
enabledSections += StringToLower(configHandler->GetString("LogSubsystems")) + ","; // XXX deprecated on 22. August 2011, before the 0.83 release
#endif

const char* const envSec = getenv("SPRING_LOG_SECTIONS");
const char* const envSubsys = getenv("SPRING_LOG_SUBSYSTEMS"); // XXX deprecated on 22. August 2011, before the 0.83 release
std::string env;
if (envSec != NULL) {
env += ",";
env += envSec;
}
if (envSubsys != NULL) {
env += ",";
env += envSubsys;
}

if (!env.empty()) {
// this allows to disable all sections from the env var
Expand Down

1 comment on commit 32a6242

@FLOZi
Copy link
Contributor

@FLOZi FLOZi commented on 32a6242 Jun 16, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! x x x x x x

(I'll try and refrain from making snide comments about how this could have been done from the beginning... whoops looks like I just did ;)

Please sign in to comment.