Skip to content

Commit

Permalink
Add recording log output dir attribute to sairedis (sonic-net#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
kcudnik authored and Shuotian Cheng committed May 26, 2017
1 parent 31131d7 commit 9125f1c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/inc/sai_redis.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ extern "C" {

extern volatile bool g_record;
extern void setRecording(bool record);
extern sai_status_t setRecordingOutputDir(
_In_ const sai_attribute_t &attr);
extern void recordLine(std::string s);

extern std::string joinFieldValues(
Expand Down
14 changes: 14 additions & 0 deletions lib/inc/sairedis.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,20 @@ typedef enum _sai_redis_switch_attr_t
*/
SAI_REDIS_SWITCH_ATTR_FLUSH,

/**
* @brief Recording output directory.
*
* By default is current directory. Also setting empty will force default
* directory.
*
* It will have only impact on next created recording.
*
* @type sai_s8_list_t
* @flags CREATE_AND_SET
* @default empty
*/
SAI_REDIS_SWITCH_ATTR_RECORDING_OUTPUT_DIR,

} sai_redis_switch_attr_t;

/*
Expand Down
45 changes: 44 additions & 1 deletion lib/src/sai_redis_record.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "sai_redis.h"
#include <string.h>
#include <unistd.h>

std::string logOutputDir = ".";

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

recfile = "sairedis." + getTimestamp() + ".rec";
recfile = logOutputDir + "/sairedis." + getTimestamp() + ".rec";

recording.open(recfile);

Expand Down Expand Up @@ -104,3 +107,43 @@ std::string joinFieldValues(

return ss.str();
}

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

if (attr.value.s8list.count == 0)
{
logOutputDir = ".";
return SAI_STATUS_SUCCESS;
}

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

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 SAI_STATUS_FAILURE;
}

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

int result = access(dir.c_str(), W_OK);

if (result != 0)
{
SWSS_LOG_ERROR("can't access dir '%s' for writing", dir.c_str());
return SAI_STATUS_FAILURE;
}

logOutputDir = dir;

return SAI_STATUS_SUCCESS;
}
3 changes: 3 additions & 0 deletions lib/src/sai_redis_switch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,9 @@ sai_status_t redis_set_switch_attribute(
g_asicState->flush();
return SAI_STATUS_SUCCESS;

case SAI_REDIS_SWITCH_ATTR_RECORDING_OUTPUT_DIR:
return setRecordingOutputDir(*attr);

default:
break;
}
Expand Down

0 comments on commit 9125f1c

Please sign in to comment.