Skip to content

Commit 03bd011

Browse files
authored
[Fabric-Sync] Run interactive mode in Fabric-Admin by default (#33455)
* Run interactive mode by default * Address review comments * allocate make a std::vector<const char*> and pass .data() of that vector to commands.Run()
1 parent 28b896a commit 03bd011

File tree

3 files changed

+77
-7
lines changed

3 files changed

+77
-7
lines changed

examples/fabric-admin/commands/common/CHIPCommand.h

+4
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ class CHIPCommand : public Command
6767
CHIPCommand(const char * commandName, CredentialIssuerCommands * credIssuerCmds, const char * helpText = nullptr) :
6868
Command(commandName, helpText), mCredIssuerCmds(credIssuerCmds)
6969
{
70+
AddArgument("log-file-path", &mLogFilePath,
71+
"Path to the log file where the output is redirected. Can be absolute or relative to the current working "
72+
"directory.");
7073
AddArgument("paa-trust-store-path", &mPaaTrustStorePath,
7174
"Path to directory holding PAA certificate information. Can be absolute or relative to the current working "
7275
"directory.");
@@ -156,6 +159,7 @@ class CHIPCommand : public Command
156159
// identity without shutting it down or something in between...
157160
PersistentStorage mCommissionerStorage;
158161
#endif // CONFIG_USE_LOCAL_STORAGE
162+
chip::Optional<char *> mLogFilePath;
159163
chip::PersistentStorageOperationalKeystore mOperationalKeystore;
160164
chip::Credentials::PersistentStorageOpCertStore mOpCertStore;
161165
static chip::Crypto::RawKeySessionKeystore sSessionKeystore;

examples/fabric-admin/commands/interactive/InteractiveCommands.cpp

+51-6
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@
1919
#include "InteractiveCommands.h"
2020

2121
#include <platform/logging/LogV.h>
22+
#include <system/SystemClock.h>
2223

2324
#include <editline.h>
2425

26+
#include <stdarg.h>
27+
#include <stdio.h>
2528
#include <string>
2629
#include <vector>
2730

@@ -31,16 +34,52 @@ constexpr char kInteractiveModeStopCommand[] = "quit()";
3134

3235
namespace {
3336

37+
// File pointer for the log file
38+
FILE * sLogFile = nullptr;
39+
40+
void OpenLogFile(const char * filePath)
41+
{
42+
sLogFile = fopen(filePath, "a");
43+
if (sLogFile == nullptr)
44+
{
45+
perror("Failed to open log file");
46+
}
47+
}
48+
49+
void CloseLogFile()
50+
{
51+
if (sLogFile != nullptr)
52+
{
53+
fclose(sLogFile);
54+
sLogFile = nullptr;
55+
}
56+
}
57+
3458
void ClearLine()
3559
{
3660
printf("\r\x1B[0J"); // Move cursor to the beginning of the line and clear from cursor to end of the screen
3761
}
3862

3963
void ENFORCE_FORMAT(3, 0) LoggingCallback(const char * module, uint8_t category, const char * msg, va_list args)
4064
{
41-
ClearLine();
42-
chip::Logging::Platform::LogV(module, category, msg, args);
43-
ClearLine();
65+
if (sLogFile == nullptr)
66+
{
67+
return;
68+
}
69+
70+
uint64_t timeMs = chip::System::SystemClock().GetMonotonicMilliseconds64().count();
71+
uint64_t seconds = timeMs / 1000;
72+
uint64_t milliseconds = timeMs % 1000;
73+
74+
flockfile(sLogFile);
75+
76+
fprintf(sLogFile, "[%llu.%06llu] CHIP:%s: ", static_cast<unsigned long long>(seconds),
77+
static_cast<unsigned long long>(milliseconds), module);
78+
vfprintf(sLogFile, msg, args);
79+
fprintf(sLogFile, "\n");
80+
fflush(sLogFile);
81+
82+
funlockfile(sLogFile);
4483
}
4584

4685
} // namespace
@@ -90,9 +129,13 @@ CHIP_ERROR InteractiveStartCommand::RunCommand()
90129
{
91130
read_history(GetHistoryFilePath().c_str());
92131

93-
// Logs needs to be redirected in order to refresh the screen appropriately when something
94-
// is dumped to stdout while the user is typing a command.
95-
chip::Logging::SetLogRedirectCallback(LoggingCallback);
132+
if (mLogFilePath.HasValue())
133+
{
134+
OpenLogFile(mLogFilePath.Value());
135+
136+
// Redirect logs to the custom logging callback
137+
chip::Logging::SetLogRedirectCallback(LoggingCallback);
138+
}
96139

97140
char * command = nullptr;
98141
int status;
@@ -112,6 +155,8 @@ CHIP_ERROR InteractiveStartCommand::RunCommand()
112155
}
113156

114157
SetCommandExitStatus(CHIP_NO_ERROR);
158+
CloseLogFile();
159+
115160
return CHIP_NO_ERROR;
116161
}
117162

examples/fabric-admin/main.cpp

+22-1
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,26 @@
2323
#include "commands/pairing/Commands.h"
2424
#include <zap-generated/cluster/Commands.h>
2525

26+
#include <iostream>
27+
#include <string>
28+
#include <vector>
29+
2630
// ================================================================================
2731
// Main Code
2832
// ================================================================================
2933
int main(int argc, char * argv[])
3034
{
35+
// Convert command line arguments to a vector of strings for easier manipulation
36+
std::vector<std::string> args(argv, argv + argc);
37+
38+
// Check if "interactive" and "start" are not in the arguments
39+
if (args.size() < 3 || args[1] != "interactive" || args[2] != "start")
40+
{
41+
// Insert "interactive" and "start" after the executable name
42+
args.insert(args.begin() + 1, "interactive");
43+
args.insert(args.begin() + 2, "start");
44+
}
45+
3146
ExampleCredentialIssuerCommands credIssuerCommands;
3247
Commands commands;
3348

@@ -36,5 +51,11 @@ int main(int argc, char * argv[])
3651
registerClusters(commands, &credIssuerCommands);
3752
registerCommandsSubscriptions(commands, &credIssuerCommands);
3853

39-
return commands.Run(argc, argv);
54+
std::vector<char *> c_args;
55+
for (auto & arg : args)
56+
{
57+
c_args.push_back(const_cast<char *>(arg.c_str()));
58+
}
59+
60+
return commands.Run(static_cast<int>(c_args.size()), c_args.data());
4061
}

0 commit comments

Comments
 (0)