Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[201911][logfile] sairedis record file handling #758

Merged
merged 1 commit into from
Jan 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/inc/sai_redis.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ extern void clear_local_state();
extern void setRecording(bool record);
extern sai_status_t setRecordingOutputDir(
_In_ const sai_attribute_t &attr);
extern sai_status_t setRecordingOutputFile(
_In_ const sai_attribute_t &attr);
extern void recordLine(std::string s);
extern std::string joinFieldValues(
_In_ const std::vector<swss::FieldValueTuple> &values);
Expand Down
12 changes: 12 additions & 0 deletions lib/inc/sairedis.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,18 @@ typedef enum _sai_redis_switch_attr_t
*/
SAI_REDIS_SWITCH_ATTR_SYNC_MODE,

/**
* @brief Recording log filename.
*
* Default valus is sairedis.rec
*
* @type sai_s8_list_t
* @flags CREATE_AND_SET
* @default sairedis.rec
*/
SAI_REDIS_SWITCH_ATTR_RECORDING_FILENAME,


} sai_redis_switch_attr_t;

/*
Expand Down
46 changes: 45 additions & 1 deletion lib/src/sai_redis_record.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <unistd.h>

std::string logOutputDir = ".";
std::string logOutputFile = "sairedis.rec";

std::string getTimestamp()
{
Expand Down Expand Up @@ -80,7 +81,7 @@ void startRecording()
{
SWSS_LOG_ENTER();

recfile = logOutputDir + "/sairedis.rec";
recfile = logOutputDir + "/" + logOutputFile ;

recording.open(recfile, std::ofstream::out | std::ofstream::app);

Expand Down Expand Up @@ -187,3 +188,46 @@ sai_status_t setRecordingOutputDir(

return SAI_STATUS_SUCCESS;
}

sai_status_t setRecordingOutputFile(
_In_ const sai_attribute_t &attr)
{
SWSS_LOG_ENTER();

if (attr.value.s8list.count == 0)
{
SWSS_LOG_NOTICE("setting recording filename to default filename: %s", logOutputFile.c_str());

return true;
}

if (attr.value.s8list.list == NULL)
{
SWSS_LOG_ERROR("list pointer is NULL");

return false;
}

size_t len = strnlen((const char *)attr.value.s8list.list, attr.value.s8list.count);

if (len != (size_t)attr.value.s8list.count)
{
SWSS_LOG_ERROR("count (%u) is different than strnlen (%zu)", attr.value.s8list.count, len);

return false;
}

std::string filename((const char*)attr.value.s8list.list, len);

/// Stop the recording with old file before updating the filename
stopRecording();

logOutputFile = filename;

SWSS_LOG_NOTICE("setting recording filename : %s", logOutputFile.c_str());

/// Start recording with new file
startRecording();
return SAI_STATUS_SUCCESS;

}
2 changes: 2 additions & 0 deletions lib/src/sai_redis_switch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,8 @@ sai_status_t redis_set_switch_attribute(
case SAI_REDIS_SWITCH_ATTR_RECORDING_OUTPUT_DIR:
return setRecordingOutputDir(*attr);

case SAI_REDIS_SWITCH_ATTR_RECORDING_FILENAME:
return setRecordingOutputFile(*attr);
default:
break;
}
Expand Down