15#include "llvm/ADT/StringRef.h"
16#include "llvm/Support/Errno.h"
17#include "llvm/Support/ErrorHandling.h"
18#include "llvm/Support/FileSystem.h"
19#include "llvm/Support/Format.h"
20#include "llvm/Support/Process.h"
21#include "llvm/Support/Threading.h"
36 return T.tv_sec * 1000 + T.tv_usec / 1000;
38 auto Time = std::chrono::system_clock::now();
39 auto Millis = std::chrono::duration_cast<std::chrono::milliseconds>(
40 Time.time_since_epoch());
41 return Millis.count();
56 std::error_code EC = llvm::sys::fs::openFileForWrite(
57 Path, FD, llvm::sys::fs::CD_OpenAlways,
58 llvm::sys::fs::OF_Append | llvm::sys::fs::OF_Text);
60 llvm::errs() <<
"warning: unable to open log file '" << Path
61 <<
"': " << EC.message() <<
"\n";
73 (void)FD, (
void)
Data, (void)Size;
74 llvm_unreachable(
"dependency scanning logging is unsupported on Windows");
76 ssize_t Written = llvm::sys::RetryAfterSignal(-1, write, FD,
Data, Size);
77 return Written >= 0 && (
static_cast<size_t>(Written) == Size);
82 : FormattingOS(Buffer), FD(FD), DroppedLines(DroppedLines) {
84 *FormattingOS << llvm::format(
"[%lld.%0.3lld]", Millis / 1000, Millis % 1000);
85 *FormattingOS <<
' ' << llvm::sys::Process::getProcessId() <<
' '
86 << llvm::get_threadid() <<
": ";
91 DroppedLines(
Other.DroppedLines) {
92 if (
Other.FormattingOS)
93 FormattingOS.emplace(Buffer);
96 Other.FormattingOS.reset();
98 Other.DroppedLines =
nullptr;
104 *FormattingOS <<
"\n";
106 DroppedLines->fetch_add(1, std::memory_order_relaxed);
109void AtomicLineLogger::initialize(StringRef LogFilePath) {
110 LogPath = LogFilePath.str();
114 FD.store(NewFD, std::memory_order_relaxed);
115 log() <<
"logging_start";
119 if (LogFilePath.empty())
121 initialize(LogFilePath);
122 PathSource = LogPathSource::Constructor;
126 std::lock_guard<std::mutex> Lock(EnableMtx);
127 switch (PathSource) {
128 case LogPathSource::None:
129 PathSource = LogPathSource::EnableMethod;
130 if (!RequestedLogPath.empty())
131 initialize(RequestedLogPath);
133 case LogPathSource::Constructor:
134 return RequestedLogPath.empty() || RequestedLogPath == LogPath;
135 case LogPathSource::EnableMethod:
136 return RequestedLogPath == LogPath;
139 llvm_unreachable(
"unhandled LogPathSource");
143 int CurFD = FD.load(std::memory_order_relaxed);
145 return LogLine(CurFD, &DroppedLines);
150 int CurFD = FD.load(std::memory_order_relaxed);
153 log() <<
"logging_end";
154 if (uint64_t Dropped = DroppedLines.load(std::memory_order_relaxed))
155 llvm::errs() <<
"warning: log '" << LogPath
156 <<
"' is incomplete: " << Dropped
157 <<
" line(s) dropped due to write errors\n";
158 llvm::sys::Process::SafelyCloseFileDescriptor(FD);
static int openLogFile(StringRef Path)
static bool writeLineToFD(int FD, const char *Data, size_t Size)
static uint64_t getTimestampMillis()
Defines a logger where each line is written atomically to the file.
bool enable(StringRef RequestedLogPath)
Enables the logger if it is not already enabled.
Top level wrappers for InstallAPI frontend operations.
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat uint32_t uint64_t __builtin_riscv_ssha_i32 __builtin_riscv_sshl_u32 uint64_t