Skip to content

Commit 9125f1c

Browse files
kcudnikShuotian Cheng
authored and
Shuotian Cheng
committed
Add recording log output dir attribute to sairedis (sonic-net#186)
1 parent 31131d7 commit 9125f1c

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

lib/inc/sai_redis.h

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ extern "C" {
2929

3030
extern volatile bool g_record;
3131
extern void setRecording(bool record);
32+
extern sai_status_t setRecordingOutputDir(
33+
_In_ const sai_attribute_t &attr);
3234
extern void recordLine(std::string s);
3335

3436
extern std::string joinFieldValues(

lib/inc/sairedis.h

+14
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,20 @@ typedef enum _sai_redis_switch_attr_t
6868
*/
6969
SAI_REDIS_SWITCH_ATTR_FLUSH,
7070

71+
/**
72+
* @brief Recording output directory.
73+
*
74+
* By default is current directory. Also setting empty will force default
75+
* directory.
76+
*
77+
* It will have only impact on next created recording.
78+
*
79+
* @type sai_s8_list_t
80+
* @flags CREATE_AND_SET
81+
* @default empty
82+
*/
83+
SAI_REDIS_SWITCH_ATTR_RECORDING_OUTPUT_DIR,
84+
7185
} sai_redis_switch_attr_t;
7286

7387
/*

lib/src/sai_redis_record.cpp

+44-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#include "sai_redis.h"
22
#include <string.h>
3+
#include <unistd.h>
4+
5+
std::string logOutputDir = ".";
36

47
std::string getTimestamp()
58
{
@@ -41,7 +44,7 @@ void startRecording()
4144
{
4245
SWSS_LOG_ENTER();
4346

44-
recfile = "sairedis." + getTimestamp() + ".rec";
47+
recfile = logOutputDir + "/sairedis." + getTimestamp() + ".rec";
4548

4649
recording.open(recfile);
4750

@@ -104,3 +107,43 @@ std::string joinFieldValues(
104107

105108
return ss.str();
106109
}
110+
111+
sai_status_t setRecordingOutputDir(
112+
_In_ const sai_attribute_t &attr)
113+
{
114+
SWSS_LOG_ENTER();
115+
116+
if (attr.value.s8list.count == 0)
117+
{
118+
logOutputDir = ".";
119+
return SAI_STATUS_SUCCESS;
120+
}
121+
122+
if (attr.value.s8list.list == NULL)
123+
{
124+
SWSS_LOG_ERROR("list pointer is NULL");
125+
return SAI_STATUS_FAILURE;
126+
}
127+
128+
size_t len = strnlen((const char *)attr.value.s8list.list, attr.value.s8list.count);
129+
130+
if (len != (size_t)attr.value.s8list.count)
131+
{
132+
SWSS_LOG_ERROR("count (%u) is different than strnlen (%zu)", attr.value.s8list.count, len);
133+
return SAI_STATUS_FAILURE;
134+
}
135+
136+
std::string dir((const char*)attr.value.s8list.list, len);
137+
138+
int result = access(dir.c_str(), W_OK);
139+
140+
if (result != 0)
141+
{
142+
SWSS_LOG_ERROR("can't access dir '%s' for writing", dir.c_str());
143+
return SAI_STATUS_FAILURE;
144+
}
145+
146+
logOutputDir = dir;
147+
148+
return SAI_STATUS_SUCCESS;
149+
}

lib/src/sai_redis_switch.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,9 @@ sai_status_t redis_set_switch_attribute(
388388
g_asicState->flush();
389389
return SAI_STATUS_SUCCESS;
390390

391+
case SAI_REDIS_SWITCH_ATTR_RECORDING_OUTPUT_DIR:
392+
return setRecordingOutputDir(*attr);
393+
391394
default:
392395
break;
393396
}

0 commit comments

Comments
 (0)