Skip to content

Commit 9c7f63c

Browse files
committed
Allow Log as out for ShowCodeLocation()
refs #7827
1 parent cfd9b80 commit 9c7f63c

File tree

2 files changed

+62
-66
lines changed

2 files changed

+62
-66
lines changed

lib/base/debuginfo.cpp

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
22

33
#include "base/debuginfo.hpp"
4-
#include "base/convert.hpp"
5-
#include <fstream>
64

75
using namespace icinga;
86

@@ -33,66 +31,3 @@ DebugInfo icinga::DebugInfoRange(const DebugInfo& start, const DebugInfo& end)
3331
result.LastColumn = end.LastColumn;
3432
return result;
3533
}
36-
37-
#define EXTRA_LINES 2
38-
39-
void icinga::ShowCodeLocation(std::ostream& out, const DebugInfo& di, bool verbose)
40-
{
41-
if (di.Path.IsEmpty())
42-
return;
43-
44-
out << "Location: " << di;
45-
46-
std::ifstream ifs;
47-
ifs.open(di.Path.CStr(), std::ifstream::in);
48-
49-
int lineno = 0;
50-
char line[1024];
51-
52-
while (ifs.good() && lineno <= di.LastLine + EXTRA_LINES) {
53-
if (lineno == 0)
54-
out << "\n";
55-
56-
lineno++;
57-
58-
ifs.getline(line, sizeof(line));
59-
60-
for (int i = 0; line[i]; i++)
61-
if (line[i] == '\t')
62-
line[i] = ' ';
63-
64-
int extra_lines = verbose ? EXTRA_LINES : 0;
65-
66-
if (lineno < di.FirstLine - extra_lines || lineno > di.LastLine + extra_lines)
67-
continue;
68-
69-
String pathInfo = di.Path + "(" + Convert::ToString(lineno) + "): ";
70-
out << pathInfo;
71-
out << line << "\n";
72-
73-
if (lineno >= di.FirstLine && lineno <= di.LastLine) {
74-
int start, end;
75-
76-
start = 0;
77-
end = strlen(line);
78-
79-
if (lineno == di.FirstLine)
80-
start = di.FirstColumn - 1;
81-
82-
if (lineno == di.LastLine)
83-
end = di.LastColumn;
84-
85-
if (start < 0) {
86-
end -= start;
87-
start = 0;
88-
}
89-
90-
out << String(pathInfo.GetLength(), ' ');
91-
out << String(start, ' ');
92-
out << String(end - start, '^');
93-
94-
out << "\n";
95-
}
96-
}
97-
}
98-

lib/base/debuginfo.hpp

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
#ifndef DEBUGINFO_H
44
#define DEBUGINFO_H
55

6+
#include "base/convert.hpp"
67
#include "base/i2-base.hpp"
78
#include "base/string.hpp"
9+
#include <fstream>
810

911
namespace icinga
1012
{
@@ -29,7 +31,66 @@ std::ostream& operator<<(std::ostream& out, const DebugInfo& val);
2931

3032
DebugInfo DebugInfoRange(const DebugInfo& start, const DebugInfo& end);
3133

32-
void ShowCodeLocation(std::ostream& out, const DebugInfo& di, bool verbose = true);
34+
template<class T, unsigned long ExtraLines = 2>
35+
void ShowCodeLocation(T& out, const DebugInfo& di, bool verbose = true)
36+
{
37+
if (di.Path.IsEmpty())
38+
return;
39+
40+
out << "Location: " << di;
41+
42+
std::ifstream ifs;
43+
ifs.open(di.Path.CStr(), std::ifstream::in);
44+
45+
int lineno = 0;
46+
char line[1024];
47+
48+
while (ifs.good() && lineno <= di.LastLine + ExtraLines) {
49+
if (lineno == 0)
50+
out << "\n";
51+
52+
lineno++;
53+
54+
ifs.getline(line, sizeof(line));
55+
56+
for (int i = 0; line[i]; i++)
57+
if (line[i] == '\t')
58+
line[i] = ' ';
59+
60+
int extra_lines = verbose ? ExtraLines : 0;
61+
62+
if (lineno < di.FirstLine - extra_lines || lineno > di.LastLine + extra_lines)
63+
continue;
64+
65+
String pathInfo = di.Path + "(" + Convert::ToString(lineno) + "): ";
66+
out << pathInfo;
67+
out << line << "\n";
68+
69+
if (lineno >= di.FirstLine && lineno <= di.LastLine) {
70+
int start, end;
71+
72+
start = 0;
73+
end = strlen(line);
74+
75+
if (lineno == di.FirstLine)
76+
start = di.FirstColumn - 1;
77+
78+
if (lineno == di.LastLine)
79+
end = di.LastColumn;
80+
81+
if (start < 0) {
82+
end -= start;
83+
start = 0;
84+
}
85+
86+
out << String(pathInfo.GetLength(), ' ');
87+
out << String(start, ' ');
88+
out << String(end - start, '^');
89+
90+
out << "\n";
91+
}
92+
}
93+
}
3394

3495
}
3596

0 commit comments

Comments
 (0)