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"
10*990d0c71SJonas Devlieghere #include "llvm/Support/WithColor.h"
115e65e79bSJonas Devlieghere 
125e65e79bSJonas Devlieghere using namespace lldb_private;
135e65e79bSJonas Devlieghere 
142fc38b2bSJonas Devlieghere template <typename T>
GetEventDataFromEventImpl(const Event * event_ptr)152fc38b2bSJonas Devlieghere static const T *GetEventDataFromEventImpl(const Event *event_ptr) {
162fc38b2bSJonas Devlieghere   if (event_ptr)
172fc38b2bSJonas Devlieghere     if (const EventData *event_data = event_ptr->GetData())
182fc38b2bSJonas Devlieghere       if (event_data->GetFlavor() == T::GetFlavorString())
192fc38b2bSJonas Devlieghere         return static_cast<const T *>(event_ptr->GetData());
202fc38b2bSJonas Devlieghere   return nullptr;
212fc38b2bSJonas Devlieghere }
222fc38b2bSJonas Devlieghere 
GetFlavorString()235e65e79bSJonas Devlieghere ConstString ProgressEventData::GetFlavorString() {
245e65e79bSJonas Devlieghere   static ConstString g_flavor("ProgressEventData");
255e65e79bSJonas Devlieghere   return g_flavor;
265e65e79bSJonas Devlieghere }
275e65e79bSJonas Devlieghere 
GetFlavor() const285e65e79bSJonas Devlieghere ConstString ProgressEventData::GetFlavor() const {
295e65e79bSJonas Devlieghere   return ProgressEventData::GetFlavorString();
305e65e79bSJonas Devlieghere }
315e65e79bSJonas Devlieghere 
Dump(Stream * s) const325e65e79bSJonas Devlieghere void ProgressEventData::Dump(Stream *s) const {
335e65e79bSJonas Devlieghere   s->Printf(" id = %" PRIu64 ", message = \"%s\"", m_id, m_message.c_str());
345e65e79bSJonas Devlieghere   if (m_completed == 0 || m_completed == m_total)
355e65e79bSJonas Devlieghere     s->Printf(", type = %s", m_completed == 0 ? "start" : "end");
365e65e79bSJonas Devlieghere   else
375e65e79bSJonas Devlieghere     s->PutCString(", type = update");
385e65e79bSJonas Devlieghere   // If m_total is UINT64_MAX, there is no progress to report, just "start"
395e65e79bSJonas Devlieghere   // and "end". If it isn't we will show the completed and total amounts.
405e65e79bSJonas Devlieghere   if (m_total != UINT64_MAX)
415e65e79bSJonas Devlieghere     s->Printf(", progress = %" PRIu64 " of %" PRIu64, m_completed, m_total);
425e65e79bSJonas Devlieghere }
435e65e79bSJonas Devlieghere 
445e65e79bSJonas Devlieghere const ProgressEventData *
GetEventDataFromEvent(const Event * event_ptr)455e65e79bSJonas Devlieghere ProgressEventData::GetEventDataFromEvent(const Event *event_ptr) {
462fc38b2bSJonas Devlieghere   return GetEventDataFromEventImpl<ProgressEventData>(event_ptr);
472fc38b2bSJonas Devlieghere }
482fc38b2bSJonas Devlieghere 
GetPrefix() const492fc38b2bSJonas Devlieghere llvm::StringRef DiagnosticEventData::GetPrefix() const {
502fc38b2bSJonas Devlieghere   switch (m_type) {
512fc38b2bSJonas Devlieghere   case Type::Warning:
522fc38b2bSJonas Devlieghere     return "warning";
532fc38b2bSJonas Devlieghere   case Type::Error:
542fc38b2bSJonas Devlieghere     return "error";
552fc38b2bSJonas Devlieghere   }
56ae2aa2d2SMartin Storsjö   llvm_unreachable("Fully covered switch above!");
572fc38b2bSJonas Devlieghere }
582fc38b2bSJonas Devlieghere 
Dump(Stream * s) const592fc38b2bSJonas Devlieghere void DiagnosticEventData::Dump(Stream *s) const {
60*990d0c71SJonas Devlieghere   llvm::HighlightColor color = m_type == Type::Warning
61*990d0c71SJonas Devlieghere                                    ? llvm::HighlightColor::Warning
62*990d0c71SJonas Devlieghere                                    : llvm::HighlightColor::Error;
63*990d0c71SJonas Devlieghere   llvm::WithColor(s->AsRawOstream(), color, llvm::ColorMode::Enable)
64*990d0c71SJonas Devlieghere       << GetPrefix();
65*990d0c71SJonas Devlieghere   *s << ": " << GetMessage() << '\n';
662fc38b2bSJonas Devlieghere   s->Flush();
672fc38b2bSJonas Devlieghere }
682fc38b2bSJonas Devlieghere 
GetFlavorString()692fc38b2bSJonas Devlieghere ConstString DiagnosticEventData::GetFlavorString() {
702fc38b2bSJonas Devlieghere   static ConstString g_flavor("DiagnosticEventData");
712fc38b2bSJonas Devlieghere   return g_flavor;
722fc38b2bSJonas Devlieghere }
732fc38b2bSJonas Devlieghere 
GetFlavor() const742fc38b2bSJonas Devlieghere ConstString DiagnosticEventData::GetFlavor() const {
752fc38b2bSJonas Devlieghere   return DiagnosticEventData::GetFlavorString();
762fc38b2bSJonas Devlieghere }
772fc38b2bSJonas Devlieghere 
782fc38b2bSJonas Devlieghere const DiagnosticEventData *
GetEventDataFromEvent(const Event * event_ptr)792fc38b2bSJonas Devlieghere DiagnosticEventData::GetEventDataFromEvent(const Event *event_ptr) {
802fc38b2bSJonas Devlieghere   return GetEventDataFromEventImpl<DiagnosticEventData>(event_ptr);
815e65e79bSJonas Devlieghere }
82