2014-03-21 15:39:25 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2015-01-22 12:00:23 +01:00
|
|
|
* Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
|
2014-03-21 15:39:25 +01:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2014-09-09 14:49:21 +02:00
|
|
|
#include "base/debuginfo.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/convert.hpp"
|
2014-03-21 15:39:25 +01:00
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2014-12-18 12:58:46 +01:00
|
|
|
DebugInfo::DebugInfo(void)
|
|
|
|
: FirstLine(0), FirstColumn(0), LastLine(0), LastColumn(0)
|
|
|
|
{ }
|
|
|
|
|
2014-03-21 15:39:25 +01:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2014-03-21 15:51:55 +01:00
|
|
|
#define EXTRA_LINES 2
|
|
|
|
|
2014-03-29 13:48:04 +01:00
|
|
|
void icinga::ShowCodeFragment(std::ostream& out, const DebugInfo& di, bool verbose)
|
2014-03-21 15:39:25 +01:00
|
|
|
{
|
|
|
|
if (di.Path.IsEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::ifstream ifs;
|
|
|
|
ifs.open(di.Path.CStr(), std::ifstream::in);
|
|
|
|
|
2014-03-29 13:48:04 +01:00
|
|
|
int lineno = 0;
|
2014-03-21 15:39:25 +01:00
|
|
|
char line[1024];
|
|
|
|
|
2014-03-21 15:51:55 +01:00
|
|
|
while (ifs.good() && lineno <= di.LastLine + EXTRA_LINES) {
|
2014-03-29 13:48:04 +01:00
|
|
|
lineno++;
|
|
|
|
|
2014-03-21 15:39:25 +01:00
|
|
|
ifs.getline(line, sizeof(line));
|
|
|
|
|
|
|
|
for (int i = 0; line[i]; i++)
|
|
|
|
if (line[i] == '\t')
|
|
|
|
line[i] = ' ';
|
|
|
|
|
2014-03-29 13:48:04 +01:00
|
|
|
int extra_lines = verbose ? EXTRA_LINES : 0;
|
2014-03-21 15:39:25 +01:00
|
|
|
|
2014-03-29 13:48:04 +01:00
|
|
|
if (lineno < di.FirstLine - extra_lines || lineno > di.LastLine + extra_lines)
|
|
|
|
continue;
|
2014-03-21 15:39:25 +01:00
|
|
|
|
2014-03-29 13:48:04 +01:00
|
|
|
String pathInfo = di.Path + "(" + Convert::ToString(lineno) + "): ";
|
|
|
|
out << pathInfo;
|
|
|
|
out << line << "\n";
|
2014-03-21 15:39:25 +01:00
|
|
|
|
2014-03-29 13:48:04 +01:00
|
|
|
if (lineno >= di.FirstLine && lineno <= di.LastLine) {
|
|
|
|
int start, end;
|
2014-03-21 15:39:25 +01:00
|
|
|
|
2014-03-29 13:48:04 +01:00
|
|
|
start = 0;
|
|
|
|
end = strlen(line);
|
2014-03-21 15:39:25 +01:00
|
|
|
|
2014-03-29 13:48:04 +01:00
|
|
|
if (lineno == di.FirstLine)
|
|
|
|
start = di.FirstColumn - 1;
|
2014-03-21 15:39:25 +01:00
|
|
|
|
2014-03-29 13:48:04 +01:00
|
|
|
if (lineno == di.LastLine)
|
|
|
|
end = di.LastColumn;
|
2014-03-21 15:39:25 +01:00
|
|
|
|
2014-03-31 12:44:18 +02:00
|
|
|
if (start < 0) {
|
|
|
|
end -= start;
|
|
|
|
start = 0;
|
|
|
|
}
|
|
|
|
|
2014-03-29 13:48:04 +01:00
|
|
|
out << String(pathInfo.GetLength(), ' ');
|
|
|
|
out << String(start, ' ');
|
|
|
|
out << String(end - start, '^');
|
|
|
|
|
|
|
|
out << "\n";
|
|
|
|
}
|
2014-03-21 15:39:25 +01:00
|
|
|
}
|
|
|
|
}
|
2014-09-09 14:49:21 +02:00
|
|
|
|