Skip to content

Commit

Permalink
Run interactive mode by default
Browse files Browse the repository at this point in the history
  • Loading branch information
yufengwangca committed May 14, 2024
1 parent 3fe770c commit 47b8ea3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
47 changes: 44 additions & 3 deletions examples/fabric-admin/commands/interactive/InteractiveCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,53 @@ constexpr char kInteractiveModeStopCommand[] = "quit()";

namespace {

// File pointer for the log file
FILE * logFile = nullptr;

void OpenLogFile(const char * filePath)
{
logFile = fopen(filePath, "a");
if (logFile == nullptr)
{
perror("Failed to open log file");
}
}

void CloseLogFile()
{
if (logFile != nullptr)
{
fclose(logFile);
logFile = nullptr;
}
}

void ClearLine()
{
printf("\r\x1B[0J"); // Move cursor to the beginning of the line and clear from cursor to end of the screen
}

void ENFORCE_FORMAT(3, 0) LoggingCallback(const char * module, uint8_t category, const char * msg, va_list args)
{
ClearLine();
chip::Logging::Platform::LogV(module, category, msg, args);
ClearLine();
struct timeval tv;

// 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
// indicate the error occurred during getting time.
gettimeofday(&tv, nullptr);

FILE * outputStream = (logFile == nullptr) ? stdout : logFile;
// Lock outputStream, so a single log line will not be corrupted in case
// where multiple threads are using logging subsystem at the same time.
flockfile(outputStream);

fprintf(outputStream, "[%" PRIu64 ".%06" PRIu64 "][%lld:%lld] CHIP:%s: ", static_cast<uint64_t>(tv.tv_sec),
static_cast<uint64_t>(tv.tv_usec), static_cast<long long>(syscall(SYS_getpid)),
static_cast<long long>(syscall(SYS_gettid)), module);
vfprintf(outputStream, msg, args);
fprintf(outputStream, "\n");
fflush(outputStream);

funlockfile(outputStream);
}

} // namespace
Expand Down Expand Up @@ -90,6 +127,8 @@ CHIP_ERROR InteractiveStartCommand::RunCommand()
{
read_history(GetHistoryFilePath().c_str());

OpenLogFile("/tmp/fabric_admin.log");

// Logs needs to be redirected in order to refresh the screen appropriately when something
// is dumped to stdout while the user is typing a command.
chip::Logging::SetLogRedirectCallback(LoggingCallback);
Expand All @@ -112,6 +151,8 @@ CHIP_ERROR InteractiveStartCommand::RunCommand()
}

SetCommandExitStatus(CHIP_NO_ERROR);
CloseLogFile();

return CHIP_NO_ERROR;
}

Expand Down
3 changes: 2 additions & 1 deletion examples/fabric-admin/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
// ================================================================================
int main(int argc, char * argv[])
{
const char * args[] = { argv[0], "interactive", "start" };
ExampleCredentialIssuerCommands credIssuerCommands;
Commands commands;

Expand All @@ -36,5 +37,5 @@ int main(int argc, char * argv[])
registerClusters(commands, &credIssuerCommands);
registerCommandsSubscriptions(commands, &credIssuerCommands);

return commands.Run(argc, argv);
return commands.Run(3, const_cast<char **>(args));
}

0 comments on commit 47b8ea3

Please sign in to comment.