clang 24.0.0git
AtomicLineLogger.h
Go to the documentation of this file.
1//===- AtomicLineLogger.h ---------------------------------------*- C++ -*-===//
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/// \file
10/// Defines a logger where each line is written atomically to the file. It is
11/// safe to share a logger instance across threads.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_BASIC_ATOMICLINELOGGER_H
16#define LLVM_CLANG_BASIC_ATOMICLINELOGGER_H
17
18#include "clang/Basic/LLVM.h"
19#include "llvm/ADT/SmallString.h"
20#include "llvm/Support/raw_ostream.h"
21#include <atomic>
22#include <mutex>
23#include <optional>
24#include <string>
25
26namespace clang {
27
28class LogLine {
29 SmallString<128> Buffer;
30 std::optional<llvm::raw_svector_ostream> FormattingOS;
31 int FD = -1;
32 std::atomic<uint64_t> *DroppedLines = nullptr;
33
34 explicit LogLine(int FD, std::atomic<uint64_t> *DroppedLines);
35
36public:
39 LogLine(const LogLine &) = delete;
40 LogLine &operator=(const LogLine &) = delete;
41 LogLine &operator=(LogLine &&) = delete;
42
43 ~LogLine();
44
45 template <typename T> LogLine &operator<<(const T &Val) {
46 if (FormattingOS)
47 *FormattingOS << Val;
48 return *this;
49 }
50
51 template <typename RangeT>
52 void logArray(StringRef Prefix, StringRef Sep, const RangeT &Arr) {
53 if (FormattingOS) {
54 *FormattingOS << Prefix;
55 for (const auto &C : Arr)
56 *FormattingOS << Sep << C;
57 }
58 }
59
60 friend class AtomicLineLogger;
61};
62
64 std::atomic<int> FD{-1};
65 std::string LogPath;
66 std::atomic<uint64_t> DroppedLines{0};
67 std::mutex EnableMtx;
68 enum class LogPathSource {
69 None,
70 Constructor, // The path is from the constructor.
71 EnableMethod // The path is set through calling the enable() method.
72 };
73 LogPathSource PathSource = LogPathSource::None;
74
75 void initialize(StringRef LogFilePath);
76
77public:
79 AtomicLineLogger(StringRef LogFilePath);
80
81 // AtomicLineLogger is non-movable because LogLines have pointers to the
82 // atomic member DroppedLines.
85
87
88 /// Enables the logger if it is not already enabled. Thread safe.
89 ///
90 /// \returns false if the logger is not enabled consistently during its
91 /// lifetime.
92 bool enable(StringRef RequestedLogPath);
93
94 StringRef getLogPath() const { return LogPath; }
95
96 LogLine log();
97};
98
99} // namespace clang
100
101#endif
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
bool enable(StringRef RequestedLogPath)
Enables the logger if it is not already enabled.
AtomicLineLogger(AtomicLineLogger &&)=delete
AtomicLineLogger & operator=(AtomicLineLogger &&)=delete
StringRef getLogPath() const
LogLine(const LogLine &)=delete
LogLine & operator<<(const T &Val)
void logArray(StringRef Prefix, StringRef Sep, const RangeT &Arr)
friend class AtomicLineLogger
LogLine & operator=(const LogLine &)=delete
LogLine & operator=(LogLine &&)=delete
Top level wrappers for InstallAPI frontend operations.
const FunctionProtoType * T
@ Other
Other implicit parameter.
Definition Decl.h:1775