Dump AST execution state in debug mode.

Refs #5878
This commit is contained in:
Gunnar Beutner 2014-03-29 13:48:04 +01:00
parent 4428b8c6ee
commit 64aa5d1b09
5 changed files with 36 additions and 24 deletions

View File

@ -89,7 +89,7 @@ static bool LoadConfigFiles(const String& appType, ValidationType validate)
BOOST_FOREACH(const ConfigCompilerMessage& message, ConfigCompilerContext::GetInstance()->GetMessages()) { BOOST_FOREACH(const ConfigCompilerMessage& message, ConfigCompilerContext::GetInstance()->GetMessages()) {
std::ostringstream locbuf; std::ostringstream locbuf;
ShowCodeFragment(locbuf, message.Location); ShowCodeFragment(locbuf, message.Location, true);
String location = locbuf.str(); String location = locbuf.str();
String logmsg; String logmsg;

View File

@ -28,6 +28,7 @@
#include "base/utility.h" #include "base/utility.h"
#include "base/objectlock.h" #include "base/objectlock.h"
#include "base/object.h" #include "base/object.h"
#include "base/logger_fwd.h"
#include <boost/foreach.hpp> #include <boost/foreach.hpp>
#include <boost/exception_ptr.hpp> #include <boost/exception_ptr.hpp>
#include <boost/exception/errinfo_nested_exception.hpp> #include <boost/exception/errinfo_nested_exception.hpp>
@ -45,6 +46,14 @@ AExpression::AExpression(OpCallback op, const Value& operand1, const Value& oper
Value AExpression::Evaluate(const Dictionary::Ptr& locals) const Value AExpression::Evaluate(const Dictionary::Ptr& locals) const
{ {
try { try {
#ifdef _DEBUG
if (m_Operator != &AExpression::OpLiteral) {
std::ostringstream msgbuf;
ShowCodeFragment(msgbuf, m_DebugInfo, false);
Log(LogDebug, "config", "Executing:\n" + msgbuf.str());
}
#endif /* _DEBUG */
return m_Operator(this, locals); return m_Operator(this, locals);
} catch (const std::exception& ex) { } catch (const std::exception& ex) {
if (boost::get_error_info<boost::errinfo_nested_exception>(ex)) if (boost::get_error_info<boost::errinfo_nested_exception>(ex))

View File

@ -38,6 +38,6 @@ std::string icinga::to_string(const errinfo_debuginfo& e)
{ {
std::ostringstream msgbuf; std::ostringstream msgbuf;
msgbuf << "Config location: " << e.value() << "\n"; msgbuf << "Config location: " << e.value() << "\n";
ShowCodeFragment(msgbuf, e.value()); ShowCodeFragment(msgbuf, e.value(), true);
return msgbuf.str(); return msgbuf.str();
} }

View File

@ -53,7 +53,7 @@ DebugInfo icinga::DebugInfoRange(const DebugInfo& start, const DebugInfo& end)
#define EXTRA_LINES 2 #define EXTRA_LINES 2
void icinga::ShowCodeFragment(std::ostream& out, const DebugInfo& di) void icinga::ShowCodeFragment(std::ostream& out, const DebugInfo& di, bool verbose)
{ {
if (di.Path.IsEmpty()) if (di.Path.IsEmpty())
return; return;
@ -61,41 +61,44 @@ void icinga::ShowCodeFragment(std::ostream& out, const DebugInfo& di)
std::ifstream ifs; std::ifstream ifs;
ifs.open(di.Path.CStr(), std::ifstream::in); ifs.open(di.Path.CStr(), std::ifstream::in);
int lineno = 1; int lineno = 0;
char line[1024]; char line[1024];
while (ifs.good() && lineno <= di.LastLine + EXTRA_LINES) { while (ifs.good() && lineno <= di.LastLine + EXTRA_LINES) {
lineno++;
ifs.getline(line, sizeof(line)); ifs.getline(line, sizeof(line));
for (int i = 0; line[i]; i++) for (int i = 0; line[i]; i++)
if (line[i] == '\t') if (line[i] == '\t')
line[i] = ' '; line[i] = ' ';
if (lineno >= di.FirstLine - EXTRA_LINES && lineno <= di.LastLine + EXTRA_LINES) { int extra_lines = verbose ? EXTRA_LINES : 0;
String pathInfo = di.Path + "(" + Convert::ToString(lineno) + "): ";
out << pathInfo;
out << line << "\n";
if (lineno >= di.FirstLine && lineno <= di.LastLine) { if (lineno < di.FirstLine - extra_lines || lineno > di.LastLine + extra_lines)
int start, end; continue;
start = 0; String pathInfo = di.Path + "(" + Convert::ToString(lineno) + "): ";
end = strlen(line); out << pathInfo;
out << line << "\n";
if (lineno == di.FirstLine) if (lineno >= di.FirstLine && lineno <= di.LastLine) {
start = di.FirstColumn - 1; int start, end;
if (lineno == di.LastLine) start = 0;
end = di.LastColumn; end = strlen(line);
out << String(pathInfo.GetLength(), ' '); if (lineno == di.FirstLine)
out << String(start, ' '); start = di.FirstColumn - 1;
out << String(end - start, '^');
out << "\n"; if (lineno == di.LastLine)
} end = di.LastColumn;
out << String(pathInfo.GetLength(), ' ');
out << String(start, ' ');
out << String(end - start, '^');
out << "\n";
} }
lineno++;
} }
} }

View File

@ -64,7 +64,7 @@ I2_CONFIG_API std::ostream& operator<<(std::ostream& out, const DebugInfo& val);
I2_CONFIG_API DebugInfo DebugInfoRange(const DebugInfo& start, const DebugInfo& end); I2_CONFIG_API DebugInfo DebugInfoRange(const DebugInfo& start, const DebugInfo& end);
I2_CONFIG_API void ShowCodeFragment(std::ostream& out, const DebugInfo& di); I2_CONFIG_API void ShowCodeFragment(std::ostream& out, const DebugInfo& di, bool verbose);
} }