1 /******************************************************************************
3 * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "base/debuginfo.hpp"
21 #include "base/convert.hpp"
24 using namespace icinga;
26 DebugInfo::DebugInfo(void)
27 : FirstLine(0), FirstColumn(0), LastLine(0), LastColumn(0)
31 * Outputs a DebugInfo struct to a stream.
33 * @param out The output stream.
34 * @param val The DebugInfo struct.
35 * @returns The output stream.
37 std::ostream& icinga::operator<<(std::ostream& out, const DebugInfo& val)
39 out << "in " << val.Path << ": "
40 << val.FirstLine << ":" << val.FirstColumn
42 << val.LastLine << ":" << val.LastColumn;
47 DebugInfo icinga::DebugInfoRange(const DebugInfo& start, const DebugInfo& end)
50 result.Path = start.Path;
51 result.FirstLine = start.FirstLine;
52 result.FirstColumn = start.FirstColumn;
53 result.LastLine = end.LastLine;
54 result.LastColumn = end.LastColumn;
60 void icinga::ShowCodeLocation(std::ostream& out, const DebugInfo& di, bool verbose)
62 if (di.Path.IsEmpty())
65 out << "Location: " << di << "\n";
68 ifs.open(di.Path.CStr(), std::ifstream::in);
73 while (ifs.good() && lineno <= di.LastLine + EXTRA_LINES) {
76 ifs.getline(line, sizeof(line));
78 for (int i = 0; line[i]; i++)
82 int extra_lines = verbose ? EXTRA_LINES : 0;
84 if (lineno < di.FirstLine - extra_lines || lineno > di.LastLine + extra_lines)
87 String pathInfo = di.Path + "(" + Convert::ToString(lineno) + "): ";
91 if (lineno >= di.FirstLine && lineno <= di.LastLine) {
97 if (lineno == di.FirstLine)
98 start = di.FirstColumn - 1;
100 if (lineno == di.LastLine)
108 out << String(pathInfo.GetLength(), ' ');
109 out << String(start, ' ');
110 out << String(end - start, '^');