Debian Codegraph @

src/Gui/GuiConsole.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2004 Jürgen Riegel <[email protected]> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23 #include <FCConfig.h>
24
25 #ifdef FC_OS_WIN32
26 # include "io.h"
27 # include <windows.h>
28 #endif
29 #include <fcntl.h>
30 #include <iostream>
31
32 #include "GuiConsole.h"
33
34 using namespace Gui;
35
36 #ifdef FC_OS_WIN32
37
38 const unsigned int GUIConsole::s_nMaxLines = 1000;
39 unsigned int GUIConsole::s_nRefCount = 0;
40
41 /** Constructor
42 * Open a Top level Window and redirect the
43 * stdin, stdout and stderr stream to it.
44 * Not needed in Linux!
45 */
46 GUIConsole::GUIConsole(void)
47 {
48 if (!s_nRefCount++) {
49 bLog = false;
50
51 CONSOLE_SCREEN_BUFFER_INFO csbi;
52
53 ::AllocConsole();
54
55 ::GetConsoleScreenBufferInfo(::GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
56 csbi.dwSize.Y = s_nMaxLines;
57 ::SetConsoleScreenBufferSize(::GetStdHandle(STD_OUTPUT_HANDLE), csbi.dwSize);
58 ::SetConsoleTitleA("FreeCAD Console");
59
60 *stdout = *::_fdopen(
61 ::_open_osfhandle(reinterpret_cast<intptr_t>(::GetStdHandle(STD_OUTPUT_HANDLE)), _O_TEXT),
62 "w"
63 );
64 ::setvbuf(stdout, 0, _IONBF, 0);
65
66 *stdin = *::_fdopen(
67 ::_open_osfhandle(reinterpret_cast<intptr_t>(::GetStdHandle(STD_INPUT_HANDLE)), _O_TEXT),
68 "r"
69 );
70 ::setvbuf(stdin, 0, _IONBF, 0);
71
72 *stderr = *::_fdopen(
73 ::_open_osfhandle(reinterpret_cast<intptr_t>(::GetStdHandle(STD_ERROR_HANDLE)), _O_TEXT),
74 "w"
75 );
76 ::setvbuf(stderr, 0, _IONBF, 0);
77 }
78 }
79
80 /** Destructor
81 * Close the window and redirect the streams back
82 */
83 GUIConsole::~GUIConsole(void)
84 {
85 if (!--s_nRefCount) {
86 FreeConsole();
87 }
88 }
89
90 void GUIConsole::sendLog(
91 const std::string& notifiername,
92 const std::string& msg,
93 Base::LogStyle level,
94 Base::IntendedRecipient recipient,
95 Base::ContentType content
96 )
97 {
98 (void)notifiername;
99
100 // Do not log translated messages, or messages intended only to the user to std log
101 if (recipient == Base::IntendedRecipient::User || content == Base::ContentType::Translated) {
102 return;
103 }
104
105 int color = -1;
106 switch (level) {
107 case Base::LogStyle::Warning:
108 color = FOREGROUND_RED | FOREGROUND_GREEN;
109 break;
110 case Base::LogStyle::Message:
111 color = FOREGROUND_GREEN;
112 break;
113 case Base::LogStyle::Error:
114 color = FOREGROUND_RED;
115 break;
116 case Base::LogStyle::Log:
117 color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
118 break;
119 case Base::LogStyle::Critical:
120 color = FOREGROUND_RED | FOREGROUND_GREEN;
121 break;
122 default:
123 break;
124 }
125
126 ::SetConsoleTextAttribute(::GetStdHandle(STD_OUTPUT_HANDLE), color);
127 printf("%s", msg.c_str());
128 ::SetConsoleTextAttribute(
129 ::GetStdHandle(STD_OUTPUT_HANDLE),
130 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
131 );
132 }
133
134 #else /* FC_OS_LINUX */
135
136 // safely ignore GUIConsole::s_nMaxLines and GUIConsole::s_nRefCount
137 GUIConsole::GUIConsole() = default;
138 GUIConsole::~GUIConsole() = default;
139 void GUIConsole::sendLog(
140 const std::string& notifiername,
141 const std::string& msg,
142 Base::LogStyle level,
143 Base::IntendedRecipient recipient,
144 Base::ContentType content
145 )
146 {
147 (void)notifiername;
148
149 // Do not log translated messages, or messages intended only to the user to std log
150 if (recipient == Base::IntendedRecipient::User || content == Base::ContentType::Translated) {
151 return;
152 }
153
154 switch (level) {
155 case Base::LogStyle::Warning:
156 std::cerr << "Warning: " << msg;
157 break;
158 case Base::LogStyle::Message:
159 std::cout << msg;
160 break;
161 case Base::LogStyle::Error:
162 std::cerr << "Error: " << msg;
163 break;
164 case Base::LogStyle::Log:
165 std::clog << msg;
166 break;
167 case Base::LogStyle::Critical:
168 std::cout << "Critical: " << msg;
169 break;
170 default:
171 break;
172 }
173 }
174
175 #endif /* FC_OS_LINUX */