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()) {
std::ostringstream locbuf;
ShowCodeFragment(locbuf, message.Location);
ShowCodeFragment(locbuf, message.Location, true);
String location = locbuf.str();
String logmsg;

View File

@ -28,6 +28,7 @@
#include "base/utility.h"
#include "base/objectlock.h"
#include "base/object.h"
#include "base/logger_fwd.h"
#include <boost/foreach.hpp>
#include <boost/exception_ptr.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
{
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);
} catch (const std::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;
msgbuf << "Config location: " << e.value() << "\n";
ShowCodeFragment(msgbuf, e.value());
ShowCodeFragment(msgbuf, e.value(), true);
return msgbuf.str();
}

View File

@ -53,7 +53,7 @@ DebugInfo icinga::DebugInfoRange(const DebugInfo& start, const DebugInfo& end)
#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())
return;
@ -61,17 +61,23 @@ void icinga::ShowCodeFragment(std::ostream& out, const DebugInfo& di)
std::ifstream ifs;
ifs.open(di.Path.CStr(), std::ifstream::in);
int lineno = 1;
int lineno = 0;
char line[1024];
while (ifs.good() && lineno <= di.LastLine + EXTRA_LINES) {
lineno++;
ifs.getline(line, sizeof(line));
for (int i = 0; line[i]; i++)
if (line[i] == '\t')
line[i] = ' ';
if (lineno >= di.FirstLine - EXTRA_LINES && lineno <= di.LastLine + EXTRA_LINES) {
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";
@ -95,7 +101,4 @@ void icinga::ShowCodeFragment(std::ostream& out, const DebugInfo& di)
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 void ShowCodeFragment(std::ostream& out, const DebugInfo& di);
I2_CONFIG_API void ShowCodeFragment(std::ostream& out, const DebugInfo& di, bool verbose);
}