forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathException.cpp
90 lines (76 loc) · 2.38 KB
/
Exception.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "c10/util/Exception.h"
#include "c10/util/Backtrace.h"
#include "c10/util/Type.h"
#include "c10/util/Logging.h"
#include <iostream>
#include <numeric>
#include <string>
namespace c10 {
Error::Error(
const std::string& new_msg,
const std::string& backtrace,
const void* caller)
: msg_stack_{new_msg}, backtrace_(backtrace), caller_(caller) {
msg_ = msg();
msg_without_backtrace_ = msg_without_backtrace();
}
// PyTorch-style error message
// Error::Error(SourceLocation source_location, const std::string& msg)
// NB: This is defined in Logging.cpp for access to GetFetchStackTrace
// Caffe2-style error message
Error::Error(
const char* file,
const uint32_t line,
const char* condition,
const std::string& msg,
const std::string& backtrace,
const void* caller)
: Error(
str("[enforce fail at ",
detail::StripBasename(file),
":",
line,
"] ",
condition,
". ",
msg,
"\n"),
backtrace,
caller) {}
std::string Error::msg() const {
return std::accumulate(
msg_stack_.begin(), msg_stack_.end(), std::string("")) +
backtrace_;
}
std::string Error::msg_without_backtrace() const {
return std::accumulate(msg_stack_.begin(), msg_stack_.end(), std::string(""));
}
void Error::AppendMessage(const std::string& new_msg) {
msg_stack_.push_back(new_msg);
// Refresh the cache
// TODO: Calling AppendMessage O(n) times has O(n^2) cost. We can fix
// this perf problem by populating the fields lazily... if this ever
// actually is a problem.
msg_ = msg();
msg_without_backtrace_ = msg_without_backtrace();
}
void Warning::warn(SourceLocation source_location, std::string msg) {
warning_handler_(source_location, msg.c_str());
}
void Warning::set_warning_handler(handler_t handler) {
warning_handler_ = handler;
}
void Warning::print_warning(
const SourceLocation& source_location,
const char* msg) {
std::cerr << "Warning: " << msg << " (" << source_location << ")\n";
}
Warning::handler_t Warning::warning_handler_ = &Warning::print_warning;
std::string GetExceptionString(const std::exception& e) {
#ifdef __GXX_RTTI
return demangle(typeid(e).name()) + ": " + e.what();
#else
return std::string("Exception (no RTTI available): ") + e.what();
#endif // __GXX_RTTI
}
} // namespace c10