-
Notifications
You must be signed in to change notification settings - Fork 98
/
console.cpp
213 lines (184 loc) · 4.33 KB
/
console.cpp
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
/*
* 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
* 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/>.
*
* Additional permission under GNU GPL version 3 section 7
*
* If you modify this Program, or any covered work, by linking or combining
* it with OpenSSL (or a modified version of that library), containing parts
* covered by the terms of OpenSSL License and SSLeay License, the licensors
* of this Program grant you additional permission to convey the resulting work.
*
*/
#include "console.h"
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#ifdef _WIN32
#include <windows.h>
int get_key()
{
DWORD mode, rd;
HANDLE h;
if ((h = GetStdHandle(STD_INPUT_HANDLE)) == NULL)
return -1;
GetConsoleMode( h, &mode );
SetConsoleMode( h, mode & ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT) );
int c = 0;
ReadConsole( h, &c, 1, &rd, NULL );
SetConsoleMode( h, mode );
return c;
}
void set_colour(out_colours cl)
{
WORD attr = 0;
switch(cl)
{
case K_RED:
attr = FOREGROUND_RED | FOREGROUND_INTENSITY;
break;
case K_GREEN:
attr = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
break;
case K_BLUE:
attr = FOREGROUND_BLUE | FOREGROUND_INTENSITY;
break;
case K_YELLOW:
attr = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY;
break;
case K_CYAN:
attr = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY;
break;
case K_MAGENTA:
attr = FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY;
break;
case K_WHITE:
attr = FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY;
break;
default:
break;
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), attr);
}
void reset_colour()
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
}
#else
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
int get_key()
{
struct termios oldattr, newattr;
int ch;
tcgetattr( STDIN_FILENO, &oldattr );
newattr = oldattr;
newattr.c_lflag &= ~( ICANON | ECHO );
tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
ch = getchar();
tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
return ch;
}
void set_colour(out_colours cl)
{
switch(cl)
{
case K_RED:
fputs("\x1B[1;31m", stdout);
break;
case K_GREEN:
fputs("\x1B[1;32m", stdout);
break;
case K_BLUE:
fputs("\x1B[1;34m", stdout);
break;
case K_YELLOW:
fputs("\x1B[1;33m", stdout);
break;
case K_CYAN:
fputs("\x1B[1;36m", stdout);
break;
case K_MAGENTA:
fputs("\x1B[1;35m", stdout);
break;
case K_WHITE:
fputs("\x1B[1;37m", stdout);
break;
default:
break;
}
}
void reset_colour()
{
fputs("\x1B[0m", stdout);
}
#endif // _WIN32
inline void comp_localtime(const time_t* ctime, tm* stime)
{
#ifdef _WIN32
localtime_s(stime, ctime);
#else
localtime_r(ctime, stime);
#endif // __WIN32
}
printer* printer::oInst = nullptr;
printer::printer()
{
verbose_level = LINF;
logfile = nullptr;
}
bool printer::open_logfile(const char* file)
{
logfile = fopen(file, "ab+");
return logfile != nullptr;
}
void printer::print_msg(verbosity verbose, const char* fmt, ...)
{
if(verbose > verbose_level)
return;
char buf[1024];
size_t bpos;
tm stime;
time_t now = time(nullptr);
comp_localtime(&now, &stime);
strftime(buf, sizeof(buf), "[%F %T] : ", &stime);
bpos = strlen(buf);
va_list args;
va_start(args, fmt);
vsnprintf(buf+bpos, sizeof(buf)-bpos, fmt, args);
va_end(args);
bpos = strlen(buf);
if(bpos+2 >= sizeof(buf))
return;
buf[bpos] = '\n';
buf[bpos+1] = '\0';
std::unique_lock<std::mutex> lck(print_mutex);
fputs(buf, stdout);
if(logfile != nullptr)
{
fputs(buf, logfile);
fflush(logfile);
}
}
void printer::print_str(const char* str)
{
std::unique_lock<std::mutex> lck(print_mutex);
fputs(str, stdout);
if(logfile != nullptr)
{
fputs(str, logfile);
fflush(logfile);
}
}