Skip to content

Commit

Permalink
common: micro optimize flush of logger buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
andrea-iob committed Jun 12, 2024
1 parent 8c7ce91 commit 0c25394
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions src/common/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ int LoggerBuffer::flush(bool terminate)
}

// Flush buffer a line at the time
static const std::string EMPTY_STRING = "";

const char *lineBegin = nullptr;
const char *lineEnd = bufferBegin;
while (lineEnd != bufferEnd) {
Expand All @@ -203,29 +205,36 @@ int LoggerBuffer::flush(bool terminate)
}

// Get timestamp
std::string consoleTimestamp;
std::string fileTimestamp;
std::string timestamp;
if (m_settings->consoleTimestampEnabled || m_settings->fileTimestampEnabled) {
std::string timestamp = getTimestamp();
if (m_settings->consoleTimestampEnabled) {
consoleTimestamp = timestamp;
}
if (m_settings->fileTimestampEnabled) {
fileTimestamp = timestamp;
}
timestamp = getTimestamp();
}

// Flush line to console
if (m_console && m_consoleEnabled) {
int status = flushLine(*m_console, lineBegin, lineEnd, consoleTimestamp, terminateLine);
const std::string *consoleTimestamp;
if (m_settings->consoleTimestampEnabled) {
consoleTimestamp = &timestamp;
} else {
consoleTimestamp = &EMPTY_STRING;
}

int status = flushLine(*m_console, lineBegin, lineEnd, *consoleTimestamp, terminateLine);
if (status != 0) {
return status;
}
}

// Flush line to file
if (m_file && m_fileEnabled && m_file->is_open()) {
int status = flushLine(*m_file, lineBegin, lineEnd, fileTimestamp, terminateLine);
const std::string *fileTimestamp;
if (m_settings->fileTimestampEnabled) {
fileTimestamp = &timestamp;
} else {
fileTimestamp = &EMPTY_STRING;
}

int status = flushLine(*m_file, lineBegin, lineEnd, *fileTimestamp, terminateLine);
if (status != 0) {
return status;
}
Expand Down

0 comments on commit 0c25394

Please sign in to comment.