Skip to content

Commit

Permalink
Fix filesink drop messages improved (#479)
Browse files Browse the repository at this point in the history
* Fix FileSink dropping messages introduced by 'optionable buffer to x messages' feature
Commit 6c6122f introduced a bug where 99 out of 100 logs were actually dumped

* Updated unit tests to trigger sink flush bug
---------

Co-authored-by: Ryan Ammoury <[email protected]>
  • Loading branch information
KjellKod and ryanammoury authored Feb 20, 2023
1 parent 43f5edd commit 4f1224b
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ endif()

# Calculate the version number
SET(MAJOR_VERSION 2)
SET(MINOR_VERSION 2)
SET(MINOR_VERSION 3)

IF ( NOT VERSION )
IF ( "${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Windows" )
Expand Down
3 changes: 1 addition & 2 deletions src/filesink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,11 @@ namespace g3 {
_firstEntry = false;
}

std::ofstream &out(filestream());
auto data = message.get().toString(_log_details_func);

_write_buffer.append(data);
if (++_write_counter % _write_to_log_every_x_message == 0) {
out << message.get().toString(_log_details_func) << std::flush;
filestream() << _write_buffer << std::flush;
_write_buffer.clear();
}
}
Expand Down
4 changes: 4 additions & 0 deletions test_unit/test_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,16 @@ TEST(Basics, Shutdown) {
std::string file_content;
{
RestoreFileLogger logger(log_directory);
LOG(INFO) << "First message buffered, then flushed";
LOG(INFO) << "Second message still in the buffer";
LOG(INFO) << "Not yet shutdown. This message should make it";
logger.reset(); // force flush of logger (which will trigger a shutdown)
LOG(INFO) << "Logger is shutdown,. this message will not make it (but it's safe to try)";
file_content = readFileToText(logger.logFile()); // logger is already reset
SCOPED_TRACE("LOG_INFO"); // Scope exit be prepared for destructor failure
}
EXPECT_TRUE(verifyContent(file_content, "First message buffered, then flushed"));
EXPECT_TRUE(verifyContent(file_content, "Second message still in the buffer"));
EXPECT_TRUE(verifyContent(file_content, "Not yet shutdown. This message should make it"));
EXPECT_FALSE(verifyContent(file_content, "Logger is shutdown,. this message will not make it (but it's safe to try)"));
}
Expand Down
8 changes: 5 additions & 3 deletions test_unit/testing_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ namespace testing_helpers {
std::string g_mockFatal_message = {};
int g_mockFatal_signal = -1;
bool g_mockFatalWasCalled = false;

const size_t kFlushToDiskWithThisInterval = 2;

std::string mockFatalMessage() {
return g_mockFatal_message;
}
Expand Down Expand Up @@ -110,7 +111,8 @@ namespace testing_helpers {
}

RestoreFileLogger::RestoreFileLogger(std::string directory)
: _scope(new ScopedLogger), _handle(_scope->get()->addSink(std::make_unique<g3::FileSink>("UNIT_TEST_LOGGER", directory), &g3::FileSink::fileWrite)) {
: _scope(new ScopedLogger), _handle(_scope->get()->addSink(std::make_unique<g3::FileSink>("UNIT_TEST_LOGGER",
directory, "g3log", kFlushToDiskWithThisInterval), &g3::FileSink::fileWrite)) {
using namespace g3;
g3::initializeLogging(_scope->_currentWorker.get());
clearMockFatal();
Expand All @@ -133,7 +135,7 @@ namespace testing_helpers {
reset();

if (!removeFile(_log_file))
ADD_FAILURE();
ADD_FAILURE();
}

std::string RestoreFileLogger::logFile() {
Expand Down

0 comments on commit 4f1224b

Please sign in to comment.