Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: ce820474c388719006fdf4be9bb7ef6ef7d643a9 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
   sys/consoleinterface.cc
   This file is part of the Osirion project and is distributed under
   the terms of the GNU General Public License version 2
*/

#include <iomanip>
#include "sys/consoleinterface.h"

namespace sys
{

const size_t DEFAULT_LOGSIZE = 2048;

/* -- ANSI color code support -------------------------------------- */

bool con_ansicolor = false;

bool con_timestamps = false;

bool beginofline = true;

bool console_timestamps()
{
	return con_timestamps;
}

void set_console_timestamps(const bool timestamps)
{
	con_timestamps = timestamps;
}

bool ansi()
{
	return con_ansicolor;
}

void set_ansi(const bool ansi)
{
	con_ansicolor = ansi;
	if (con_ansicolor) {
		// ANSI default color
		std::cout << "\033[0;39m";
	}
}

void fallback_print_timestamp()
{
	int year = 0, month = 0, day = 0, hour = 0, min = 0, sec = 0;
	get_datetime(year, month, day, hour, min, sec);
	std::cout << year << "-" << month << "-" << day << " " << hour << ":" << min << ":" << sec << " ";
}

void fallback_print(const std::string &text)
{
	bool is_color_code = false;
	int ansi_bold = 0;
	int ansi_color = 39;

	const char *c = text.c_str();
	while (*c) {

		if ((*c) == '\n') {
			std::cout << std::endl;
			beginofline = true;

		} else if ((*c) == '^') {

			is_color_code = true;
			ansi_bold = 0;
			ansi_color = 39;

			switch (*(c + 1)) {
				case '0': // black
					ansi_color = 0;
					ansi_bold = 1;
					break;
				case '1': // red
					ansi_color = 31;
					break;
				case '2': // green
					ansi_color = 32;
					break;
				case '3': // yellow
					ansi_color = 1;
					ansi_color = 33;
					break;
				case '4': // blue
					ansi_color = 34;
					break;
				case '5': // cyan
					ansi_color = 36;
					break;
				case '6': // magenta
					ansi_color = 35;
					break;
				case '7': // white is mapped to foreground color
					ansi_bold = 1;
					ansi_color = 39;
					break;

				case 'N': // normal
					ansi_bold = 0;
					ansi_color = 39;
					break;
				case 'B': // bold
					ansi_bold = 1;
					ansi_color = 39;
					break;
				case 'D': // debug
					ansi_bold = 0;
					ansi_color = 39;
					break;
				case 'R': // error
					ansi_bold = 0;
					ansi_color = 31;
					break;
				case 'W': // warning
					ansi_bold = 1;
					ansi_color = 33;
					break;
				case 'F': // fancy
					ansi_bold = 0;
					ansi_color = 32;
					break;
				default:
					is_color_code = false;
			}

			if (is_color_code) {
				if (con_ansicolor)
					std::cout <<  "\033[" << ansi_bold << ";" << ansi_color << "m";
				c++;
			} else {
				if (beginofline && con_timestamps) {
					fallback_print_timestamp();
				}
				std::cout << *c;
				beginofline = false;
			}

		} else {
			if (beginofline && con_timestamps) {
				fallback_print_timestamp();
			}			
			std::cout << *c;
			beginofline = false;
		}

		c++;
	}
}

/* -- ConsoleBuffer ------------------------------------------------ */

int ConsoleBuffer::overflow(int c)
{
	if (c == Traits::eof())
		return Traits::not_eof(c);

	if (c == '\n') {	
		if (ConsoleInterface::instance()) {
			/*
			 * In the rcon case, the remote console will handle the timestamps
			 */
			if (con_timestamps && !ConsoleInterface::instance()->rcon()) {
				std::ostringstream str;
				int year = 0, month = 0, day = 0, hour = 0, min = 0, sec = 0;
				get_datetime(year, month, day, hour, min, sec);
				
				str << "^B"
					<< std::setw(4) << std::setfill('0') << year << "-" 
					<< std::setw(2) << std::setfill('0') << month << "-" 
					<< std::setw(2) << std::setfill('0') << day << " " 
					<< std::setw(2) << hour << ":" 
					<< std::setw(2) << min << ":" 
					<< std::setw(2) << sec << " " 
					<< std::setw(2) << con_buffer;
					
				ConsoleInterface::instance()->event_text(str.str());
			} else {
				ConsoleInterface::instance()->event_text(con_buffer);
			}
		} else {
			fallback_print(con_buffer);
			std::cout << std::endl;
		}
		con_buffer.clear();
	} else {
		con_buffer += c;
	}
	return c;
}

/* -- ConsoleStream ------------------------------------------------ */

ConsoleStream con_out;

ConsoleStream::ConsoleStream() : std::basic_ostream<char, Traits>(&con_buffer)
{
	clear();
}

ConsoleStream::~ConsoleStream()
{
	if (con_ansicolor) {
		// ANSI default color
		std::cout << "\033[0;39m" << std::endl;
	}
}

/* -- Console ------------------------------------------------------ */

ConsoleInterface *ConsoleInterface::consoleinterface_instance = 0;

ConsoleInterface::ConsoleInterface()
{
	if (consoleinterface_instance) {
		std::cerr << "multiple sys::ConsoleInterface instances!" << std::endl;
		sys::quit(2);
	}

	consoleinterface_rcon = false;
	consoleinterface_instance = this;
	consoleinterface_logsize = DEFAULT_LOGSIZE;
}

ConsoleInterface::~ConsoleInterface()
{
	consoleinterface_instance = 0;
}

ConsoleInterface *ConsoleInterface::instance()
{
	return consoleinterface_instance;
}


void ConsoleInterface::set_rcon(bool enable)
{
	consoleinterface_rcon = enable;
}

void ConsoleInterface::set_logsize(const size_t logsize)
{
	consoleinterface_logsize = logsize < 16 ? 16 : logsize;
}

void ConsoleInterface::event_text(const std::string & text)
{
	if (rcon()) {
		consoleinterface_rconbuf.push_back(text);
	} else {
		while (consoleinterface_log.size() >= consoleinterface_logsize) {
			consoleinterface_log.pop_front();
		}

		consoleinterface_log.push_back(text);
		print(text);
	}
}

void ConsoleInterface::print(const std::string & text)
{
	fallback_print(text);
	std::cout << std::endl;
}

} // namespace sys