Skip to content

Commit

Permalink
support logrotate for swss recording log (sonic-net#244)
Browse files Browse the repository at this point in the history
* support logrotate for swss recording log
  • Loading branch information
lguohan authored Jun 19, 2017
1 parent 002adc7 commit 316b333
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
6 changes: 4 additions & 2 deletions orchagent/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ int gBatchSize = DEFAULT_BATCH_SIZE;

bool gSairedisRecord = true;
bool gSwssRecord = true;
bool gLogRotate = false;
ofstream gRecordOfs;
string gRecordFile;

Expand Down Expand Up @@ -76,6 +77,7 @@ void sighup_handler(int signo)
/*
* Don't do any logging since they are using mutexes.
*/
gLogRotate = true;

sai_attribute_t attr;

Expand Down Expand Up @@ -197,8 +199,8 @@ int main(int argc, char **argv)
/* Disable/enable SwSS recording */
if (gSwssRecord)
{
gRecordFile = "swss." + getTimestamp() + ".rec";
gRecordOfs.open(record_location + "/" + gRecordFile);
gRecordFile = record_location + "/" + "swss." + getTimestamp() + ".rec";
gRecordOfs.open(gRecordFile);
if (!gRecordOfs.is_open())
{
SWSS_LOG_ERROR("Failed to open SwSS recording file %s", gRecordFile.c_str());
Expand Down
27 changes: 27 additions & 0 deletions orchagent/orch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ extern PortsOrch *gPortsOrch;

extern bool gSwssRecord;
extern ofstream gRecordOfs;
extern bool gLogRotate;
extern string gRecordFile;
extern string getTimestamp();

Expand Down Expand Up @@ -232,6 +233,25 @@ void Orch::doTask()
}
}

void Orch::logfileReopen()
{
gRecordOfs.close();

/*
* On log rotate we will use the same file name, we are assuming that
* logrotate deamon move filename to filename.1 and we will create new
* empty file here.
*/

gRecordOfs.open(gRecordFile);

if (!gRecordOfs.is_open())
{
SWSS_LOG_ERROR("failed to open gRecordOfs file %s: %s", gRecordFile.c_str(), strerror(errno));
return;
}
}

void Orch::recordTuple(Consumer &consumer, KeyOpFieldsValuesTuple &tuple)
{
string s = consumer.m_consumer->getTableName() + ":" + kfvKey(tuple)
Expand All @@ -242,6 +262,13 @@ void Orch::recordTuple(Consumer &consumer, KeyOpFieldsValuesTuple &tuple)
}

gRecordOfs << getTimestamp() << "|" << s << endl;

if (gLogRotate)
{
gLogRotate = false;

logfileReopen();
}
}

ref_resolve_status Orch::resolveFieldRefArray(
Expand Down
1 change: 1 addition & 0 deletions orchagent/orch.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class Orch

/* Run doTask against a specific consumer */
virtual void doTask(Consumer &consumer) = 0;
void logfileReopen();
void recordTuple(Consumer &consumer, KeyOpFieldsValuesTuple &tuple);
ref_resolve_status resolveFieldRefValue(type_map&, const string&, KeyOpFieldsValuesTuple&, sai_object_id_t&);
bool parseIndexRange(const string &input, sai_uint32_t &range_low, sai_uint32_t &range_high);
Expand Down

0 comments on commit 316b333

Please sign in to comment.