Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: a7fde2eb55f4bfb3f1f1d77a1723d57f0e3c5e27 (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
/*
   sys/consoleinterface.h
   This file is part of the Osirion project and is distributed under
   the terms of the GNU General Public License version 2.
*/

#ifndef __INCLUDED_SYS_CONSOLEINTERFACE_H__
#define __INCLUDED_SYS_CONSOLEINTERFACE_H__

#include <iostream>
#include <string>
#include <streambuf>
#include <ostream>
#include <sstream>
#include <deque>

#include "sys/sys.h"


namespace sys
{

/// true if ANSI colors are enabled
bool ansi();

/// enable or disable ANSI colors
void set_ansi(bool enable = true);

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

typedef std::char_traits<char> Traits;

/// buffer back-end for console output
class ConsoleBuffer : public std::basic_streambuf<char, Traits >
{
public:
	const std::string & str() const {
		return con_buffer;
	}

protected:
	/// stream overflow
	virtual int overflow(int c = Traits::eof());

private:
	std::string		con_buffer;
};


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

/// provides global streams for console output
class ConsoleStream : public std::basic_ostream<char, Traits >
{
public:
	ConsoleStream();
	~ConsoleStream();

	inline ConsoleBuffer & buf() {
		return con_buffer;
	}

private:
	ConsoleBuffer		con_buffer;
};

/// global console output stream
extern ConsoleStream		con_out;


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


/// interface for the client and server Console classes
class ConsoleInterface
{
public:
	/// default constructor
	ConsoleInterface();

	/// default destructor
	virtual ~ConsoleInterface();

	/// set maximal number of lines in the log
	void set_logsize(const size_t logsize);

	/// return rcon state
	inline bool rcon() {
		return consoleinterface_rcon;
	}

	/// enable or disable rcon
	void set_rcon(bool enable = true);

	typedef std::deque<std::string> Queue;

	inline Queue & rconbuf() {
		return consoleinterface_rconbuf;
	}

	inline Queue & log() {
		return consoleinterface_log;
	}

	/// a pointer to the current console instance
	static ConsoleInterface *instance();

	/// incoming text event handler
	void event_text(const std::string & text);

	/// ncurses resize event
	virtual void resize() {};

protected:
	/// print one line of text
	virtual void print(const std::string & text);

private:
	/// console singleton
	static ConsoleInterface *consoleinterface_instance;

	bool			consoleinterface_rcon;
	Queue			consoleinterface_rconbuf;

	Queue			consoleinterface_log;
	size_t			consoleinterface_logsize;
};

} // namespace sys

#define con_print sys::con_out << "^N"
#define con_warn sys::con_out << "^W"
#define con_error sys::con_out << "^R"
#ifdef HAVE_DEBUG_MESSAGES
#define con_debug sys::con_out << "^D"
#else
#define con_debug if (0) *(std::ostream*)(0)
#endif


#endif // __INCLUDED_SYS_CONSOLEINTERFACE_H__