15e65e79bSJonas Devlieghere //===-- DebuggerEvents.cpp ------------------------------------------------===//
25e65e79bSJonas Devlieghere //
35e65e79bSJonas Devlieghere // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45e65e79bSJonas Devlieghere // See https://llvm.org/LICENSE.txt for license information.
55e65e79bSJonas Devlieghere // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65e65e79bSJonas Devlieghere //
75e65e79bSJonas Devlieghere //===----------------------------------------------------------------------===//
85e65e79bSJonas Devlieghere 
95e65e79bSJonas Devlieghere #include "lldb/Core/DebuggerEvents.h"
105e65e79bSJonas Devlieghere 
115e65e79bSJonas Devlieghere using namespace lldb_private;
125e65e79bSJonas Devlieghere 
13*2fc38b2bSJonas Devlieghere template <typename T>
14*2fc38b2bSJonas Devlieghere static const T *GetEventDataFromEventImpl(const Event *event_ptr) {
15*2fc38b2bSJonas Devlieghere   if (event_ptr)
16*2fc38b2bSJonas Devlieghere     if (const EventData *event_data = event_ptr->GetData())
17*2fc38b2bSJonas Devlieghere       if (event_data->GetFlavor() == T::GetFlavorString())
18*2fc38b2bSJonas Devlieghere         return static_cast<const T *>(event_ptr->GetData());
19*2fc38b2bSJonas Devlieghere   return nullptr;
20*2fc38b2bSJonas Devlieghere }
21*2fc38b2bSJonas Devlieghere 
225e65e79bSJonas Devlieghere ConstString ProgressEventData::GetFlavorString() {
235e65e79bSJonas Devlieghere   static ConstString g_flavor("ProgressEventData");
245e65e79bSJonas Devlieghere   return g_flavor;
255e65e79bSJonas Devlieghere }
265e65e79bSJonas Devlieghere 
275e65e79bSJonas Devlieghere ConstString ProgressEventData::GetFlavor() const {
285e65e79bSJonas Devlieghere   return ProgressEventData::GetFlavorString();
295e65e79bSJonas Devlieghere }
305e65e79bSJonas Devlieghere 
315e65e79bSJonas Devlieghere void ProgressEventData::Dump(Stream *s) const {
325e65e79bSJonas Devlieghere   s->Printf(" id = %" PRIu64 ", message = \"%s\"", m_id, m_message.c_str());
335e65e79bSJonas Devlieghere   if (m_completed == 0 || m_completed == m_total)
345e65e79bSJonas Devlieghere     s->Printf(", type = %s", m_completed == 0 ? "start" : "end");
355e65e79bSJonas Devlieghere   else
365e65e79bSJonas Devlieghere     s->PutCString(", type = update");
375e65e79bSJonas Devlieghere   // If m_total is UINT64_MAX, there is no progress to report, just "start"
385e65e79bSJonas Devlieghere   // and "end". If it isn't we will show the completed and total amounts.
395e65e79bSJonas Devlieghere   if (m_total != UINT64_MAX)
405e65e79bSJonas Devlieghere     s->Printf(", progress = %" PRIu64 " of %" PRIu64, m_completed, m_total);
415e65e79bSJonas Devlieghere }
425e65e79bSJonas Devlieghere 
435e65e79bSJonas Devlieghere const ProgressEventData *
445e65e79bSJonas Devlieghere ProgressEventData::GetEventDataFromEvent(const Event *event_ptr) {
45*2fc38b2bSJonas Devlieghere   return GetEventDataFromEventImpl<ProgressEventData>(event_ptr);
46*2fc38b2bSJonas Devlieghere }
47*2fc38b2bSJonas Devlieghere 
48*2fc38b2bSJonas Devlieghere llvm::StringRef DiagnosticEventData::GetPrefix() const {
49*2fc38b2bSJonas Devlieghere   switch (m_type) {
50*2fc38b2bSJonas Devlieghere   case Type::Warning:
51*2fc38b2bSJonas Devlieghere     return "warning";
52*2fc38b2bSJonas Devlieghere   case Type::Error:
53*2fc38b2bSJonas Devlieghere     return "error";
54*2fc38b2bSJonas Devlieghere   }
55*2fc38b2bSJonas Devlieghere }
56*2fc38b2bSJonas Devlieghere 
57*2fc38b2bSJonas Devlieghere void DiagnosticEventData::Dump(Stream *s) const {
58*2fc38b2bSJonas Devlieghere   *s << GetPrefix() << ": " << GetMessage() << '\n';
59*2fc38b2bSJonas Devlieghere   s->Flush();
60*2fc38b2bSJonas Devlieghere }
61*2fc38b2bSJonas Devlieghere 
62*2fc38b2bSJonas Devlieghere ConstString DiagnosticEventData::GetFlavorString() {
63*2fc38b2bSJonas Devlieghere   static ConstString g_flavor("DiagnosticEventData");
64*2fc38b2bSJonas Devlieghere   return g_flavor;
65*2fc38b2bSJonas Devlieghere }
66*2fc38b2bSJonas Devlieghere 
67*2fc38b2bSJonas Devlieghere ConstString DiagnosticEventData::GetFlavor() const {
68*2fc38b2bSJonas Devlieghere   return DiagnosticEventData::GetFlavorString();
69*2fc38b2bSJonas Devlieghere }
70*2fc38b2bSJonas Devlieghere 
71*2fc38b2bSJonas Devlieghere const DiagnosticEventData *
72*2fc38b2bSJonas Devlieghere DiagnosticEventData::GetEventDataFromEvent(const Event *event_ptr) {
73*2fc38b2bSJonas Devlieghere   return GetEventDataFromEventImpl<DiagnosticEventData>(event_ptr);
745e65e79bSJonas Devlieghere }
75