1 //===-- Diagnostics.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 #ifndef LLDB_UTILITY_DIAGNOSTICS_H
10 #define LLDB_UTILITY_DIAGNOSTICS_H
11 
12 #include "lldb/Utility/FileSpec.h"
13 #include "lldb/Utility/Log.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ADT/StringSet.h"
16 #include "llvm/Support/Error.h"
17 
18 #include <functional>
19 #include <mutex>
20 #include <optional>
21 #include <vector>
22 
23 namespace lldb_private {
24 
25 /// Diagnostics are a collection of files to help investigate bugs and
26 /// troubleshoot issues. Any part of the debugger can register itself with the
27 /// help of a callback to emit one or more files into the diagnostic directory.
28 class Diagnostics {
29 public:
30   Diagnostics();
31   ~Diagnostics();
32 
33   /// Gather diagnostics in the given directory.
34   llvm::Error Create(const FileSpec &dir);
35 
36   /// Gather diagnostics and print a message to the given output stream.
37   /// @{
38   bool Dump(llvm::raw_ostream &stream);
39   bool Dump(llvm::raw_ostream &stream, const FileSpec &dir);
40   /// @}
41 
42   void Report(llvm::StringRef message);
43 
44   using Callback = std::function<llvm::Error(const FileSpec &)>;
45 
46   void AddCallback(Callback callback);
47 
48   static Diagnostics &Instance();
49 
50   static bool Enabled();
51   static void Initialize();
52   static void Terminate();
53 
54   /// Create a unique diagnostic directory.
55   static llvm::Expected<FileSpec> CreateUniqueDirectory();
56 
57 private:
58   static std::optional<Diagnostics> &InstanceImpl();
59 
60   llvm::Error DumpDiangosticsLog(const FileSpec &dir) const;
61 
62   RotatingLogHandler m_log_handler;
63 
64   llvm::SmallVector<Callback, 4> m_callbacks;
65   std::mutex m_callbacks_mutex;
66 };
67 
68 } // namespace lldb_private
69 
70 #endif
71