1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "base/debuginfo.hpp"
4 #include "base/convert.hpp"
7 using namespace icinga;
10 * Outputs a DebugInfo struct to a stream.
12 * @param out The output stream.
13 * @param val The DebugInfo struct.
14 * @returns The output stream.
16 std::ostream& icinga::operator<<(std::ostream& out, const DebugInfo& val)
18 out << "in " << val.Path << ": "
19 << val.FirstLine << ":" << val.FirstColumn
21 << val.LastLine << ":" << val.LastColumn;
26 DebugInfo icinga::DebugInfoRange(const DebugInfo& start, const DebugInfo& end)
29 result.Path = start.Path;
30 result.FirstLine = start.FirstLine;
31 result.FirstColumn = start.FirstColumn;
32 result.LastLine = end.LastLine;
33 result.LastColumn = end.LastColumn;
39 void icinga::ShowCodeLocation(std::ostream& out, const DebugInfo& di, bool verbose)
41 if (di.Path.IsEmpty())
44 out << "Location: " << di;
47 ifs.open(di.Path.CStr(), std::ifstream::in);
52 while (ifs.good() && lineno <= di.LastLine + EXTRA_LINES) {
58 ifs.getline(line, sizeof(line));
60 for (int i = 0; line[i]; i++)
64 int extra_lines = verbose ? EXTRA_LINES : 0;
66 if (lineno < di.FirstLine - extra_lines || lineno > di.LastLine + extra_lines)
69 String pathInfo = di.Path + "(" + Convert::ToString(lineno) + "): ";
73 if (lineno >= di.FirstLine && lineno <= di.LastLine) {
79 if (lineno == di.FirstLine)
80 start = di.FirstColumn - 1;
82 if (lineno == di.LastLine)
90 out << String(pathInfo.GetLength(), ' ');
91 out << String(start, ' ');
92 out << String(end - start, '^');