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_False)
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_False)
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;
85 myStream = &std::cout;
87 std::cerr << "Error opening " << theFileName << std::endl << std::flush;
92 //=======================================================================
95 //=======================================================================
97 void Message_PrinterOStream::Close ()
99 if ( ! myStream ) return;
100 Standard_OStream* ostr = (Standard_OStream*)myStream;
106 std::ofstream* ofile = (std::ofstream* )ostr;
109 myIsFile = Standard_False;
113 //=======================================================================
116 //=======================================================================
118 void Message_PrinterOStream::Send (const Standard_CString theString,
119 const Message_Gravity theGravity,
120 const Standard_Boolean putEndl) const
122 if (theGravity < myTraceLevel
128 Message_ConsoleColor aColor = Message_ConsoleColor_Default;
129 bool toIntense = false;
130 if (myToColorize && !myIsFile)
135 aColor = Message_ConsoleColor_Yellow;
138 aColor = Message_ConsoleColor_Green;
141 case Message_Warning:
142 aColor = Message_ConsoleColor_Yellow;
146 aColor = Message_ConsoleColor_Red;
150 aColor = Message_ConsoleColor_Red;
156 Standard_OStream* aStream = (Standard_OStream*)myStream;
157 if (toIntense || aColor != Message_ConsoleColor_Default)
159 SetConsoleTextColor (aStream, aColor, toIntense);
160 *aStream << theString;
161 SetConsoleTextColor (aStream, Message_ConsoleColor_Default, false);
165 *aStream << theString;
169 (*aStream) << std::endl;
173 //=======================================================================
176 //=======================================================================
178 void Message_PrinterOStream::Send (const TCollection_AsciiString &theString,
179 const Message_Gravity theGravity,
180 const Standard_Boolean putEndl) const
182 Send ( theString.ToCString(), theGravity, putEndl );
185 //=======================================================================
188 //=======================================================================
190 void Message_PrinterOStream::Send (const TCollection_ExtendedString &theString,
191 const Message_Gravity theGravity,
192 const Standard_Boolean putEndl) const
194 TCollection_AsciiString aStr (theString, myUseUtf8 ? Standard_Character(0) : '?');
195 Send (aStr.ToCString(), theGravity, putEndl);
198 //=======================================================================
199 //function : SetConsoleTextColor
201 //=======================================================================
202 void Message_PrinterOStream::SetConsoleTextColor (Standard_OStream* theOStream,
203 Message_ConsoleColor theTextColor,
204 bool theIsIntenseText)
207 // there is no difference between STD_OUTPUT_HANDLE/STD_ERROR_HANDLE for std::cout/std::cerr
209 if (HANDLE anStdOut = GetStdHandle (STD_OUTPUT_HANDLE))
212 if (theIsIntenseText)
214 aFlags |= FOREGROUND_INTENSITY;
216 switch (theTextColor)
218 case Message_ConsoleColor_Default:
219 case Message_ConsoleColor_White:
220 aFlags |= FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
222 case Message_ConsoleColor_Black:
224 case Message_ConsoleColor_Red:
225 aFlags |= FOREGROUND_RED;
227 case Message_ConsoleColor_Green:
228 aFlags |= FOREGROUND_GREEN;
230 case Message_ConsoleColor_Blue:
231 aFlags |= FOREGROUND_BLUE;
233 case Message_ConsoleColor_Yellow:
234 aFlags |= FOREGROUND_RED | FOREGROUND_GREEN;
236 case Message_ConsoleColor_Cyan:
237 aFlags |= FOREGROUND_GREEN | FOREGROUND_BLUE;
239 case Message_ConsoleColor_Magenta:
240 aFlags |= FOREGROUND_RED | FOREGROUND_BLUE;
243 SetConsoleTextAttribute (anStdOut, aFlags);
246 if (theOStream == NULL)
251 const char* aCode = "\e[0m";
252 switch (theTextColor)
254 case Message_ConsoleColor_Default:
255 aCode = theIsIntenseText ? "\e[0;1m" : "\e[0m";
257 case Message_ConsoleColor_Black:
258 aCode = theIsIntenseText ? "\e[30;1m" : "\e[30m";
260 case Message_ConsoleColor_Red:
261 aCode = theIsIntenseText ? "\e[31;1m" : "\e[31m";
263 case Message_ConsoleColor_Green:
264 aCode = theIsIntenseText ? "\e[32;1m" : "\e[32m";
266 case Message_ConsoleColor_Yellow:
267 aCode = theIsIntenseText ? "\e[33;1m" : "\e[33m";
269 case Message_ConsoleColor_Blue:
270 aCode = theIsIntenseText ? "\e[34;1m" : "\e[34m";
272 case Message_ConsoleColor_Magenta:
273 aCode = theIsIntenseText ? "\e[35;1m" : "\e[35m";
275 case Message_ConsoleColor_Cyan:
276 aCode = theIsIntenseText ? "\e[36;1m" : "\e[36m";
278 case Message_ConsoleColor_White:
279 aCode = theIsIntenseText ? "\e[37;1m" : "\e[37m";
282 *theOStream << aCode;