forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
source_location.h
54 lines (47 loc) · 1.4 KB
/
source_location.h
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
#pragma once
#include <ostream>
#include <sstream>
#include <stdexcept>
#include <string>
namespace torch {
namespace jit {
// SourceLocation represents source code-level debug information for a node.
// It contains information about where a node got generated.
// In the case of tracing this will be a python stack trace.
// In the case of using the scripting frontend this will be backed
// by a SourceRange object
struct SourceLocation {
virtual ~SourceLocation() = default;
virtual void highlight(std::ostream& out) const = 0;
std::string wrapException(
const std::exception& e,
const std::string& additional = "") {
std::stringstream msg;
msg << "\n" << e.what() << ":\n";
if (!additional.empty()) {
msg << additional << ":\n";
}
highlight(msg);
return msg.str();
}
void wrapAndRethrowException(
const std::exception& e,
const std::string& additional = "") {
throw std::runtime_error(wrapException(e, additional));
}
};
inline std::ostream& operator<<(std::ostream& out, const SourceLocation& sl) {
sl.highlight(out);
return out;
}
// normally a python stack trace
struct StringSourceLocation : public SourceLocation {
StringSourceLocation(std::string context) : context(std::move(context)) {}
void highlight(std::ostream& out) const override {
out << context;
}
private:
std::string context;
};
} // namespace jit
} // namespace torch