Skip to content

Commit 664b050

Browse files
committed
[Linux] Add mechnism to redirect CHIP log other than stdout
1 parent b791b75 commit 664b050

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

src/platform/Linux/Logging.cpp

+31-8
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,27 @@ void __attribute__((weak)) OnLogOutput() {}
3232
namespace Logging {
3333
namespace Platform {
3434

35+
// File pointer for the log file
36+
FILE * logFile = nullptr;
37+
38+
void OpenLogFile(const char * filePath)
39+
{
40+
logFile = fopen(filePath, "a");
41+
if (logFile == nullptr)
42+
{
43+
perror("Failed to open log file");
44+
}
45+
}
46+
47+
void CloseLogFile()
48+
{
49+
if (logFile != nullptr)
50+
{
51+
fclose(logFile);
52+
logFile = nullptr;
53+
}
54+
}
55+
3556
/**
3657
* CHIP log output functions.
3758
*/
@@ -44,17 +65,19 @@ void ENFORCE_FORMAT(3, 0) LogV(const char * module, uint8_t category, const char
4465
gettimeofday(&tv, nullptr);
4566

4667
#if !CHIP_USE_PW_LOGGING
47-
// Lock standard output, so a single log line will not be corrupted in case
68+
FILE * outputStream = (logFile == nullptr) ? stdout : logFile;
69+
// Lock outputStream, so a single log line will not be corrupted in case
4870
// where multiple threads are using logging subsystem at the same time.
49-
flockfile(stdout);
71+
flockfile(outputStream);
5072

51-
printf("[%" PRIu64 ".%06" PRIu64 "][%lld:%lld] CHIP:%s: ", static_cast<uint64_t>(tv.tv_sec), static_cast<uint64_t>(tv.tv_usec),
52-
static_cast<long long>(syscall(SYS_getpid)), static_cast<long long>(syscall(SYS_gettid)), module);
53-
vprintf(msg, v);
54-
printf("\n");
55-
fflush(stdout);
73+
fprintf(outputStream, "[%" PRIu64 ".%06" PRIu64 "][%lld:%lld] CHIP:%s: ", static_cast<uint64_t>(tv.tv_sec),
74+
static_cast<uint64_t>(tv.tv_usec), static_cast<long long>(syscall(SYS_getpid)),
75+
static_cast<long long>(syscall(SYS_gettid)), module);
76+
vfprintf(outputStream, msg, v);
77+
fprintf(outputStream, "\n");
5678

57-
funlockfile(stdout);
79+
fflush(outputStream);
80+
funlockfile(outputStream);
5881
#else // !CHIP_USE_PW_LOGGING
5982
char formattedMsg[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE];
6083
snprintf(formattedMsg, sizeof(formattedMsg),

src/platform/logging/LogV.h

+19
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,25 @@ namespace Platform {
3030
*/
3131
void ENFORCE_FORMAT(3, 0) LogV(const char * module, uint8_t category, const char * msg, va_list v);
3232

33+
/**
34+
* Open a log file for appending log messages. This function opens
35+
* the specified file and ensures that subsequent logs are written to
36+
* this file. It should be called at the start of the application
37+
* before any logging is done.
38+
*
39+
* @param[in] filePath The path to the log file. If the file does not
40+
* exist, it will be created. If it exists, log
41+
* messages will be appended to it.
42+
*/
43+
void OpenLogFile(const char * filePath);
44+
45+
/**
46+
* Close the log file if it is open. This function should be called
47+
* at the end of the application to ensure that all log messages are
48+
* flushed to disk and the file is properly closed.
49+
*/
50+
void CloseLogFile();
51+
3352
} // namespace Platform
3453
} // namespace Logging
3554
} // namespace chip

0 commit comments

Comments
 (0)