aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/common/console_listener.cpp
blob: d7f27c358ad307f146f8c31b6cfb0f127aaa7fc9 (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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Copyright 2013 Dolphin Emulator Project
// Licensed under GPLv2
// Refer to the license.txt file included.

#include <algorithm>

#ifdef _WIN32
#include <windows.h>
#include <array>
#endif

#include "common/common.h"
#include "common/log_manager.h" // Common
#include "common/console_listener.h" // Common

ConsoleListener::ConsoleListener()
{
#ifdef _WIN32
    hConsole = NULL;
    bUseColor = true;
#else
    bUseColor = isatty(fileno(stdout));
#endif
}

ConsoleListener::~ConsoleListener()
{
    Close();
}

// 100, 100, "Dolphin Log Console"
// Open console window - width and height is the size of console window
// Name is the window title
void ConsoleListener::Open(bool Hidden, int Width, int Height, const char *Title)
{
#ifdef _WIN32
    if (!GetConsoleWindow())
    {
        // Open the console window and create the window handle for GetStdHandle()
        AllocConsole();
        // Hide
        if (Hidden) ShowWindow(GetConsoleWindow(), SW_HIDE);
        // Save the window handle that AllocConsole() created
        hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
        // Set the console window title
        SetConsoleTitle(Common::UTF8ToTStr(Title).c_str());
        // Set letter space
        LetterSpace(80, 4000);
        //MoveWindow(GetConsoleWindow(), 200,200, 800,800, true);
    }
    else
    {
        hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    }
#endif
}

void ConsoleListener::UpdateHandle()
{
#ifdef _WIN32
    hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
#endif
}

// Close the console window and close the eventual file handle
void ConsoleListener::Close()
{
#ifdef _WIN32
    if (hConsole == NULL)
        return;
    FreeConsole();
    hConsole = NULL;
#else
    fflush(NULL);
#endif
}

bool ConsoleListener::IsOpen()
{
#ifdef _WIN32
    return (hConsole != NULL);
#else
    return true;
#endif
}

/*
  LetterSpace: SetConsoleScreenBufferSize and SetConsoleWindowInfo are
    dependent on each other, that's the reason for the additional checks.
*/
void ConsoleListener::BufferWidthHeight(int BufferWidth, int BufferHeight, int ScreenWidth, int ScreenHeight, bool BufferFirst)
{
#ifdef _WIN32
    BOOL SB, SW;
    if (BufferFirst)
    {
        // Change screen buffer size
        COORD Co = {BufferWidth, BufferHeight};
        SB = SetConsoleScreenBufferSize(hConsole, Co);
        // Change the screen buffer window size
        SMALL_RECT coo = {0,0,ScreenWidth, ScreenHeight}; // top, left, right, bottom
        SW = SetConsoleWindowInfo(hConsole, TRUE, &coo);
    }
    else
    {
        // Change the screen buffer window size
        SMALL_RECT coo = {0,0, ScreenWidth, ScreenHeight}; // top, left, right, bottom
        SW = SetConsoleWindowInfo(hConsole, TRUE, &coo);
        // Change screen buffer size
        COORD Co = {BufferWidth, BufferHeight};
        SB = SetConsoleScreenBufferSize(hConsole, Co);
    }
#endif
}
void ConsoleListener::LetterSpace(int Width, int Height)
{
#ifdef _WIN32
    // Get console info
    CONSOLE_SCREEN_BUFFER_INFO ConInfo;
    GetConsoleScreenBufferInfo(hConsole, &ConInfo);

    //
    int OldBufferWidth = ConInfo.dwSize.X;
    int OldBufferHeight = ConInfo.dwSize.Y;
    int OldScreenWidth = (ConInfo.srWindow.Right - ConInfo.srWindow.Left);
    int OldScreenHeight = (ConInfo.srWindow.Bottom - ConInfo.srWindow.Top);
    //
    int NewBufferWidth = Width;
    int NewBufferHeight = Height;
    int NewScreenWidth = NewBufferWidth - 1;
    int NewScreenHeight = OldScreenHeight;

    // Width
    BufferWidthHeight(NewBufferWidth, OldBufferHeight, NewScreenWidth, OldScreenHeight, (NewBufferWidth > OldScreenWidth-1));
    // Height
    BufferWidthHeight(NewBufferWidth, NewBufferHeight, NewScreenWidth, NewScreenHeight, (NewBufferHeight > OldScreenHeight-1));

    // Resize the window too
    //MoveWindow(GetConsoleWindow(), 200,200, (Width*8 + 50),(NewScreenHeight*12 + 200), true);
#endif
}
#ifdef _WIN32
COORD ConsoleListener::GetCoordinates(int BytesRead, int BufferWidth)
{
    COORD Ret = {0, 0};
    // Full rows
    int Step = (int)floor((float)BytesRead / (float)BufferWidth);
    Ret.Y += Step;
    // Partial row
    Ret.X = BytesRead - (BufferWidth * Step);
    return Ret;
}
#endif
void ConsoleListener::PixelSpace(int Left, int Top, int Width, int Height, bool Resize)
{
#ifdef _WIN32
    // Check size
    if (Width < 8 || Height < 12) return;

    bool DBef = true;
    bool DAft = true;
    std::string SLog = "";

    const HWND hWnd = GetConsoleWindow();
    const HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

    // Get console info
    CONSOLE_SCREEN_BUFFER_INFO ConInfo;
    GetConsoleScreenBufferInfo(hConsole, &ConInfo);
    DWORD BufferSize = ConInfo.dwSize.X * ConInfo.dwSize.Y;

    // ---------------------------------------------------------------------
    //  Save the current text
    // ------------------------
    DWORD cCharsRead = 0;
    COORD coordScreen = { 0, 0 };

    static const int MAX_BYTES = 1024 * 16;

    std::vector<std::array<TCHAR, MAX_BYTES>> Str;
    std::vector<std::array<WORD, MAX_BYTES>> Attr;

    // ReadConsoleOutputAttribute seems to have a limit at this level
    static const int ReadBufferSize = MAX_BYTES - 32;

    DWORD cAttrRead = ReadBufferSize;
    DWORD BytesRead = 0;
    while (BytesRead < BufferSize)
    {
        Str.resize(Str.size() + 1);
        if (!ReadConsoleOutputCharacter(hConsole, Str.back().data(), ReadBufferSize, coordScreen, &cCharsRead))
            SLog += Common::StringFromFormat("WriteConsoleOutputCharacter error");

        Attr.resize(Attr.size() + 1);
        if (!ReadConsoleOutputAttribute(hConsole, Attr.back().data(), ReadBufferSize, coordScreen, &cAttrRead))
            SLog += Common::StringFromFormat("WriteConsoleOutputAttribute error");

        // Break on error
        if (cAttrRead == 0) break;
        BytesRead += cAttrRead;
        coordScreen = GetCoordinates(BytesRead, ConInfo.dwSize.X);
    }
    // Letter space
    int LWidth = (int)(floor((float)Width / 8.0f) - 1.0f);
    int LHeight = (int)(floor((float)Height / 12.0f) - 1.0f);
    int LBufWidth = LWidth + 1;
    int LBufHeight = (int)floor((float)BufferSize / (float)LBufWidth);
    // Change screen buffer size
    LetterSpace(LBufWidth, LBufHeight);


    ClearScreen(true);
    coordScreen.Y = 0;
    coordScreen.X = 0;
    DWORD cCharsWritten = 0;

    int BytesWritten = 0;
    DWORD cAttrWritten = 0;
    for (size_t i = 0; i < Attr.size(); i++)
    {
        if (!WriteConsoleOutputCharacter(hConsole, Str[i].data(), ReadBufferSize, coordScreen, &cCharsWritten))
            SLog += Common::StringFromFormat("WriteConsoleOutputCharacter error");
        if (!WriteConsoleOutputAttribute(hConsole, Attr[i].data(), ReadBufferSize, coordScreen, &cAttrWritten))
            SLog += Common::StringFromFormat("WriteConsoleOutputAttribute error");

        BytesWritten += cAttrWritten;
        coordScreen = GetCoordinates(BytesWritten, LBufWidth);
    }

    const int OldCursor = ConInfo.dwCursorPosition.Y * ConInfo.dwSize.X + ConInfo.dwCursorPosition.X;
    COORD Coo = GetCoordinates(OldCursor, LBufWidth);
    SetConsoleCursorPosition(hConsole, Coo);

    if (SLog.length() > 0) Log(LogTypes::LNOTICE, SLog.c_str());

    // Resize the window too
    if (Resize) MoveWindow(GetConsoleWindow(), Left,Top, (Width + 100),Height, true);
#endif
}

void ConsoleListener::Log(LogTypes::LOG_LEVELS Level, const char *Text)
{
#if defined(_WIN32)
    WORD Color;

    switch (Level)
    {
    case OS_LEVEL: // light yellow
        Color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY;
        break;
    case NOTICE_LEVEL: // light green
        Color = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
        break;
    case ERROR_LEVEL: // light red
        Color = FOREGROUND_RED | FOREGROUND_INTENSITY;
        break;
    case WARNING_LEVEL: // light purple
        Color = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
        break;
    case INFO_LEVEL: // cyan
        Color = FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
        break;
    case DEBUG_LEVEL: // gray
        Color = FOREGROUND_INTENSITY;
        break;
    default: // off-white
        Color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
        break;
    }
    SetConsoleTextAttribute(hConsole, Color);
    printf(Text);
#else
    char ColorAttr[16] = "";
    char ResetAttr[16] = "";

    if (bUseColor)
    {
        strcpy(ResetAttr, "\033[0m");
        switch (Level)
        {
        case NOTICE_LEVEL: // light green
            strcpy(ColorAttr, "\033[92m");
            break;
        case ERROR_LEVEL: // light red
            strcpy(ColorAttr, "\033[91m");
            break;
        case WARNING_LEVEL: // light yellow
            strcpy(ColorAttr, "\033[93m");
            break;
        default:
            break;
        }
    }
    fprintf(stderr, "%s%s%s", ColorAttr, Text, ResetAttr);
#endif
}
// Clear console screen
void ConsoleListener::ClearScreen(bool Cursor)
{
#if defined(_WIN32)
    COORD coordScreen = { 0, 0 };
    DWORD cCharsWritten;
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    DWORD dwConSize;

    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

    GetConsoleScreenBufferInfo(hConsole, &csbi);
    dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
    // Write space to the entire console
    FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
    GetConsoleScreenBufferInfo(hConsole, &csbi);
    FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
    // Reset cursor
    if (Cursor) SetConsoleCursorPosition(hConsole, coordScreen);
#endif
}