swift
logpattern.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (C) 2014 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #ifndef SWIFT_MISC_LOGPATTERN_H
7 #define SWIFT_MISC_LOGPATTERN_H
8 
9 #include <QDBusArgument>
10 #include <QMetaType>
11 #include <QSet>
12 #include <QString>
13 #include <QStringList>
14 #include <QtGlobal>
15 
16 #include "misc/metaclass.h"
19 #include "misc/mixin/mixindbus.h"
20 #include "misc/mixin/mixinhash.h"
21 #include "misc/mixin/mixinicon.h"
22 #include "misc/mixin/mixinindex.h"
24 #include "misc/mixin/mixinstring.h"
25 #include "misc/statusmessage.h"
26 #include "misc/swiftmiscexport.h"
27 #include "misc/variant.h"
28 
29 template <class Key, class T>
30 class QHash;
31 
32 namespace swift::misc
33 {
34  class CLogCategory;
35  class CLogCategoryList;
36 
41  public mixin::MetaType<CLogPattern>,
42  public mixin::HashByMetaClass<CLogPattern>,
43  public mixin::EqualsByMetaClass<CLogPattern>,
44  public mixin::DBusOperators<CLogPattern>,
45  public mixin::DataStreamOperators<CLogPattern>,
46  public mixin::Index<CLogPattern>,
47  public mixin::String<CLogPattern>,
48  public mixin::Icon<CLogPattern>
49  {
50  public:
52  static const QStringList &allHumanReadableNames();
53 
55  static const CLogPattern &fromHumanReadableName(const QString &name);
56 
58  static QStringList humanReadableNamesFrom(const CStatusMessage &message);
59 
61  static QStringList humanOrTechnicalCategoriesFrom(const CStatusMessage &message);
62 
64  CLogPattern();
65 
67  static CLogPattern exactMatch(const CLogCategory &category);
68 
70  static CLogPattern anyOf(const CLogCategoryList &categories);
71 
73  static CLogPattern allOf(const CLogCategoryList &categories);
74 
76  static CLogPattern startsWith(const QString &prefix);
77 
79  static CLogPattern endsWith(const QString &suffix);
80 
82  static CLogPattern contains(const QString &substring);
83 
85  static CLogPattern empty();
86 
88  CLogPattern withSeverity(CStatusMessage::StatusSeverity severity) const;
89 
91  CLogPattern withSeverities(const QSet<CStatusMessage::StatusSeverity> &severities) const;
92 
95  CLogPattern withSeverityAtOrAbove(CStatusMessage::StatusSeverity minimumSeverity) const;
96 
98  bool match(const CStatusMessage &message) const;
99 
101  bool matches(const CVariant &message) const { return match(message.to<CStatusMessage>()); }
102 
104  QSet<QString> getCategoryStrings() const { return m_strings; }
105 
112  bool isProperSubsetOf(const CLogPattern &other) const;
113 
115  QString convertToQString(bool i18n = false) const;
116 
118  void marshallToDbus(QDBusArgument &argument) const;
119 
121  void unmarshallFromDbus(const QDBusArgument &argument);
122 
124  void marshalToDataStream(QDataStream &stream) const;
125 
127  void unmarshalFromDataStream(QDataStream &stream);
128 
129  private:
130  bool checkInvariants() const;
131 
132  enum Strategy
133  {
134  Everything = 0,
135  ExactMatch,
136  AnyOf,
137  AllOf,
138  StartsWith,
139  EndsWith,
140  Contains,
141  Nothing
142  };
143 
144  CLogPattern(Strategy strategy, const QSet<QString> &strings);
145 
146  QSet<CStatusMessage::StatusSeverity> m_severities;
147  Strategy m_strategy;
148  QSet<QString> m_strings;
149 
150  const QString &getString() const
151  {
152  Q_ASSERT(m_strategy == ExactMatch && m_strings.size() == 1);
153  return *m_strings.begin();
154  }
155  const QString &getPrefix() const
156  {
157  Q_ASSERT(m_strategy == StartsWith && m_strings.size() == 1);
158  return *m_strings.begin();
159  }
160  const QString &getSuffix() const
161  {
162  Q_ASSERT(m_strategy == EndsWith && m_strings.size() == 1);
163  return *m_strings.begin();
164  }
165  const QString &getSubstring() const
166  {
167  Q_ASSERT(m_strategy == Contains && m_strings.size() == 1);
168  return *m_strings.begin();
169  }
170 
171  static const QHash<QString, CLogPattern> &allHumanReadablePatterns();
172 
174  CLogPattern,
175  SWIFT_METAMEMBER(severities),
176  SWIFT_METAMEMBER(strategy),
177  SWIFT_METAMEMBER(strings));
178  };
179 } // namespace swift::misc
180 
181 Q_DECLARE_METATYPE(swift::misc::CLogPattern)
182 
183 #endif // SWIFT_MISC_LOGPATTERN_H
A log category is an arbitrary string tag which can be attached to log messages.
Definition: logcategory.h:28
A sequence of log categories.
Value class for matching log messages based on their categories.
Definition: logpattern.h:49
QSet< QString > getCategoryStrings() const
Technical category names matched by this pattern.
Definition: logpattern.h:104
bool matches(const CVariant &message) const
This class acts as a SharedState filter when stored in a CVariant.
Definition: logpattern.h:101
Streamable status message, e.g.
Wrapper around QVariant which provides transparent access to CValueObject methods of the contained ob...
Definition: variant.h:66
T to() const
Synonym for value().
Definition: variant.h:176
CRTP class template which will generate marshalling operators for a derived class with its own marsha...
Definition: mixindbus.h:39
CRTP class template to generate non-member QDataStream streaming operators.
CRTP class template from which a derived class can inherit operator== implemented by metaclass.
Definition: mixincompare.h:46
CRTP class template from which a derived class can inherit common methods dealing with hashing instan...
Definition: mixinhash.h:48
CRTP class template from which a derived class can inherit icon-related functions.
Definition: mixinicon.h:28
CRTP class template from which a derived class can inherit property indexing functions.
Definition: mixinindex.h:40
CRTP class template from which a derived class can inherit common methods dealing with the metatype o...
Definition: mixinmetatype.h:29
CRTP class template from which a derived class can inherit string streaming operations.
Definition: mixinstring.h:31
#define SWIFT_METAMEMBER(MEMBER,...)
Macro to define an element within a metaclass.
Definition: metaclass.h:73
#define SWIFT_METACLASS(CLASS,...)
Macro to define a nested metaclass that describes the attributes of its enclosing class.
Definition: metaclass.h:53
Free functions in swift::misc.
StatusSeverity
Status severities.
Definition: statusmessage.h:35
#define SWIFT_MISC_EXPORT
Export a class or function from the library.