GuiConsole.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2004 Jürgen Riegel <juergen.riegel@web.de>              *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 
00024 #include "PreCompiled.h"
00025 
00026 #ifndef _PreComp_
00027 # include "stdio.h"
00028 # ifdef FC_OS_WIN32
00029 #   include "io.h"
00030 # endif
00031 # ifdef FC_OS_WIN32
00032 #   include <windows.h>
00033 # endif
00034 # include "fcntl.h"
00035 # include <iostream>
00036 #endif
00037 
00038 #include "GuiConsole.h"
00039 
00040 using namespace Gui;
00041 
00042 #ifdef FC_OS_WIN32
00043 
00044 const unsigned int GUIConsole::s_nMaxLines = 1000;
00045 unsigned int       GUIConsole::s_nRefCount = 0;
00046 
00052 GUIConsole::GUIConsole (void)
00053 {
00054   if (!s_nRefCount++)
00055   {
00056     bLog = false;
00057 
00058     CONSOLE_SCREEN_BUFFER_INFO csbi;
00059 
00060     ::AllocConsole();
00061 
00062     ::GetConsoleScreenBufferInfo(::GetStdHandle(STD_OUTPUT_HANDLE),&csbi);
00063      csbi.dwSize.Y = s_nMaxLines;
00064     ::SetConsoleScreenBufferSize(::GetStdHandle(STD_OUTPUT_HANDLE),csbi.dwSize);
00065     ::SetConsoleTitle( "FreeCAD Console");
00066 
00067     *stdout = *::_fdopen(::_open_osfhandle(reinterpret_cast<LONG>(::GetStdHandle(STD_OUTPUT_HANDLE)), _O_TEXT), "w");
00068     ::setvbuf(stdout, 0, _IONBF, 0);
00069 
00070     *stdin = *::_fdopen(::_open_osfhandle(reinterpret_cast<LONG>(::GetStdHandle(STD_INPUT_HANDLE)), _O_TEXT), "r");
00071     ::setvbuf(stdin, 0, _IONBF, 0);
00072 
00073     *stderr = *::_fdopen(::_open_osfhandle(reinterpret_cast<LONG>(::GetStdHandle(STD_ERROR_HANDLE)), _O_TEXT), "w");
00074     ::setvbuf(stderr, 0, _IONBF, 0);
00075   }
00076 }
00077 
00081 GUIConsole::~GUIConsole (void)
00082 {
00083   if (!--s_nRefCount)
00084       FreeConsole();
00085 }
00086 
00087 void GUIConsole::Message(const char *sMsg)
00088 {
00089   ::SetConsoleTextAttribute(::GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN);
00090   printf("%s",sMsg);
00091   ::SetConsoleTextAttribute(::GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE );
00092 }
00093 
00094 void GUIConsole::Warning(const char *sWarn)
00095 {
00096   ::SetConsoleTextAttribute(::GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED | FOREGROUND_GREEN);
00097   printf("%s",sWarn);
00098   ::SetConsoleTextAttribute(::GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE );
00099 }
00100 
00101 void GUIConsole::Error  (const char *sErr)
00102 {
00103   ::SetConsoleTextAttribute(::GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED );
00104   printf("%s",sErr);
00105   ::SetConsoleTextAttribute(::GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE );
00106 }
00107 
00108 void GUIConsole::Log  (const char *sLog)
00109 {
00110   printf("%s",sLog);
00111 }
00112 
00113 #else /* FC_OS_LINUX */
00114 
00115 // safely ignore GUIConsole::s_nMaxLines and  GUIConsole::s_nRefCount
00116 GUIConsole::GUIConsole (void) {}
00117 GUIConsole::~GUIConsole (void) {}
00118 void GUIConsole::Message(const char *sMsg) { std::cout<<sMsg; }
00119 void GUIConsole::Warning(const char *sWarn){ std::cerr<<"Warning: "<<sWarn; }
00120 void GUIConsole::Error  (const char *sErr) { std::cerr<<"Error: "<<sErr;}
00121 void GUIConsole::Log  (const char *sLog)   { std::clog<<sLog;}
00122 
00123 #endif /* FC_OS_LINUX */

Generated on Wed Nov 23 19:00:17 2011 for FreeCAD by  doxygen 1.6.1