1//===- AtomicLineLogger.cpp -----------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the implementation of an AtomicLineLogger and the relevant
10// supporting classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Basic/AtomicLineLogger.h"
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"
22#ifndef _WIN32
23#include <unistd.h>
24#endif
25#ifdef __APPLE__
26#include <sys/time.h>
27#endif
28
29using namespace clang;
30
31static uint64_t getTimestampMillis() {
32#ifdef __APPLE__
33 // Using chrono is roughly 50% slower.
34 struct timeval T;
35 gettimeofday(&T, 0);
36 return T.tv_sec * 1000 + T.tv_usec / 1000;
37#else
38 auto Time = std::chrono::system_clock::now();
39 auto Millis = std::chrono::duration_cast<std::chrono::milliseconds>(
40 d: Time.time_since_epoch());
41 return Millis.count();
42#endif
43}
44
45static int openLogFile(StringRef Path) {
46#ifdef _WIN32
47 // Logging is always disabled on Windows. openLogFile implements this policy
48 // by never returning a valid FD, so the logger and the LogLines it creates
49 // stay dormant (FD == -1). The reason is that writes to files opened with
50 // OF_Append are not guaranteed atomic on Windows. If a use case arises we'll
51 // need a different strategy to write LogLines atomically.
52 (void)Path;
53 return -1;
54#else
55 int FD = -1;
56 std::error_code EC = llvm::sys::fs::openFileForWrite(
57 Name: Path, ResultFD&: FD, Disp: llvm::sys::fs::CD_OpenAlways,
58 Flags: llvm::sys::fs::OF_Append | llvm::sys::fs::OF_Text);
59 if (EC) {
60 llvm::errs() << "warning: unable to open log file '" << Path
61 << "': " << EC.message() << "\n";
62 return -1;
63 }
64 return FD;
65#endif
66}
67
68// Writes the whole line into an FD that is opened with OF_Append.
69// This function only does one write (up to retry due to interrupts), and the
70// single write is blocking and atomic on POSIX systems.
71static bool writeLineToFD(int FD, const char *Data, size_t Size) {
72#ifdef _WIN32
73 (void)FD, (void)Data, (void)Size;
74 llvm_unreachable("dependency scanning logging is unsupported on Windows");
75#else
76 ssize_t Written = llvm::sys::RetryAfterSignal(Fail: -1, F&: write, As: FD, As: Data, As: Size);
77 return Written >= 0 && (static_cast<size_t>(Written) == Size);
78#endif
79}
80
81LogLine::LogLine(int FD, std::atomic<uint64_t> *DroppedLines)
82 : FormattingOS(Buffer), FD(FD), DroppedLines(DroppedLines) {
83 auto Millis = getTimestampMillis();
84 *FormattingOS << llvm::format(Fmt: "[%lld.%0.3lld]", Vals: Millis / 1000, Vals: Millis % 1000);
85 *FormattingOS << ' ' << llvm::sys::Process::getProcessId() << ' '
86 << llvm::get_threadid() << ": ";
87}
88
89LogLine::LogLine(LogLine &&Other)
90 : Buffer(std::move(Other.Buffer)), FD(Other.FD),
91 DroppedLines(Other.DroppedLines) {
92 if (Other.FormattingOS)
93 FormattingOS.emplace(args&: Buffer);
94
95 // Destroy the info in Other so its destructor does not write out the line.
96 Other.FormattingOS.reset();
97 Other.FD = -1;
98 Other.DroppedLines = nullptr;
99}
100
101LogLine::~LogLine() {
102 if (!FormattingOS)
103 return;
104 *FormattingOS << "\n";
105 if (!writeLineToFD(FD, Data: Buffer.data(), Size: Buffer.size()))
106 DroppedLines->fetch_add(i: 1, m: std::memory_order_relaxed);
107}
108
109void AtomicLineLogger::initialize(StringRef LogFilePath) {
110 LogPath = LogFilePath.str();
111 int NewFD = openLogFile(Path: LogFilePath);
112 if (NewFD == -1)
113 return;
114 FD.store(i: NewFD, m: std::memory_order_relaxed);
115 log() << "logging_start";
116}
117
118AtomicLineLogger::AtomicLineLogger(StringRef LogFilePath) {
119 if (LogFilePath.empty())
120 return;
121 initialize(LogFilePath);
122 PathSource = LogPathSource::Constructor;
123}
124
125bool AtomicLineLogger::enable(StringRef RequestedLogPath) {
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(LogFilePath: RequestedLogPath);
132 return true;
133 case LogPathSource::Constructor:
134 return RequestedLogPath.empty() || RequestedLogPath == LogPath;
135 case LogPathSource::EnableMethod:
136 return RequestedLogPath == LogPath;
137 }
138
139 llvm_unreachable("unhandled LogPathSource");
140}
141
142LogLine AtomicLineLogger::log() {
143 int CurFD = FD.load(m: std::memory_order_relaxed);
144 if (CurFD != -1)
145 return LogLine(CurFD, &DroppedLines);
146 return LogLine();
147}
148
149AtomicLineLogger::~AtomicLineLogger() {
150 int CurFD = FD.load(m: std::memory_order_relaxed);
151 if (CurFD == -1)
152 return;
153 log() << "logging_end";
154 if (uint64_t Dropped = DroppedLines.load(m: 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);
159}
160