summaryrefslogtreecommitdiff
path: root/src/core/frontend/TerminalLogger.cpp
blob: 3bead06418d910438f114ff6ffe50d0ee3afc1c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
    Ousía
    Copyright (C) 2014, 2015  Benjamin Paaßen, Andreas Stöckel

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <core/common/Utils.hpp>

#include "Terminal.hpp"
#include "TerminalLogger.hpp"

namespace ousia {

/* Class TerminalLogger */

void TerminalLogger::processMessage(const Message &msg)
{
	Terminal t(useColor);

	// Fetch filename, position and context
	const SourceContext ctx = messageContext(msg);

	// Print the file name
	if (ctx.hasFile()) {
		os << t.bright() << ctx.filename << t.reset();
	}

	// Print line and column number
	if (ctx.hasLine()) {
		if (ctx.hasFile()) {
			os << ':';
		}
		os << t.bright() << ctx.startLine << t.reset();
		if (ctx.hasColumn()) {
			os << ':' << ctx.startColumn;
		}
	}

	// Print the optional seperator
	if (ctx.hasFile() || ctx.hasLine()) {
		os << ": ";
	}

	// Print the severity
	switch (msg.severity) {
		case Severity::DEBUG:
			break;
		case Severity::NOTE:
			os << t.color(Terminal::CYAN, true) << "note: ";
			break;
		case Severity::WARNING:
			os << t.color(Terminal::MAGENTA, true) << "warning: ";
			break;
		case Severity::ERROR:
			os << t.color(Terminal::RED, true) << "error: ";
			break;
		case Severity::FATAL_ERROR:
			os << t.color(Terminal::RED, true) << "fatal error: ";
			break;
	}
	os << t.reset();

	// Print the actual message
	os << msg.msg << std::endl;

	// Print the error message context if available
	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 << t.italic() << "[...] " << t.reset();
			}
			if (!firstLine) {
				os << "\t";
			}

			// Print the actual line, replace tabs
			for (char c: lines[n]) {
				if (c == '\t') {
					os << "    ";
				} else {
					os << c;
				}
			}
			if (!lastLine) {
				os << t.color(Terminal::BLACK) << "¶" << t.reset();
			}

			// Indicate truncation
			if (ctx.truncatedEnd && lastLine) {
				os << t.italic() << " [...]" << t.reset();
			}
			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();
			bool inRegion = false;
			for (size_t i = lstart; i < lend + 1; i++) {
				if (i >= pstart && i < pend) {
					if (!inRegion) {
						os << t.color(Terminal::GREEN);
						inRegion = true;
					}
				} else {
					if (inRegion) {
						os << t.reset();
						inRegion = false;
					}
				}
				char c = i < ctx.text.size() ? ctx.text[i] : ' ';
				if (c == '\t') {
					if (inRegion) {
						if (relLen == 1) {
							os << "^   ";
						} else {
							os << "~~~~";
						}
					} else {
						os << "    ";
					}
				} else {
					if (inRegion) {
						if (relLen == 1) {
							os << "^";
						} else {
							os << "~";
						}
					} else {
						os << " ";
					}
				}
			}
			if (inRegion) {
				os << t.reset();
			}
			os << std::endl;

			lstart = lend + 1; // skip newline
		}
	}
}

}