swift
statusmessagefilter.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2016 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #include "misc/logpattern.h"
7 
8 using namespace swift::misc;
9 
10 namespace swift::gui::models
11 {
12  CStatusMessageFilter::CStatusMessageFilter(CStatusMessage::StatusSeverity severity, const QString &text,
13  const QString &category)
14  : m_severity(severity), m_msgText(text.trimmed()), m_category(category.trimmed())
15  {
16  // info or debug are treated like no filter
17  this->m_valid = !((severity == CStatusMessage::SeverityInfo || severity == CStatusMessage::SeverityDebug) &&
18  m_msgText.isEmpty() && m_category.isEmpty());
19  }
20 
22  {
23  if (!this->isValid()) { return inContainer; }
24  CStatusMessageList outContainer;
25  for (const CStatusMessage &msg : inContainer)
26  {
27  if (!(m_severity == CStatusMessage::SeverityInfo || m_severity == CStatusMessage::SeverityDebug))
28  {
29  if (!msg.isSeverityHigherOrEqual(this->m_severity)) { continue; }
30  }
31 
32  if (!this->m_msgText.isEmpty())
33  {
34  if (!this->stringMatchesFilterExpression(msg.getMessage(), this->m_msgText)) { continue; }
35  }
36 
37  if (!this->m_category.isEmpty())
38  {
39  if (!this->stringMatchesFilterExpression(CLogPattern::humanOrTechnicalCategoriesFrom(msg).join(", "),
40  this->m_category))
41  {
42  continue;
43  }
44  }
45 
46  outContainer.push_back(msg);
47  }
48  return outContainer;
49  }
50 
52  {
53  if (!m_category.contains('*'))
54  {
55  if (CLogPattern::allHumanReadableNames().contains(m_category))
56  {
57  return CVariant::from(CLogPattern::fromHumanReadableName(m_category).withSeverityAtOrAbove(m_severity));
58  }
59  return CVariant::from(CLogPattern::exactMatch(m_category).withSeverityAtOrAbove(m_severity));
60  }
61 
62  CLogCategoryList categories = CLogCategoryList::fromQStringList(CLogCategories::allSpecialCategories());
63  categories.removeIf([this](const CLogCategory &cat) {
64  return this->stringMatchesFilterExpression(cat.toQString(), this->m_category);
65  });
66  CSequence<QString> humanNames = CLogPattern::allHumanReadableNames();
67  humanNames.removeIf(
68  [this](const QString &name) { return this->stringMatchesFilterExpression(name, this->m_category); });
69  auto humanCats = humanNames.transform([](const QString &name) {
70  const auto strings = CLogPattern::fromHumanReadableName(name).getCategoryStrings();
71  return strings.isEmpty() ? QString {} : *strings.begin();
72  });
73 
74  return CVariant::from(CLogPattern::anyOf(categories.join(humanCats)).withSeverityAtOrAbove(m_severity));
75  }
76 } // namespace swift::gui::models
virtual swift::misc::CVariant getAsValueObject() const
Return an implementation-specific value object representing the filter.
virtual swift::misc::CStatusMessageList filter(const swift::misc::CStatusMessageList &inContainer) const
Used container data.
bool stringMatchesFilterExpression(const QString &value, const QString &filter, Qt::CaseSensitivity cs=Qt::CaseInsensitive) const
Standard string search supporting wildcard at begin and end: "*xyz", "abc*".
Definition: modelfilter.cpp:31
A log category is an arbitrary string tag which can be attached to log messages.
Definition: logcategory.h:28
A sequence of log categories.
auto transform(F function) const
Return a new container generated by applying some transformation function to all elements of this one...
Definition: range.h:403
Generic sequential container with value semantics.
Definition: sequence.h:86
int removeIf(Predicate p)
Remove elements for which a given predicate returns true.
Definition: sequence.h:446
void push_back(const T &value)
Appends an element at the end of the sequence.
Definition: sequence.h:305
CSequence join(const CSequence &other) const
Concatenates two sequences and returns the result.
Definition: sequence.h:350
Streamable status message, e.g.
Status messages, e.g. from Core -> GUI.
Wrapper around QVariant which provides transparent access to CValueObject methods of the contained ob...
Definition: variant.h:66
QString toQString(bool i18n=false) const
Cast as QString.
Definition: mixinstring.h:76
Models to be used with views, mainly QTableView.
Free functions in swift::misc.
StatusSeverity
Status severities.
Definition: statusmessage.h:35