7#ifndef PXR_BASE_TF_DIAGNOSTIC_MGR_H
8#define PXR_BASE_TF_DIAGNOSTIC_MGR_H
20#include "pxr/base/tf/spinRWMutex.h"
21#include "pxr/base/tf/status.h"
23#include "pxr/base/tf/warning.h"
26#include "pxr/base/tf/api.h"
33#include <tbb/enumerable_thread_specific.h>
43PXR_NAMESPACE_OPEN_SCOPE
46 TF_LOG_STACK_TRACE_ON_ERROR,
47 TF_LOG_STACK_TRACE_ON_WARNING,
48 TF_ERROR_MARK_TRACKING,
49 TF_PRINT_ALL_POSTED_ERRORS_TO_STDERR
65 typedef std::list<TfError> ErrorList;
97 const TfCallContext &context,
const std::string &msg,
98 const TfDiagnosticInfo &info);
134 std::string
const &msg) = 0;
171 return _threadState.local()._errorList.begin();
176 return _threadState.local()._errorList.end();
204 TfCallContext
const &context,
205 const std::string& commentary, TfDiagnosticInfo info,
222 TfCallContext
const &context, std::string
const &commentary,
223 TfDiagnosticInfo info,
bool quiet);
238 TfCallContext
const &context, std::string
const &commentary,
239 TfDiagnosticInfo info,
bool quiet);
255 std::string
const &msg)
const;
260 return _markCountsAndTrapStacks.local().markCount > 0;
270 ErrorHelper(TfCallContext
const &context,
TfEnum errorCode,
271 const char* errorCodeString)
272 : _context(context), _errorCode(errorCode),
273 _errorCodeString(errorCodeString)
278 void Post(
const char* fmt, ...) const
282 void PostQuietly(const
char* fmt, ...) const
286 void Post(const
std::
string& msg) const;
290 const
std::
string& msg,
291 TfDiagnosticInfo info = TfDiagnosticInfo()) const;
294 void PostQuietly(const
std::
string& msg,
295 TfDiagnosticInfo info = TfDiagnosticInfo()) const;
298 TfCallContext _context;
300 const
char *_errorCodeString;
303 struct WarningHelper {
304 WarningHelper(TfCallContext
const &context,
TfEnum warningCode,
305 const char *warningCodeString)
306 : _context(context), _warningCode(warningCode),
307 _warningCodeString(warningCodeString)
312 void Post(
const char* fmt, ...) const
316 void PostQuietly(const
char* fmt, ...) const
320 void Post(const
std::
string &str) const;
324 const
std::
string& msg,
325 TfDiagnosticInfo info = TfDiagnosticInfo()) const;
328 void PostQuietly(const
std::
string& msg) const;
331 TfCallContext _context;
333 const
char *_warningCodeString;
336 struct StatusHelper {
337 StatusHelper(TfCallContext
const &context,
TfEnum statusCode,
338 const char *statusCodeString)
339 : _context(context), _statusCode(statusCode),
340 _statusCodeString(statusCodeString)
345 void Post(
const char* fmt, ...) const
349 void PostQuietly(const
char* fmt, ...) const
353 void Post(const
std::
string &str) const;
357 const
std::
string& msg,
358 TfDiagnosticInfo info = TfDiagnosticInfo()) const;
361 void PostQuietly(const
std::
string& msg) const;
364 TfCallContext _context;
366 const
char *_statusCodeString;
370 FatalHelper(TfCallContext
const &context,
TfEnum statusCode)
372 _statusCode(statusCode)
376 void Post(
const std::string &str)
const {
377 This::GetInstance().PostFatal(_context, _statusCode, str);
380 TfCallContext _context;
395 ErrorIterator _GetErrorMarkBegin(
size_t mark,
size_t *nErrors);
398 inline void *_CreateErrorMark() {
399 size_t &markCount = _markCountsAndTrapStacks.local().markCount;
405 inline bool _DestroyErrorMark(
void *key) {
406 size_t &markCount = *
static_cast<size_t *
>(key);
407 return --markCount == 0;
420 void _ReportError(
const TfError &err);
425 void _SpliceErrors(ErrorList &src);
428 void _AppendPendingErrorsLogText(ErrorIterator i);
433 void _RebuildPendingErrorLogText(
size_t startSerial = 0);
442 void _RebuildTrappedDiagnosticsLogText(
size_t validLogEnd = 0);
448 _LogTextPin() =
default;
449 _LogTextPin(_LogTextPin &&) =
default;
450 TF_API _LogTextPin &operator=(_LogTextPin &&);
451 _LogTextPin(
const _LogTextPin &) =
delete;
452 _LogTextPin &operator=(
const _LogTextPin &) =
delete;
453 TF_API ~_LogTextPin();
455 friend class Tf_DiagnosticMgrTestAccess;
457 _LogTextPin(std::string &&key,
458 std::unique_ptr<std::vector<std::string>> &&lines);
460 std::unique_ptr<std::vector<std::string>> _lines;
467 _PinErrorLogText(ErrorIterator first, ErrorIterator last)
const;
472 _PinDiagnosticsLogText(Tf_DiagnosticContainer
const &container)
const;
477 bool _ForEachDelegate(Fn
const &fn)
const;
481 mutable tbb::enumerable_thread_specific<bool> _reentrantGuard;
484 std::vector<Delegate*> _delegates;
489 std::atomic<size_t> _nextSerial;
495 struct _LogTextBuffer {
496 _LogTextBuffer() =
default;
497 explicit _LogTextBuffer(std::string &&label);
500 template <
class Fn>
void Update(Fn &&fn);
502 size_t GetSize()
const {
503 return _texts.first.size();
507 std::pair<std::vector<std::string>,
508 std::vector<std::string>> _texts;
509 bool _parity =
false;
513 struct _ThreadState {
515 : _pendingErrorsLogText(_MakeLabel(
"Pending Errors"))
516 , _trappedDiagnosticsLogText(_MakeLabel(
"Pending Diagnostics"))
518 static std::string _MakeLabel(
const char *suffix);
519 ErrorList _errorList;
520 _LogTextBuffer _pendingErrorsLogText;
521 _LogTextBuffer _trappedDiagnosticsLogText;
524 tbb::enumerable_thread_specific<_ThreadState> _threadState;
528 static constexpr size_t _TrapStackLocalCap =
529 TfComputeSmallVectorLocalCapacityForTotalSize<
536 struct _MarkCountAndTrapStack {
537 size_t markCount = 0;
538 _TrapStack trapStack;
545 tbb::enumerable_thread_specific<
546 _MarkCountAndTrapStack,
547 tbb::cache_aligned_allocator<_MarkCountAndTrapStack>,
548 tbb::ets_key_per_instance> _markCountsAndTrapStacks;
552 friend class Tf_DiagnosticContainer;
553 friend class Tf_DiagnosticMgrTestAccess;
563PXR_NAMESPACE_CLOSE_SCOPE
Provide architecture-specific memory-alignment information.
Define function attributes.
#define ARCH_PRINTF_FUNCTION(_fmt, _firstArg)
Macro used to indicate a function takes a printf-like specification.
Provide facilities for error handling in script.
Functions for recording call locations.
Represents the base class of an object representing a diagnostic message.
One may set a delegate with the TfDiagnosticMgr which will be called to respond to errors and diagnos...
virtual void IssueStatus(TfStatus const &status)=0
Called when a TF_STATUS() is issued.
virtual void IssueError(TfError const &err)=0
Called when a TfError is posted.
TF_API void _UnhandledAbort() const
Abort the program, but avoid the session logging mechanism.
virtual void IssueWarning(TfWarning const &warning)=0
Called when a TF_WARNING() is issued.
virtual void IssueFatalError(TfCallContext const &context, std::string const &msg)=0
Called when a TF_FATAL_ERROR is issued (or a failed TF_AXIOM).
Singleton class through which all errors and diagnostics pass.
TF_API void RemoveDelegate(Delegate *delegate)
Removes all delegates equal to delegate from the current delegates.
TF_API void PostError(const TfDiagnosticBase &diagnostic)
This method will create a TfError, append it to the error list, and pass it to all delegates.
TF_API void PostStatus(TfEnum statusCode, const char *statusCodeString, TfCallContext const &context, std::string const &commentary, TfDiagnosticInfo info, bool quiet)
This method will create a TfStatus and pass it to all delegates.
TF_API void AddDelegate(Delegate *delegate)
Add the delegate delegate to the list of current delegates.
TF_API void SetQuiet(bool quiet)
Set whether errors, warnings and status messages should be printed out to the terminal.
TF_API void PostWarning(const TfDiagnosticBase &diagnostic)
This method will create a TfWarning and pass it to all delegates.
TF_API void PostError(TfEnum errorCode, const char *errorCodeString, TfCallContext const &context, const std::string &commentary, TfDiagnosticInfo info, bool quiet)
This method will create a TfError, append it to the error list, and pass it to all delegates.
TF_API ErrorIterator EraseError(ErrorIterator i)
Remove error specified by iterator i.
static TF_API This & GetInstance()
Return the singleton instance.
TF_API void PostFatal(TfCallContext const &context, TfEnum statusCode, std::string const &msg) const
This method will issue a fatal error to all delegates.
ErrorIterator GetErrorEnd()
Return an iterator to the end of this thread's error list.
TF_API void AppendError(TfError const &e)
Append an error to the list of active errors.
ErrorList::iterator ErrorIterator
Synonym for standard STL iterator to traverse the error list.
TF_API ErrorIterator EraseRange(ErrorIterator first, ErrorIterator last)
Remove all the errors in [first, last) from this thread's error stream.
bool HasActiveErrorMark()
Return true if an instance of TfErrorMark exists in the current thread of execution,...
TF_API void PostStatus(const TfDiagnosticBase &diagnostic)
This method will create a TfStatus and pass it to all delegates.
TF_API void PostWarning(TfEnum warningCode, const char *warningCodeString, TfCallContext const &context, std::string const &commentary, TfDiagnosticInfo info, bool quiet)
This method will create a TfWarning and pass it to all delegates.
static TF_API std::string FormatDiagnostic(const TfEnum &code, const TfCallContext &context, const std::string &msg, const TfDiagnosticInfo &info)
Return a human-readable diagnostic message.
ErrorIterator GetErrorBegin()
Return an iterator to the beginning of this thread's error list.
static TF_API std::string GetCodeName(const TfEnum &code)
Returns the name of the given diagnostic code.
A facility for transporting diagnostics from thread to thread.
A scoped, stack-based mechanism for intercepting and examining diagnostics issued on the current thre...
An enum class that records both enum type and enum value.
Represents an object that contains error information.
Class used to record the end of the error-list.
A facility for transporting errors from thread to thread.
Manage a single instance of an object (see.
static T & GetInstance()
Return a reference to an object of type T, creating it if necessary.
This is a small-vector class with local storage optimization, the local storage can be specified via ...
This class implements a readers-writer spin lock that emphasizes throughput when there is light conte...
Represents an object that contains information about a status message.
Represents an object that contains information about a warning.
Enable a concrete base class for use with TfWeakPtr.
Conditional debugging output class and macros.
Stripped down version of diagnostic.h that doesn't define std::string.
Define preprocessor function name macros.
#define ARCH_CACHE_LINE_SIZE
The size of a CPU cache line on the current processor architecture in bytes.
#define TF_DEBUG_CODES(...)
Define debugging symbols.
Manage a single instance of an object.
Definitions of basic string utilities in tf.
Pointer storage with deletion detection.