1 //===-- DebuggerEvents.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 #include "lldb/Core/DebuggerEvents.h"
10 #include "llvm/Support/WithColor.h"
11 
12 using namespace lldb_private;
13 
14 template <typename T>
15 static const T *GetEventDataFromEventImpl(const Event *event_ptr) {
16   if (event_ptr)
17     if (const EventData *event_data = event_ptr->GetData())
18       if (event_data->GetFlavor() == T::GetFlavorString())
19         return static_cast<const T *>(event_ptr->GetData());
20   return nullptr;
21 }
22 
23 ConstString ProgressEventData::GetFlavorString() {
24   static ConstString g_flavor("ProgressEventData");
25   return g_flavor;
26 }
27 
28 ConstString ProgressEventData::GetFlavor() const {
29   return ProgressEventData::GetFlavorString();
30 }
31 
32 void ProgressEventData::Dump(Stream *s) const {
33   s->Printf(" id = %" PRIu64 ", message = \"%s\"", m_id, m_message.c_str());
34   if (m_completed == 0 || m_completed == m_total)
35     s->Printf(", type = %s", m_completed == 0 ? "start" : "end");
36   else
37     s->PutCString(", type = update");
38   // If m_total is UINT64_MAX, there is no progress to report, just "start"
39   // and "end". If it isn't we will show the completed and total amounts.
40   if (m_total != UINT64_MAX)
41     s->Printf(", progress = %" PRIu64 " of %" PRIu64, m_completed, m_total);
42 }
43 
44 const ProgressEventData *
45 ProgressEventData::GetEventDataFromEvent(const Event *event_ptr) {
46   return GetEventDataFromEventImpl<ProgressEventData>(event_ptr);
47 }
48 
49 llvm::StringRef DiagnosticEventData::GetPrefix() const {
50   switch (m_type) {
51   case Type::Warning:
52     return "warning";
53   case Type::Error:
54     return "error";
55   }
56   llvm_unreachable("Fully covered switch above!");
57 }
58 
59 void DiagnosticEventData::Dump(Stream *s) const {
60   llvm::HighlightColor color = m_type == Type::Warning
61                                    ? llvm::HighlightColor::Warning
62                                    : llvm::HighlightColor::Error;
63   llvm::WithColor(s->AsRawOstream(), color, llvm::ColorMode::Enable)
64       << GetPrefix();
65   *s << ": " << GetMessage() << '\n';
66   s->Flush();
67 }
68 
69 ConstString DiagnosticEventData::GetFlavorString() {
70   static ConstString g_flavor("DiagnosticEventData");
71   return g_flavor;
72 }
73 
74 ConstString DiagnosticEventData::GetFlavor() const {
75   return DiagnosticEventData::GetFlavorString();
76 }
77 
78 const DiagnosticEventData *
79 DiagnosticEventData::GetEventDataFromEvent(const Event *event_ptr) {
80   return GetEventDataFromEventImpl<DiagnosticEventData>(event_ptr);
81 }
82