1 // Created on: 2001-01-06
2 // Created by: OCC Team
3 // Copyright (c) 2001-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
20 #include <Message_PrinterOStream.hxx>
22 #include <OSD_OpenFile.hxx>
23 #include <TCollection_AsciiString.hxx>
24 #include <TCollection_ExtendedString.hxx>
26 IMPLEMENT_STANDARD_RTTIEXT(Message_PrinterOStream,Message_Printer)
28 #if !defined(_MSC_VER)
32 //=======================================================================
33 //function : Constructor
34 //purpose : Empty constructor, defaulting to cerr
35 //=======================================================================
36 Message_PrinterOStream::Message_PrinterOStream (const Message_Gravity theTraceLevel)
37 : myStream (&std::cout),
38 myIsFile (Standard_False),
39 myUseUtf8 (Standard_False),
40 myToColorize (Standard_True)
42 myTraceLevel = theTraceLevel;
45 //=======================================================================
46 //function : Constructor
47 //purpose : Opening a file as an std::ostream
48 // for specific file names standard streams are created
49 //=======================================================================
50 Message_PrinterOStream::Message_PrinterOStream (const Standard_CString theFileName,
51 const Standard_Boolean theToAppend,
52 const Message_Gravity theTraceLevel)
53 : myStream (&std::cout),
54 myIsFile (Standard_False),
55 myUseUtf8 (Standard_False),
56 myToColorize (Standard_True)
58 myTraceLevel = theTraceLevel;
59 if (strcasecmp(theFileName, "cerr") == 0)
61 myStream = &std::cerr;
64 else if (strcasecmp(theFileName, "cout") == 0)
66 myStream = &std::cout;
70 TCollection_AsciiString aFileName (theFileName);
72 aFileName.ChangeAll ('/', '\\');
75 std::ofstream* aFile = new std::ofstream();
76 OSD_OpenStream (*aFile, aFileName.ToCString(), (theToAppend ? (std::ios_base::app | std::ios_base::out) : std::ios_base::out));
79 myStream = (Standard_OStream* )aFile;
80 myIsFile = Standard_True;
81 myToColorize = Standard_False;
86 myStream = &std::cout;
88 std::cerr << "Error opening " << theFileName << std::endl << std::flush;
93 //=======================================================================
96 //=======================================================================
98 void Message_PrinterOStream::Close ()
100 if ( ! myStream ) return;
101 Standard_OStream* ostr = (Standard_OStream*)myStream;
107 std::ofstream* ofile = (std::ofstream* )ostr;
110 myIsFile = Standard_False;
114 //=======================================================================
117 //=======================================================================
119 void Message_PrinterOStream::Send (const Standard_CString theString,
120 const Message_Gravity theGravity,
121 const Standard_Boolean putEndl) const
123 if (theGravity < myTraceLevel
129 Message_ConsoleColor aColor = Message_ConsoleColor_Default;
130 bool toIntense = false;
131 if (myToColorize && !myIsFile)
136 aColor = Message_ConsoleColor_Yellow;
139 aColor = Message_ConsoleColor_Green;
142 case Message_Warning:
143 aColor = Message_ConsoleColor_Yellow;
147 aColor = Message_ConsoleColor_Red;
151 aColor = Message_ConsoleColor_Red;
157 Standard_OStream* aStream = (Standard_OStream*)myStream;
158 if (toIntense || aColor != Message_ConsoleColor_Default)
160 SetConsoleTextColor (aStream, aColor, toIntense);
161 *aStream << theString;
162 SetConsoleTextColor (aStream, Message_ConsoleColor_Default, false);
166 *aStream << theString;
170 (*aStream) << std::endl;
174 //=======================================================================
177 //=======================================================================
179 void Message_PrinterOStream::Send (const TCollection_AsciiString &theString,
180 const Message_Gravity theGravity,
181 const Standard_Boolean putEndl) const
183 Send ( theString.ToCString(), theGravity, putEndl );
186 //=======================================================================
189 //=======================================================================
191 void Message_PrinterOStream::Send (const TCollection_ExtendedString &theString,
192 const Message_Gravity theGravity,
193 const Standard_Boolean putEndl) const
195 TCollection_AsciiString aStr (theString, myUseUtf8 ? Standard_Character(0) : '?');
196 Send (aStr.ToCString(), theGravity, putEndl);
199 //=======================================================================
200 //function : SetConsoleTextColor
202 //=======================================================================
203 void Message_PrinterOStream::SetConsoleTextColor (Standard_OStream* theOStream,
204 Message_ConsoleColor theTextColor,
205 bool theIsIntenseText)
208 // there is no difference between STD_OUTPUT_HANDLE/STD_ERROR_HANDLE for std::cout/std::cerr
210 if (HANDLE anStdOut = GetStdHandle (STD_OUTPUT_HANDLE))
213 if (theIsIntenseText)
215 aFlags |= FOREGROUND_INTENSITY;
217 switch (theTextColor)
219 case Message_ConsoleColor_Default:
220 case Message_ConsoleColor_White:
221 aFlags |= FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
223 case Message_ConsoleColor_Black:
225 case Message_ConsoleColor_Red:
226 aFlags |= FOREGROUND_RED;
228 case Message_ConsoleColor_Green:
229 aFlags |= FOREGROUND_GREEN;
231 case Message_ConsoleColor_Blue:
232 aFlags |= FOREGROUND_BLUE;
234 case Message_ConsoleColor_Yellow:
235 aFlags |= FOREGROUND_RED | FOREGROUND_GREEN;
237 case Message_ConsoleColor_Cyan:
238 aFlags |= FOREGROUND_GREEN | FOREGROUND_BLUE;
240 case Message_ConsoleColor_Magenta:
241 aFlags |= FOREGROUND_RED | FOREGROUND_BLUE;
244 SetConsoleTextAttribute (anStdOut, aFlags);
246 #elif defined(__EMSCRIPTEN__)
247 // Terminal capabilities are undefined on this platform.
248 // std::cout could be redirected to HTML page, into terminal or somewhere else.
251 (void )theIsIntenseText;
253 if (theOStream == NULL)
258 const char* aCode = "\e[0m";
259 switch (theTextColor)
261 case Message_ConsoleColor_Default:
262 aCode = theIsIntenseText ? "\e[0;1m" : "\e[0m";
264 case Message_ConsoleColor_Black:
265 aCode = theIsIntenseText ? "\e[30;1m" : "\e[30m";
267 case Message_ConsoleColor_Red:
268 aCode = theIsIntenseText ? "\e[31;1m" : "\e[31m";
270 case Message_ConsoleColor_Green:
271 aCode = theIsIntenseText ? "\e[32;1m" : "\e[32m";
273 case Message_ConsoleColor_Yellow:
274 aCode = theIsIntenseText ? "\e[33;1m" : "\e[33m";
276 case Message_ConsoleColor_Blue:
277 aCode = theIsIntenseText ? "\e[34;1m" : "\e[34m";
279 case Message_ConsoleColor_Magenta:
280 aCode = theIsIntenseText ? "\e[35;1m" : "\e[35m";
282 case Message_ConsoleColor_Cyan:
283 aCode = theIsIntenseText ? "\e[36;1m" : "\e[36m";
285 case Message_ConsoleColor_White:
286 aCode = theIsIntenseText ? "\e[37;1m" : "\e[37m";
289 *theOStream << aCode;