All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
error.h
1 //
2 // Copyright 2017 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef _GUSD_ERROR_H_
25 #define _GUSD_ERROR_H_
26 
27 #include "gusd/api.h"
28 
29 #include "pxr/pxr.h"
30 
31 #include "pxr/base/arch/hints.h"
32 #include "pxr/base/tf/errorMark.h"
33 
34 #include <UT/UT_ErrorManager.h>
35 #include <UT/UT_Lock.h>
36 #include <UT/UT_NonCopyable.h>
37 #include <UT/UT_WorkBuffer.h>
38 
39 
40 PXR_NAMESPACE_OPEN_SCOPE
41 
42 
54 #define GUSD_GENERIC_ERR(sev) \
55  if(sev == UT_ERROR_NONE) /*empty*/ ; else GusdPostErrorHelper(sev)
56 
57 #define GUSD_ERR() GusdPostErrorHelper(UT_ERROR_ABORT)
58 
59 #define GUSD_WARN() GusdPostErrorHelper(UT_ERROR_WARNING)
60 
61 #define GUSD_MSG() GusdPostErrorHelper(UT_ERROR_MESSAGE)
62 
63 
64 class GUSD_API GusdPostErrorHelper
65 {
66 public:
67  GusdPostErrorHelper(UT_ErrorSeverity sev) : _sev(sev) {}
68 
69  void Msg(const char* msg) const;
70 
71  template <typename T, typename... Args>
72  void Msg(const char* format, T&& arg1, Args&&... args) const;
73 
74 private:
75  UT_ErrorSeverity _sev;
76 };
77 
78 
79 template <typename T, typename... Args>
80 void
81 GusdPostErrorHelper::Msg(const char* format, T&& arg1, Args&&... args) const
82 {
83  UT_WorkBuffer buf;
84  buf.sprintf(format, std::forward<T>(arg1), std::forward<Args>(args)...);
85  UTaddGeneric(_sev, "Common", UT_ERROR_JUST_STRING, buf.buffer());
86 }
87 
88 
111 class GUSD_API GusdErrorTransport : UT_NonCopyable
112 {
113 public:
114  GusdErrorTransport(UT_ErrorManager* mgr=UTgetErrorManager()) : _mgr(mgr) {}
115 
116  GusdErrorTransport(int thread) : _mgr(UTgetErrorManager(thread)) {}
117 
118  void operator()()
119  { StealGlobalErrors(); }
120 
121  void StealErrors(UT_ErrorManager& victim,
122  UT_ErrorSeverity sev=UT_ERROR_NONE,
123  bool borrowOnly=false);
124 
125  void StealGlobalErrors(UT_ErrorSeverity sev=UT_ERROR_NONE,
126  bool borrowOnly=false)
127  { StealErrors(*UTgetErrorManager(), sev, borrowOnly); }
128 
129 private:
130  UT_Lock _lock;
131  UT_ErrorManager* const _mgr;
132 };
133 
134 
139 class GusdAutoErrorTransport : UT_NonCopyable
140 {
141 public:
143  : _transport(transport) {}
144 
145  ~GusdAutoErrorTransport() { _transport(); }
146 
147 private:
148  GusdErrorTransport& _transport;
149 };
150 
151 
154 GUSD_API std::string
155 GusdGetErrors(UT_ErrorManager* mgr=UTgetErrorManager(),
156  UT_ErrorSeverity sev=UT_ERROR_NONE);
157 
158 
162 class GUSD_API GusdTfErrorScope : UT_NonCopyable
163 {
164 public:
168  GusdTfErrorScope(UT_ErrorSeverity sev=UT_ERROR_ABORT,
169  UT_ErrorManager* mgr=UTgetErrorManager())
170  : _mgr(mgr), _sev(sev)
171  { _mark.SetMark(); }
172 
174  {
175  if(ARCH_UNLIKELY(!_mark.IsClean()))
176  _Update();
177  }
178 
179  explicit operator bool() const { return _mgr; }
180 
183  UT_ErrorSeverity Update()
184  {
185  if(_mark.IsClean())
186  return UT_ERROR_NONE;
187  return _Update();
188  }
189 
190  bool IsClean() const { return _mark.IsClean(); }
191 
192  UT_ErrorSeverity GetLogSeverity() const { return _sev; }
193 
194 protected:
195  UT_ErrorSeverity _Update();
196 
197 private:
198  TfErrorMark _mark;
199  UT_ErrorManager* const _mgr;
200  const UT_ErrorSeverity _sev;
201 };
202 
203 
204 PXR_NAMESPACE_CLOSE_SCOPE
205 
206 #endif // _GUSD_ERROR_H_
Helper class used to propagate errors from different threads.
Definition: error.h:111
Helper for catching Tf errors and forwarding them to a UT_ErrorManager.
Definition: error.h:162
Helper for ensuring consistent, automatic transport of errors from within threaded loops...
Definition: error.h:139
UT_ErrorSeverity Update()
Clean any errors on the current scope.
Definition: error.h:183
GusdTfErrorScope(UT_ErrorSeverity sev=UT_ERROR_ABORT, UT_ErrorManager *mgr=UTgetErrorManager())
Construct a scope for capturing Tf errors and forwarding them to mgr.
Definition: error.h:168
Class used to record the end of the error-list.
Definition: errorMark.h:66