-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsole.cpp
More file actions
179 lines (155 loc) · 3.39 KB
/
Console.cpp
File metadata and controls
179 lines (155 loc) · 3.39 KB
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
#include "Global.h"
#include "Console.h"
#include "AST.h"
#include "Parser.h"
#ifdef WIN32
//saved console attributes, to be restored on exit
WORD user_attributes;
#endif WIN32
void RestoreConsoleAttributes()
{
#ifdef WIN32
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), user_attributes);
#elif !defined(MACVER)
printf("\x1b[0m");
#endif
}
void SaveConsoleAttributes()
{
#ifdef WIN32
CONSOLE_SCREEN_BUFFER_INFO csbiScreenBufferInfo;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbiScreenBufferInfo);
user_attributes = csbiScreenBufferInfo.wAttributes;
#endif
}
void SetConsoleAttributes(unsigned short attr)
{
if(no_colors) return;
#ifdef WIN32
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (WORD)attr);
#elif !defined(MACVER)
printf("\x1b[1;%dm", attr);
#endif
}
#define VPRINTF_ARGS(format) va_list args; va_start(args, format); vprintf(format, args); va_end(args);
void Trace(const char* format, ...) {
if (!quiet && verbose) {
SetConsoleAttributes(Console::DARKGRAY);
VPRINTF_ARGS(format);
printf("\n");
RestoreConsoleAttributes();
}
}
void Trace(const Node* node, const char* format, ...)
{
if (!quiet && verbose) {
SetConsoleAttributes(Console::DARKGRAY);
printf("%4d %s: ", node->SourceLoc.first_line, node->SourceLoc.file_name.c_str());
VPRINTF_ARGS(format);
printf("\n");
RestoreConsoleAttributes();
}
}
void Info(const char* format, ...)
{
if (!quiet) {
SetConsoleAttributes(Console::WHITE);
VPRINTF_ARGS(format);
printf("\n");
RestoreConsoleAttributes();
}
}
void Warn(const char* format, ...) {
if (!quiet) {
SetConsoleAttributes(Console::YELLOW);
printf("Warning: ");
VPRINTF_ARGS(format);
printf("\n");
RestoreConsoleAttributes();
}
}
void Warn(const Node* node, const char* format, ...)
{
if (!quiet) {
printf("%4d %s: ", node->SourceLoc.first_line, node->SourceLoc.file_name.c_str());
SetConsoleAttributes(Console::YELLOW);
printf("Warning: ");
VPRINTF_ARGS(format);
printf("\n");
RestoreConsoleAttributes();
}
}
void Error(const char* format, ...)
{
errors++;
if (!quiet) {
SetConsoleAttributes(Console::RED);
printf("Error: ");
VPRINTF_ARGS(format);
printf("\n");
RestoreConsoleAttributes();
}
}
void Error(const Node* node, const char* format, ...)
{
errors++;
if (!quiet) {
printf("%4d %s: ", node->SourceLoc.first_line, node->SourceLoc.file_name.c_str());
SetConsoleAttributes(Console::RED);
printf("Error: ");
VPRINTF_ARGS(format);
printf("\n");
RestoreConsoleAttributes();
}
}
void Fatal(const char* format, ...)
{
errors++;
if (!quiet) {
SetConsoleAttributes(Console::MAGENTA);
printf("Fatal: ");
VPRINTF_ARGS(format);
printf("\n");
RestoreConsoleAttributes();
}
}
void Fatal(const Node *node, const char* format, ...)
{
errors++;
if (!quiet) {
printf("%4d %s: ", node->SourceLoc.first_line, node->SourceLoc.file_name.c_str());
SetConsoleAttributes(Console::MAGENTA);
printf("Fatal: ");
VPRINTF_ARGS(format);
printf("\n");
RestoreConsoleAttributes();
}
}
void Print(const char* format, ...)
{
if (!quiet) {
VPRINTF_ARGS(format);
printf("\n");
}
}
#ifdef WIN32
#include <windows.h>
BOOL CtrlHandler(DWORD fdwCtrlType)
{
switch (fdwCtrlType)
{
case CTRL_C_EVENT:
return TRUE;
default:
return FALSE;
}
}
void AddConsoleCtrlHandler()
{
SetConsoleCtrlHandler((PHANDLER_ROUTINE)CtrlHandler, TRUE);
}
#else
void AddConsoleCtrlHandler ()
{
}
#endif WIN32