diff --git a/icinga-app/icinga.cpp b/icinga-app/icinga.cpp index e48935455..abb43672b 100644 --- a/icinga-app/icinga.cpp +++ b/icinga-app/icinga.cpp @@ -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; diff --git a/lib/config/aexpression.cpp b/lib/config/aexpression.cpp index 2533bd911..68cdc49b6 100644 --- a/lib/config/aexpression.cpp +++ b/lib/config/aexpression.cpp @@ -28,6 +28,7 @@ #include "base/utility.h" #include "base/objectlock.h" #include "base/object.h" +#include "base/logger_fwd.h" #include #include #include @@ -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(ex)) diff --git a/lib/config/configerror.cpp b/lib/config/configerror.cpp index 1f3c092f7..bae40acca 100644 --- a/lib/config/configerror.cpp +++ b/lib/config/configerror.cpp @@ -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(); } diff --git a/lib/config/debuginfo.cpp b/lib/config/debuginfo.cpp index ac809a625..9992a86ad 100644 --- a/lib/config/debuginfo.cpp +++ b/lib/config/debuginfo.cpp @@ -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,41 +61,44 @@ 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) { - String pathInfo = di.Path + "(" + Convert::ToString(lineno) + "): "; - out << pathInfo; - out << line << "\n"; + int extra_lines = verbose ? EXTRA_LINES : 0; - if (lineno >= di.FirstLine && lineno <= di.LastLine) { - int start, end; + if (lineno < di.FirstLine - extra_lines || lineno > di.LastLine + extra_lines) + continue; - start = 0; - end = strlen(line); + String pathInfo = di.Path + "(" + Convert::ToString(lineno) + "): "; + out << pathInfo; + out << line << "\n"; - if (lineno == di.FirstLine) - start = di.FirstColumn - 1; + if (lineno >= di.FirstLine && lineno <= di.LastLine) { + int start, end; - if (lineno == di.LastLine) - end = di.LastColumn; + start = 0; + end = strlen(line); - out << String(pathInfo.GetLength(), ' '); - out << String(start, ' '); - out << String(end - start, '^'); + if (lineno == di.FirstLine) + start = di.FirstColumn - 1; - out << "\n"; - } + if (lineno == di.LastLine) + end = di.LastColumn; + + out << String(pathInfo.GetLength(), ' '); + out << String(start, ' '); + out << String(end - start, '^'); + + out << "\n"; } - - lineno++; } } diff --git a/lib/config/debuginfo.h b/lib/config/debuginfo.h index 9620dee04..415f0bea7 100644 --- a/lib/config/debuginfo.h +++ b/lib/config/debuginfo.h @@ -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); }