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 
132fc38b2bSJonas Devlieghere template <typename T>
142fc38b2bSJonas Devlieghere static const T *GetEventDataFromEventImpl(const Event *event_ptr) {
152fc38b2bSJonas Devlieghere   if (event_ptr)
162fc38b2bSJonas Devlieghere     if (const EventData *event_data = event_ptr->GetData())
172fc38b2bSJonas Devlieghere       if (event_data->GetFlavor() == T::GetFlavorString())
182fc38b2bSJonas Devlieghere         return static_cast<const T *>(event_ptr->GetData());
192fc38b2bSJonas Devlieghere   return nullptr;
202fc38b2bSJonas Devlieghere }
212fc38b2bSJonas 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) {
452fc38b2bSJonas Devlieghere   return GetEventDataFromEventImpl<ProgressEventData>(event_ptr);
462fc38b2bSJonas Devlieghere }
472fc38b2bSJonas Devlieghere 
482fc38b2bSJonas Devlieghere llvm::StringRef DiagnosticEventData::GetPrefix() const {
492fc38b2bSJonas Devlieghere   switch (m_type) {
502fc38b2bSJonas Devlieghere   case Type::Warning:
512fc38b2bSJonas Devlieghere     return "warning";
522fc38b2bSJonas Devlieghere   case Type::Error:
532fc38b2bSJonas Devlieghere     return "error";
542fc38b2bSJonas Devlieghere   }
55*ae2aa2d2SMartin Storsjö   llvm_unreachable("Fully covered switch above!");
562fc38b2bSJonas Devlieghere }
572fc38b2bSJonas Devlieghere 
582fc38b2bSJonas Devlieghere void DiagnosticEventData::Dump(Stream *s) const {
592fc38b2bSJonas Devlieghere   *s << GetPrefix() << ": " << GetMessage() << '\n';
602fc38b2bSJonas Devlieghere   s->Flush();
612fc38b2bSJonas Devlieghere }
622fc38b2bSJonas Devlieghere 
632fc38b2bSJonas Devlieghere ConstString DiagnosticEventData::GetFlavorString() {
642fc38b2bSJonas Devlieghere   static ConstString g_flavor("DiagnosticEventData");
652fc38b2bSJonas Devlieghere   return g_flavor;
662fc38b2bSJonas Devlieghere }
672fc38b2bSJonas Devlieghere 
682fc38b2bSJonas Devlieghere ConstString DiagnosticEventData::GetFlavor() const {
692fc38b2bSJonas Devlieghere   return DiagnosticEventData::GetFlavorString();
702fc38b2bSJonas Devlieghere }
712fc38b2bSJonas Devlieghere 
722fc38b2bSJonas Devlieghere const DiagnosticEventData *
732fc38b2bSJonas Devlieghere DiagnosticEventData::GetEventDataFromEvent(const Event *event_ptr) {
742fc38b2bSJonas Devlieghere   return GetEventDataFromEventImpl<DiagnosticEventData>(event_ptr);
755e65e79bSJonas Devlieghere }
76