Caffe2 - C++ API
A deep learning, cross platform ML framework
source_location.h
1 #pragma once
2 
3 #include <ostream>
4 #include <sstream>
5 #include <stdexcept>
6 #include <string>
7 
8 namespace torch {
9 namespace jit {
10 // SourceLocation represents source code-level debug information for a node.
11 // It contains information about where a node got generated.
12 // In the case of tracing this will be a python stack trace.
13 // In the case of using the scripting frontend this will be backed
14 // by a SourceRange object
16  virtual ~SourceLocation() = default;
17  virtual void highlight(std::ostream& out) const = 0;
18 
19  std::string wrapException(
20  const std::exception& e,
21  const std::string& additional = "") {
22  std::stringstream msg;
23  msg << "\n" << e.what() << ":\n";
24  if (!additional.empty()) {
25  msg << additional << ":\n";
26  }
27  highlight(msg);
28  return msg.str();
29  }
30  void wrapAndRethrowException(
31  const std::exception& e,
32  const std::string& additional = "") {
33  throw std::runtime_error(wrapException(e, additional));
34  }
35 };
36 
37 inline std::ostream& operator<<(std::ostream& out, const SourceLocation& sl) {
38  sl.highlight(out);
39  return out;
40 }
41 
42 // normally a python stack trace
44  StringSourceLocation(std::string context) : context(std::move(context)) {}
45  void highlight(std::ostream& out) const override {
46  out << context;
47  }
48 
49  private:
50  std::string context;
51 };
52 
53 } // namespace jit
54 } // namespace torch
Represents a location in source code (for debugging).
Definition: StringUtil.h:69
Definition: jit_type.h:17