Skip to content

Commit 0c54647

Browse files
committed
Address review comments
1 parent 7c415e6 commit 0c54647

File tree

3 files changed

+56
-27
lines changed

3 files changed

+56
-27
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

+28-25
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

@@ -32,23 +35,23 @@ constexpr char kInteractiveModeStopCommand[] = "quit()";
3235
namespace {
3336

3437
// File pointer for the log file
35-
FILE * logFile = nullptr;
38+
FILE * sLogFile = nullptr;
3639

3740
void OpenLogFile(const char * filePath)
3841
{
39-
logFile = fopen(filePath, "a");
40-
if (logFile == nullptr)
42+
sLogFile = fopen(filePath, "a");
43+
if (sLogFile == nullptr)
4144
{
4245
perror("Failed to open log file");
4346
}
4447
}
4548

4649
void CloseLogFile()
4750
{
48-
if (logFile != nullptr)
51+
if (sLogFile != nullptr)
4952
{
50-
fclose(logFile);
51-
logFile = nullptr;
53+
fclose(sLogFile);
54+
sLogFile = nullptr;
5255
}
5356
}
5457

@@ -59,25 +62,23 @@ void ClearLine()
5962

6063
void ENFORCE_FORMAT(3, 0) LoggingCallback(const char * module, uint8_t category, const char * msg, va_list args)
6164
{
62-
struct timeval tv;
65+
uint64_t timeMs = chip::System::SystemClock().GetMonotonicMilliseconds64().count();
66+
uint64_t seconds = timeMs / 1000;
67+
uint64_t milliseconds = timeMs % 1000;
6368

64-
// Should not fail per man page of gettimeofday(), but failed to get time is not a fatal error in log. The bad time value will
65-
// indicate the error occurred during getting time.
66-
gettimeofday(&tv, nullptr);
69+
flockfile(sLogFile);
6770

68-
FILE * outputStream = (logFile == nullptr) ? stdout : logFile;
69-
// Lock outputStream, so a single log line will not be corrupted in case
70-
// where multiple threads are using logging subsystem at the same time.
71-
flockfile(outputStream);
71+
// Portable thread and process identifiers
72+
auto pid = static_cast<unsigned long>(getpid());
73+
auto tid = static_cast<unsigned long>(pthread_self());
7274

73-
fprintf(outputStream, "[%llu.%06llu][%lld:%lld] CHIP:%s: ", static_cast<unsigned long long>(tv.tv_sec),
74-
static_cast<unsigned long long>(tv.tv_usec), static_cast<long long>(syscall(SYS_getpid)),
75-
static_cast<long long>(syscall(SYS_gettid)), module);
76-
vfprintf(outputStream, msg, args);
77-
fprintf(outputStream, "\n");
78-
fflush(outputStream);
75+
fprintf(sLogFile, "[%llu.%06llu][%lu:%lu] CHIP:%s: ", static_cast<unsigned long long>(seconds),
76+
static_cast<unsigned long long>(milliseconds), pid, tid, module);
77+
vfprintf(sLogFile, msg, args);
78+
fprintf(sLogFile, "\n");
79+
fflush(sLogFile);
7980

80-
funlockfile(outputStream);
81+
funlockfile(sLogFile);
8182
}
8283

8384
} // namespace
@@ -127,11 +128,13 @@ CHIP_ERROR InteractiveStartCommand::RunCommand()
127128
{
128129
read_history(GetHistoryFilePath().c_str());
129130

130-
OpenLogFile("/tmp/fabric_admin.log");
131+
if (mLogFilePath.HasValue())
132+
{
133+
OpenLogFile(mLogFilePath.Value());
131134

132-
// Logs needs to be redirected in order to refresh the screen appropriately when something
133-
// is dumped to stdout while the user is typing a command.
134-
chip::Logging::SetLogRedirectCallback(LoggingCallback);
135+
// Redirect logs to the custom logging callback
136+
chip::Logging::SetLogRedirectCallback(LoggingCallback);
137+
}
135138

136139
char * command = nullptr;
137140
int status;

examples/fabric-admin/main.cpp

+24-2
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,34 @@
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
{
31-
const char * args[] = { argv[0], "interactive", "start" };
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+
// Update argc and argv to reflect the new arguments
46+
argc = static_cast<int>(args.size());
47+
for (size_t i = 0; i < args.size(); ++i)
48+
{
49+
argv[i] = const_cast<char *>(args[i].c_str());
50+
}
51+
}
52+
53+
// const char * args[] = { argv[0], "interactive", "start" };
3254
ExampleCredentialIssuerCommands credIssuerCommands;
3355
Commands commands;
3456

@@ -37,5 +59,5 @@ int main(int argc, char * argv[])
3759
registerClusters(commands, &credIssuerCommands);
3860
registerCommandsSubscriptions(commands, &credIssuerCommands);
3961

40-
return commands.Run(3, const_cast<char **>(args));
62+
return commands.Run(argc, argv);
4163
}

0 commit comments

Comments
 (0)