19
19
#include " InteractiveCommands.h"
20
20
21
21
#include < platform/logging/LogV.h>
22
+ #include < system/SystemClock.h>
22
23
23
24
#include < editline.h>
24
25
26
+ #include < stdarg.h>
27
+ #include < stdio.h>
25
28
#include < string>
26
29
#include < vector>
27
30
@@ -32,23 +35,23 @@ constexpr char kInteractiveModeStopCommand[] = "quit()";
32
35
namespace {
33
36
34
37
// File pointer for the log file
35
- FILE * logFile = nullptr ;
38
+ FILE * sLogFile = nullptr ;
36
39
37
40
void OpenLogFile (const char * filePath)
38
41
{
39
- logFile = fopen (filePath, " a" );
40
- if (logFile == nullptr )
42
+ sLogFile = fopen (filePath, " a" );
43
+ if (sLogFile == nullptr )
41
44
{
42
45
perror (" Failed to open log file" );
43
46
}
44
47
}
45
48
46
49
void CloseLogFile ()
47
50
{
48
- if (logFile != nullptr )
51
+ if (sLogFile != nullptr )
49
52
{
50
- fclose (logFile );
51
- logFile = nullptr ;
53
+ fclose (sLogFile );
54
+ sLogFile = nullptr ;
52
55
}
53
56
}
54
57
@@ -59,25 +62,23 @@ void ClearLine()
59
62
60
63
void ENFORCE_FORMAT (3 , 0 ) LoggingCallback(const char * module, uint8_t category, const char * msg, va_list args)
61
64
{
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 ;
63
68
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 );
67
70
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 ());
72
74
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 );
79
80
80
- funlockfile (outputStream );
81
+ funlockfile (sLogFile );
81
82
}
82
83
83
84
} // namespace
@@ -127,11 +128,13 @@ CHIP_ERROR InteractiveStartCommand::RunCommand()
127
128
{
128
129
read_history (GetHistoryFilePath ().c_str ());
129
130
130
- OpenLogFile (" /tmp/fabric_admin.log" );
131
+ if (mLogFilePath .HasValue ())
132
+ {
133
+ OpenLogFile (mLogFilePath .Value ());
131
134
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
+ }
135
138
136
139
char * command = nullptr ;
137
140
int status;
0 commit comments