• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 The Dawn Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "dawn_native/ErrorData.h"
16 
17 #include "dawn_native/Error.h"
18 #include "dawn_native/ObjectBase.h"
19 #include "dawn_native/dawn_platform.h"
20 
21 namespace dawn_native {
22 
Create(InternalErrorType type,std::string message,const char * file,const char * function,int line)23     std::unique_ptr<ErrorData> ErrorData::Create(InternalErrorType type,
24                                                  std::string message,
25                                                  const char* file,
26                                                  const char* function,
27                                                  int line) {
28         std::unique_ptr<ErrorData> error = std::make_unique<ErrorData>(type, message);
29         error->AppendBacktrace(file, function, line);
30         return error;
31     }
32 
ErrorData(InternalErrorType type,std::string message)33     ErrorData::ErrorData(InternalErrorType type, std::string message)
34         : mType(type), mMessage(std::move(message)) {
35     }
36 
AppendBacktrace(const char * file,const char * function,int line)37     void ErrorData::AppendBacktrace(const char* file, const char* function, int line) {
38         BacktraceRecord record;
39         record.file = file;
40         record.function = function;
41         record.line = line;
42 
43         mBacktrace.push_back(std::move(record));
44     }
45 
AppendContext(std::string context)46     void ErrorData::AppendContext(std::string context) {
47         mContexts.push_back(std::move(context));
48     }
49 
AppendDebugGroup(std::string label)50     void ErrorData::AppendDebugGroup(std::string label) {
51         mDebugGroups.push_back(std::move(label));
52     }
53 
GetType() const54     InternalErrorType ErrorData::GetType() const {
55         return mType;
56     }
57 
GetMessage() const58     const std::string& ErrorData::GetMessage() const {
59         return mMessage;
60     }
61 
GetBacktrace() const62     const std::vector<ErrorData::BacktraceRecord>& ErrorData::GetBacktrace() const {
63         return mBacktrace;
64     }
65 
GetContexts() const66     const std::vector<std::string>& ErrorData::GetContexts() const {
67         return mContexts;
68     }
69 
GetDebugGroups() const70     const std::vector<std::string>& ErrorData::GetDebugGroups() const {
71         return mDebugGroups;
72     }
73 
GetFormattedMessage() const74     std::string ErrorData::GetFormattedMessage() const {
75         std::ostringstream ss;
76         ss << mMessage << "\n";
77 
78         if (!mContexts.empty()) {
79             for (auto context : mContexts) {
80                 ss << " - While " << context << "\n";
81             }
82         }
83 
84         // For non-validation errors, or erros that lack a context include the
85         // stack trace for debugging purposes.
86         if (mContexts.empty() || mType != InternalErrorType::Validation) {
87             for (const auto& callsite : mBacktrace) {
88                 ss << "    at " << callsite.function << " (" << callsite.file << ":"
89                    << callsite.line << ")\n";
90             }
91         }
92 
93         if (!mDebugGroups.empty()) {
94             ss << "\nDebug group stack:\n";
95             for (auto label : mDebugGroups) {
96                 ss << " > \"" << label << "\"\n";
97             }
98         }
99 
100         return ss.str();
101     }
102 
103 }  // namespace dawn_native
104