Skip to content

Commit

Permalink
Lock standard output when composing log message (#15798)
Browse files Browse the repository at this point in the history
Single log message is created by calling printf function multiple number
of times. In case of using log subsystem from more than one thread, the
log message might be corrupted. In order to prevent that, it is advised
to use flockfile()/funlockfile() for given stream.
  • Loading branch information
arkq authored and pull[bot] committed Oct 17, 2023
1 parent cf328f8 commit 2a82c1e
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/platform/Linux/Logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,18 @@ void ENFORCE_FORMAT(3, 0) LogV(const char * module, uint8_t category, const char
// indicate the error occurred during getting time.
gettimeofday(&tv, nullptr);

// Lock standard output, so a single log line will not be corrupted in case
// where multiple threads are using logging subsystem at the same time.
flockfile(stdout);

printf("[%" 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);
vprintf(msg, v);
printf("\n");
fflush(stdout);

funlockfile(stdout);

// Let the application know that a log message has been emitted.
DeviceLayer::OnLogOutput();
}
Expand Down

0 comments on commit 2a82c1e

Please sign in to comment.