blob: 99006aca51ba147870d12188961e6111da17c3db (
plain)
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
91
92
93
94
95
96
97
98
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
#include "base/debuginfo.hpp"
#include "base/convert.hpp"
#include <fstream>
using namespace icinga;
/**
* Outputs a DebugInfo struct to a stream.
*
* @param out The output stream.
* @param val The DebugInfo struct.
* @returns The output stream.
*/
std::ostream& icinga::operator<<(std::ostream& out, const DebugInfo& val)
{
out << "in " << val.Path << ": "
<< val.FirstLine << ":" << val.FirstColumn
<< "-"
<< val.LastLine << ":" << val.LastColumn;
return out;
}
DebugInfo icinga::DebugInfoRange(const DebugInfo& start, const DebugInfo& end)
{
DebugInfo result;
result.Path = start.Path;
result.FirstLine = start.FirstLine;
result.FirstColumn = start.FirstColumn;
result.LastLine = end.LastLine;
result.LastColumn = end.LastColumn;
return result;
}
#define EXTRA_LINES 2
void icinga::ShowCodeLocation(std::ostream& out, const DebugInfo& di, bool verbose)
{
if (di.Path.IsEmpty())
return;
out << "Location: " << di;
std::ifstream ifs;
ifs.open(di.Path.CStr(), std::ifstream::in);
int lineno = 0;
char line[1024];
while (ifs.good() && lineno <= di.LastLine + EXTRA_LINES) {
if (lineno == 0)
out << "\n";
lineno++;
ifs.getline(line, sizeof(line));
for (int i = 0; line[i]; i++)
if (line[i] == '\t')
line[i] = ' ';
int extra_lines = verbose ? EXTRA_LINES : 0;
if (lineno < di.FirstLine - extra_lines || lineno > di.LastLine + extra_lines)
continue;
String pathInfo = di.Path + "(" + Convert::ToString(lineno) + "): ";
out << pathInfo;
out << line << "\n";
if (lineno >= di.FirstLine && lineno <= di.LastLine) {
int start, end;
start = 0;
end = strlen(line);
if (lineno == di.FirstLine)
start = di.FirstColumn - 1;
if (lineno == di.LastLine)
end = di.LastColumn;
if (start < 0) {
end -= start;
start = 0;
}
out << String(pathInfo.GetLength(), ' ');
out << String(start, ' ');
out << String(end - start, '^');
out << "\n";
}
}
}
|