FreeCAD

Форк
0
/
GuiConsole.cpp 
154 строки · 5.5 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2004 Jürgen Riegel <juergen.riegel@web.de>              *
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

24
#include "PreCompiled.h"
25

26
#ifndef _PreComp_
27
# ifdef FC_OS_WIN32
28
#   include "io.h"
29
#   include <windows.h>
30
# endif
31
# include <fcntl.h>
32
# include <iostream>
33
#endif
34

35
#include "GuiConsole.h"
36

37
using namespace Gui;
38

39
#ifdef FC_OS_WIN32
40

41
const unsigned int GUIConsole::s_nMaxLines = 1000;
42
unsigned int       GUIConsole::s_nRefCount = 0;
43

44
/** Constructor
45
 *  Open a Top level Window and redirect the
46
 *  stdin, stdout and stderr stream to it.
47
 *  Not needed in Linux!
48
 */
49
GUIConsole::GUIConsole (void)
50
{
51
  if (!s_nRefCount++)
52
  {
53
    bLog = false;
54

55
    CONSOLE_SCREEN_BUFFER_INFO csbi;
56

57
    ::AllocConsole();
58

59
    ::GetConsoleScreenBufferInfo(::GetStdHandle(STD_OUTPUT_HANDLE),&csbi);
60
     csbi.dwSize.Y = s_nMaxLines;
61
    ::SetConsoleScreenBufferSize(::GetStdHandle(STD_OUTPUT_HANDLE),csbi.dwSize);
62
    ::SetConsoleTitleA( "FreeCAD Console");
63

64
    *stdout = *::_fdopen(::_open_osfhandle(reinterpret_cast<intptr_t>(::GetStdHandle(STD_OUTPUT_HANDLE)), _O_TEXT), "w");
65
    ::setvbuf(stdout, 0, _IONBF, 0);
66

67
    *stdin = *::_fdopen(::_open_osfhandle(reinterpret_cast<intptr_t>(::GetStdHandle(STD_INPUT_HANDLE)), _O_TEXT), "r");
68
    ::setvbuf(stdin, 0, _IONBF, 0);
69

70
    *stderr = *::_fdopen(::_open_osfhandle(reinterpret_cast<intptr_t>(::GetStdHandle(STD_ERROR_HANDLE)), _O_TEXT), "w");
71
    ::setvbuf(stderr, 0, _IONBF, 0);
72
  }
73
}
74

75
/** Destructor
76
 *  Close the window and redirect the streams back
77
 */
78
GUIConsole::~GUIConsole (void)
79
{
80
  if (!--s_nRefCount)
81
      FreeConsole();
82
}
83

84
void GUIConsole::SendLog(const std::string& notifiername, const std::string& msg, Base::LogStyle level,
85
                         Base::IntendedRecipient recipient, Base::ContentType content)
86
{
87
    (void) notifiername;
88

89
    // Do not log translated messages, or messages intended only to the user to std log
90
    if(recipient == Base::IntendedRecipient::User || content == Base::ContentType::Translated)
91
        return;
92

93
    int color = -1;
94
    switch(level){
95
        case Base::LogStyle::Warning:
96
            color = FOREGROUND_RED | FOREGROUND_GREEN;
97
            break;
98
        case Base::LogStyle::Message:
99
            color = FOREGROUND_GREEN;
100
            break;
101
        case Base::LogStyle::Error:
102
            color = FOREGROUND_RED;
103
            break;
104
        case Base::LogStyle::Log:
105
            color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
106
            break;
107
        case Base::LogStyle::Critical:
108
            color = FOREGROUND_RED | FOREGROUND_GREEN;
109
            break;
110
        default:
111
            break;
112
    }
113

114
    ::SetConsoleTextAttribute(::GetStdHandle(STD_OUTPUT_HANDLE), color);
115
    printf("%s", msg.c_str());
116
    ::SetConsoleTextAttribute(::GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE );
117
}
118

119
#else /* FC_OS_LINUX */
120

121
// safely ignore GUIConsole::s_nMaxLines and  GUIConsole::s_nRefCount
122
GUIConsole::GUIConsole () = default;
123
GUIConsole::~GUIConsole () = default;
124
void GUIConsole::SendLog(const std::string& notifiername, const std::string& msg, Base::LogStyle level,
125
                         Base::IntendedRecipient recipient, Base::ContentType content)
126
{
127
    (void) notifiername;
128

129
    // Do not log translated messages, or messages intended only to the user to std log
130
    if(recipient == Base::IntendedRecipient::User || content == Base::ContentType::Translated)
131
        return;
132

133
    switch(level){
134
        case Base::LogStyle::Warning:
135
            std::cerr << "Warning: " << msg;
136
            break;
137
        case Base::LogStyle::Message:
138
            std::cout << msg;
139
            break;
140
        case Base::LogStyle::Error:
141
            std::cerr << "Error: " << msg;
142
            break;
143
        case Base::LogStyle::Log:
144
            std::clog << msg;
145
            break;
146
        case Base::LogStyle::Critical:
147
            std::cout << "Critical: " << msg;
148
            break;
149
        default:
150
            break;
151
    }
152
}
153

154
#endif /* FC_OS_LINUX */
155

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.