@@ -32,6 +32,27 @@ void __attribute__((weak)) OnLogOutput() {}
32
32
namespace Logging {
33
33
namespace Platform {
34
34
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
+
35
56
/* *
36
57
* CHIP log output functions.
37
58
*/
@@ -44,17 +65,19 @@ void ENFORCE_FORMAT(3, 0) LogV(const char * module, uint8_t category, const char
44
65
gettimeofday (&tv, nullptr );
45
66
46
67
#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
48
70
// where multiple threads are using logging subsystem at the same time.
49
- flockfile (stdout );
71
+ flockfile (outputStream );
50
72
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 " );
56
78
57
- funlockfile (stdout);
79
+ fflush (outputStream);
80
+ funlockfile (outputStream);
58
81
#else // !CHIP_USE_PW_LOGGING
59
82
char formattedMsg[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE];
60
83
snprintf (formattedMsg, sizeof (formattedMsg),
0 commit comments