diff options
author | Andreas Stöckel <astoecke@techfak.uni-bielefeld.de> | 2015-01-24 03:07:30 +0100 |
---|---|---|
committer | Andreas Stöckel <astoecke@techfak.uni-bielefeld.de> | 2015-01-24 03:07:30 +0100 |
commit | f819b42057b2baea205569dd808c4fcf2bc4d630 (patch) | |
tree | 2e67f84a276c367e1df688a2ae0fa18d9a340da3 /src/core | |
parent | cbac51bb9e56ebe73ac3768eb9610f036419f1c2 (diff) |
Added support for new SourceContext to TerminalLogger
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/common/Logger.cpp | 94 |
1 files changed, 70 insertions, 24 deletions
diff --git a/src/core/common/Logger.cpp b/src/core/common/Logger.cpp index 034953d..aa61e6a 100644 --- a/src/core/common/Logger.cpp +++ b/src/core/common/Logger.cpp @@ -21,6 +21,7 @@ #include "Logger.hpp" #include "Terminal.hpp" +#include "Utils.hpp" namespace ousia { @@ -317,30 +318,75 @@ void TerminalLogger::processMessage(const Message &msg) os << msg.msg << std::endl; // Print the error message context if available - /* if (ctx.valid()) { - size_t relPos = ctx.relPos; - if (ctx.truncatedStart) { - os << "[...] "; - } - os << ctx.text; - if (ctx.truncatedEnd) { - os << " [...]"; - } - os << std::endl; - - if (ctx.truncatedStart) { - os << " "; - } - - for (size_t i = 0; i < relPos; i++) { - if (i < ctx.text.size() && ctx.text[i] == '\t') { - os << '\t'; - } else { - os << ' '; - } - } - os << t.color(Terminal::GREEN) << '^' << t.reset() << std::endl; - }*/ + if (ctx.hasText()) { + // Iterate over each line in the text + std::vector<std::string> lines = Utils::split(ctx.text, '\n'); + + const size_t relLen = ctx.relLen ? ctx.relLen : 1; + const size_t relPos = ctx.relPos; + const size_t pstart = relPos; + const size_t pend = relPos + relLen; + + size_t lstart = 0; + size_t lend = 0; + for (size_t n = 0; n < lines.size(); n++) { + bool firstLine = n == 0; + bool lastLine = n == lines.size() - 1; + + // Indicate truncation and indent non-first lines + if (ctx.truncatedStart && firstLine) { + os << "[...] "; + } + if (!firstLine) { + os << "\t"; + } + + // Print the actual line + os << lines[n]; + + // Indicate truncation + if (ctx.truncatedEnd && lastLine) { + os << " [...]"; + } + os << std::endl; + + // Repeat truncation or indendation space in the next line + if (ctx.truncatedStart && firstLine) { + os << " "; + } + if (!firstLine) { + os << "\t"; + } + + // Print the position indicators + lend = lastLine ? pend : lstart + lines[n].size(); + for (size_t i = lstart; i < lend; i++) { + if (i >= pstart && i < pend) { + os << t.color(Terminal::GREEN); + for (; i < std::min(lend, pend); i++) { + if (relLen == 1) { + os << '^'; + } else { + os << '~'; + } + if (i < ctx.text.size() && ctx.text[i] == '\t') { + os << '\t'; + } + } + os << t.reset(); + } else { + if (i < ctx.text.size() && ctx.text[i] == '\t') { + os << '\t'; + } else { + os << ' '; + } + } + } + os << std::endl; + + lstart = lend; + } + } } } |