| 38 | #define EXTRA_LINES 2 |
| 39 | |
| 40 | void icinga::ShowCodeLocation(std::ostream& out, const DebugInfo& di, bool verbose) |
| 41 | { |
| 42 | if (di.Path.IsEmpty()) |
| 43 | return; |
| 44 | |
| 45 | out << "Location: " << di; |
| 46 | |
| 47 | std::ifstream ifs; |
| 48 | ifs.open(di.Path.CStr(), std::ifstream::in); |
| 49 | |
| 50 | int lineno = 0; |
| 51 | char line[1024]; |
| 52 | |
| 53 | while (ifs.good() && lineno <= di.LastLine + EXTRA_LINES) { |
| 54 | if (lineno == 0) |
| 55 | out << "\n"; |
| 56 | |
| 57 | lineno++; |
| 58 | |
| 59 | ifs.getline(line, sizeof(line)); |
| 60 | |
| 61 | for (int i = 0; line[i]; i++) |
| 62 | if (line[i] == '\t') |
| 63 | line[i] = ' '; |
| 64 | |
| 65 | int extra_lines = verbose ? EXTRA_LINES : 0; |
| 66 | |
| 67 | if (lineno < di.FirstLine - extra_lines || lineno > di.LastLine + extra_lines) |
| 68 | continue; |
| 69 | |
| 70 | String pathInfo = di.Path + "(" + Convert::ToString(lineno) + "): "; |
| 71 | out << pathInfo; |
| 72 | out << line << "\n"; |
| 73 | |
| 74 | if (lineno >= di.FirstLine && lineno <= di.LastLine) { |
| 75 | int start, end; |
| 76 | |
| 77 | start = 0; |
| 78 | end = strlen(line); |
| 79 | |
| 80 | if (lineno == di.FirstLine) |
| 81 | start = di.FirstColumn - 1; |
| 82 | |
| 83 | if (lineno == di.LastLine && di.LastColumn > 0) { |
| 84 | end = di.LastColumn; |
| 85 | } |
| 86 | |
| 87 | if (start < 0) { |
| 88 | end -= start; |
| 89 | start = 0; |
| 90 | } |
| 91 | |
| 92 | out << String(pathInfo.GetLength(), ' '); |
| 93 | out << String(start, ' '); |
| 94 | out << String(end - start, '^'); |
| 95 | |
| 96 | out << "\n"; |
| 97 | } |