Loading...
Searching...
No Matches
diagnosticTrap.h
Go to the documentation of this file.
1//
2// Copyright 2026 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_BASE_TF_DIAGNOSTIC_TRAP_H
8#define PXR_BASE_TF_DIAGNOSTIC_TRAP_H
9
11
12#include "pxr/pxr.h"
13#include "pxr/base/tf/api.h"
17
18#include <vector>
19
20PXR_NAMESPACE_OPEN_SCOPE
21
61public:
62 TfDiagnosticTrap(const TfDiagnosticTrap&) = delete;
63 TfDiagnosticTrap& operator=(const TfDiagnosticTrap&) = delete;
64
68
72
75 TF_API void Dismiss();
76
78 TF_API void Clear();
79
81 TF_API void ClearErrors();
82
84 TF_API void ClearWarnings();
85
87 TF_API void ClearStatuses();
88
97 template <class Predicate>
98 size_t EraseMatching(Predicate &&pred) {
99 Tf_DiagnosticContainer result;
100 size_t erased = 0;
101 auto it = _container.GetIterator();
102 while (it.Next([&](auto const &d) {
103 if (auto r = TfTryInvoke<bool>(std::forward<Predicate>(pred), d)) {
104 if (*r) {
105 ++erased;
106 return; // erase
107 }
108 }
109 result.Append(d); // keep
110 }));
111 if (erased) {
112 _container = std::move(result);
113 _OnContentsChanged();
114 }
115 return erased;
116 }
117
119 TF_API bool IsClean() const;
120
122 TF_API bool HasErrors() const;
123
125 TF_API bool HasWarnings() const;
126
128 TF_API bool HasStatuses() const;
129
135 template <class Predicate>
136 bool HasAnyMatching(Predicate &&pred) const {
137 auto it = _container.GetIterator();
138 while (auto r = it.Next(std::forward<Predicate>(pred))) {
139 if (*r) {
140 return true;
141 }
142 }
143 return false;
144 }
145
175 template <class Predicate>
176 bool HasAllMatching(Predicate &&pred) const {
177 auto it = _container.GetIterator();
178 while (auto r = it.Next(std::forward<Predicate>(pred))) {
179 if (!*r) {
180 return false;
181 }
182 }
183 return true;
184 }
185
190 template <class Predicate>
191 size_t CountMatching(Predicate &&pred) const {
192 size_t count = 0;
193 auto it = _container.GetIterator();
194 while (auto r = it.Next(std::forward<Predicate>(pred))) {
195 if (*r) {
196 ++count;
197 }
198 }
199 return count;
200 }
201
203 TF_API std::vector<TfError> const& GetErrors() const;
204
206 TF_API std::vector<TfWarning> const& GetWarnings() const;
207
209 TF_API std::vector<TfStatus> const& GetStatuses() const;
210
219 template <class Fn>
220 void ForEach(Fn &&fn) const {
221 Tf_DiagnosticContainer copy(_container);
222 auto it = copy.GetIterator();
223 while (it.Next(std::forward<Fn>(fn))) {}
224 }
225
231
232private:
233 friend class TfDiagnosticMgr;
234
235 TF_API void _OnContentsChanged() const;
236
237 Tf_DiagnosticContainer _container;
238 bool _active = false;
239};
240
241PXR_NAMESPACE_CLOSE_SCOPE
242
243#endif // PXR_BASE_TF_DIAGNOSTIC_TRAP_H
Singleton class through which all errors and diagnostics pass.
Definition: diagnosticMgr.h:55
A facility for transporting diagnostics from thread to thread.
A scoped, stack-based mechanism for intercepting and examining diagnostics issued on the current thre...
TF_API std::vector< TfError > const & GetErrors() const
Return the captured errors.
TF_API void ClearErrors()
Discard all captured errors. They will not be re-posted.
TF_API void Dismiss()
Re-post any uncleared diagnostics and deactivate the trap; called automatically on destruction if not...
TF_API TfDiagnosticTrap()
Construct a trap and install it as the active interceptor on the current thread.
TF_API void ClearWarnings()
Discard all captured warnings. They will not be re-posted.
TF_API bool HasStatuses() const
Return true if any status messages have been captured.
size_t CountMatching(Predicate &&pred) const
Return the number of captured diagnostics for which pred returns true.
TF_API void Clear()
Discard all captured diagnostics. They will not be re-posted.
TF_API bool HasWarnings() const
Return true if any warnings have been captured.
TF_API bool HasErrors() const
Return true if any errors have been captured.
bool HasAnyMatching(Predicate &&pred) const
Invoke pred on the captured diagnostics in order, returning true as soon as pred returns true for any...
TF_API std::vector< TfWarning > const & GetWarnings() const
Return the captured warnings.
size_t EraseMatching(Predicate &&pred)
Erase all captured diagnostics for which pred returns true, preserving the relative order of the rema...
TF_API ~TfDiagnosticTrap()
Destroy the trap, re-posting any diagnostics not explicitly discarded by calls to the Clear or Erase ...
TF_API TfDiagnosticTransport Transport()
Move all accumulated diagnostics into a TfDiagnosticTransport, leaving this trap active but empty.
TF_API bool IsClean() const
Return true if no diagnostics have been captured.
TF_API std::vector< TfStatus > const & GetStatuses() const
Return the captured status messages.
void ForEach(Fn &&fn) const
Invoke fn for each captured diagnostic in the order they were received.
TF_API void ClearStatuses()
Discard all captured status messages. They will not be re-posted.
bool HasAllMatching(Predicate &&pred) const
Invoke pred on the captured diagnostics in order, returning false as soon as pred returns false for a...