Files
create/src/Gui/GuiConsole.cpp
ezzieyguywuf 9fcc18b08e Refactor and rename ConsoleObserver...
...Renamed to "ILogger", to designate that this is an Interface for a
Logger. This "Interface" is pure virtual, so that it cannot be
instantiated directly. This makes it clear that it is intended to be
derived.

Finally, got rid of all the individual log-style methods and replaced
with SendLog. The idea here is that day-to-day users will only interact
with ILogger through ConsoleSingleton (or, likely, LoggerSingleton in
the future). This singleton will manage an arbirtary collection of
ILogger, and call SendLog with the appropriate parameters based on what
the user requests.

Therefore, the singleton itself will have the individual Log, Message,
Error, etc... methods, while stil allowing us to simplify the code base
of ILogger and its derived classes.
2019-10-20 11:46:15 +02:00

134 lines
4.6 KiB
C++

/***************************************************************************
* Copyright (c) 2004 Jürgen Riegel <juergen.riegel@web.de> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library 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 Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
# include "stdio.h"
# ifdef FC_OS_WIN32
# include "io.h"
# endif
# ifdef FC_OS_WIN32
# include <windows.h>
# endif
# include "fcntl.h"
# include <iostream>
#endif
#include "GuiConsole.h"
using namespace Gui;
#ifdef FC_OS_WIN32
const unsigned int GUIConsole::s_nMaxLines = 1000;
unsigned int GUIConsole::s_nRefCount = 0;
/** Constructor
* Open a Top level Window and redirect the
* stdin, stdout and stderr stream to it.
* Not needed in Linux!
*/
GUIConsole::GUIConsole (void)
{
if (!s_nRefCount++)
{
bLog = false;
CONSOLE_SCREEN_BUFFER_INFO csbi;
::AllocConsole();
::GetConsoleScreenBufferInfo(::GetStdHandle(STD_OUTPUT_HANDLE),&csbi);
csbi.dwSize.Y = s_nMaxLines;
::SetConsoleScreenBufferSize(::GetStdHandle(STD_OUTPUT_HANDLE),csbi.dwSize);
::SetConsoleTitleA( "FreeCAD Console");
*stdout = *::_fdopen(::_open_osfhandle(reinterpret_cast<intptr_t>(::GetStdHandle(STD_OUTPUT_HANDLE)), _O_TEXT), "w");
::setvbuf(stdout, 0, _IONBF, 0);
*stdin = *::_fdopen(::_open_osfhandle(reinterpret_cast<intptr_t>(::GetStdHandle(STD_INPUT_HANDLE)), _O_TEXT), "r");
::setvbuf(stdin, 0, _IONBF, 0);
*stderr = *::_fdopen(::_open_osfhandle(reinterpret_cast<intptr_t>(::GetStdHandle(STD_ERROR_HANDLE)), _O_TEXT), "w");
::setvbuf(stderr, 0, _IONBF, 0);
}
}
/** Destructor
* Close the window and redirect the streams back
*/
GUIConsole::~GUIConsole (void)
{
if (!--s_nRefCount)
FreeConsole();
}
void GUIConsole::SendLog(const std::string& msg, Base::LogStyle level)
{
int color = -1;
switch(level){
case Base::LogStyle::Warning:
color = FOREGROUND_RED | FOREGROUND_GREEN;
break;
case Base::LogStyle::Message:
color = FOREGROUND_GREEN;
break;
case Base::LogStyle::Error:
color = FOREGROUND_RED;
break;
case Base::LogStyle::Log:
color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
break;
}
::SetConsoleTextAttribute(::GetStdHandle(STD_OUTPUT_HANDLE), color);
printf("%s", msg.c_str());
::SetConsoleTextAttribute(::GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE );
}
#else /* FC_OS_LINUX */
// safely ignore GUIConsole::s_nMaxLines and GUIConsole::s_nRefCount
GUIConsole::GUIConsole (void) {}
GUIConsole::~GUIConsole (void) {}
void GUIConsole::SendLog(const std::string& msg, Base::LogStyle level)
{
switch(level){
case Base::LogStyle::Warning:
std::cerr << "Warning: " << msg;
break;
case Base::LogStyle::Message:
std::cout << msg;
break;
case Base::LogStyle::Error:
std::cerr << "Error: " << msg;
break;
case Base::LogStyle::Log:
std::clog << msg;
break;
}
}
#endif /* FC_OS_LINUX */