Skip to content

Commit

Permalink
[sairedis] Perform log rotate on request (#1058)
Browse files Browse the repository at this point in the history
Previously log rotate was performed after request and on first log line
that was recorded which caused delay and issues with syslog logrotate
when sending HUP signal, actual log rotate was not performed and handle
to a sairedis.rec was sill open preventing logrotate from happening.
  • Loading branch information
kcudnik authored and yxieca committed Jul 17, 2022
1 parent cad0268 commit 58359d4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
23 changes: 8 additions & 15 deletions lib/Recorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,27 +179,20 @@ void Recorder::recordLine(
{
m_ofstream << getTimestamp() << "|" << line << std::endl;
}

if (m_performLogRotate)
{
m_performLogRotate = false;

recordingFileReopen();

/* double check since reopen could fail */

if (m_ofstream.is_open())
{
m_ofstream << getTimestamp() << "|" << "#|logrotate on: " << m_recordingFile << std::endl;
}
}
}

void Recorder::requestLogRotate()
{
SWSS_LOG_ENTER();

m_performLogRotate = true;
recordingFileReopen();

/* double check since reopen could fail */

if (m_ofstream.is_open())
{
m_ofstream << getTimestamp() << "|" << "#|logrotate on: " << m_recordingFile << std::endl;
}
}

void Recorder::recordingFileReopen()
Expand Down
1 change: 1 addition & 0 deletions unittest/lib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ tests_SOURCES = \
TestSkipRecordAttrContainer.cpp \
TestServerConfig.cpp \
TestRedisVidIndexGenerator.cpp \
TestRecorder.cpp \
TestRedisChannel.cpp

tests_CXXFLAGS = $(DBGFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS_COMMON)
Expand Down
28 changes: 28 additions & 0 deletions unittest/lib/TestRecorder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "Recorder.h"

#include <gtest/gtest.h>

#include <memory>

using namespace sairedis;

TEST(Recorder, requestLogRotate)
{
Recorder rec;

rec.enableRecording(true);

rec.recordComment("foo");

int code = rename("sairedis.rec", "sairedis.rec.1");

EXPECT_EQ(code, 0);

EXPECT_NE(access("sairedis.rec", F_OK),0);

rec.requestLogRotate();

EXPECT_EQ(access("sairedis.rec", F_OK),0);

rec.recordComment("bar");
}

0 comments on commit 58359d4

Please sign in to comment.