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 myToColorize (Standard_True)
41 myTraceLevel = theTraceLevel;
44 //=======================================================================
45 //function : Constructor
46 //purpose : Opening a file as an std::ostream
47 // for specific file names standard streams are created
48 //=======================================================================
49 Message_PrinterOStream::Message_PrinterOStream (const Standard_CString theFileName,
50 const Standard_Boolean theToAppend,
51 const Message_Gravity theTraceLevel)
52 : myStream (&std::cout),
53 myIsFile (Standard_False),
54 myToColorize (Standard_True)
56 myTraceLevel = theTraceLevel;
57 if (strcasecmp(theFileName, "cerr") == 0)
59 myStream = &std::cerr;
62 else if (strcasecmp(theFileName, "cout") == 0)
64 myStream = &std::cout;
68 TCollection_AsciiString aFileName (theFileName);
70 aFileName.ChangeAll ('/', '\\');
73 std::ofstream* aFile = new std::ofstream();
74 OSD_OpenStream (*aFile, aFileName.ToCString(), (theToAppend ? (std::ios_base::app | std::ios_base::out) : std::ios_base::out));
77 myStream = (Standard_OStream* )aFile;
78 myIsFile = Standard_True;
79 myToColorize = Standard_False;
84 myStream = &std::cout;
86 std::cerr << "Error opening " << theFileName << std::endl << std::flush;
91 //=======================================================================
94 //=======================================================================
96 void Message_PrinterOStream::Close ()
98 if ( ! myStream ) return;
99 Standard_OStream* ostr = (Standard_OStream*)myStream;
105 std::ofstream* ofile = (std::ofstream* )ostr;
108 myIsFile = Standard_False;
112 //=======================================================================
115 //=======================================================================
116 void Message_PrinterOStream::send (const TCollection_AsciiString& theString,
117 const Message_Gravity theGravity) const
119 if (theGravity < myTraceLevel
125 Message_ConsoleColor aColor = Message_ConsoleColor_Default;
126 bool toIntense = false;
127 if (myToColorize && !myIsFile)
132 aColor = Message_ConsoleColor_Yellow;
135 aColor = Message_ConsoleColor_Green;
138 case Message_Warning:
139 aColor = Message_ConsoleColor_Yellow;
143 aColor = Message_ConsoleColor_Red;
147 aColor = Message_ConsoleColor_Red;
153 Standard_OStream* aStream = (Standard_OStream*)myStream;
154 if (toIntense || aColor != Message_ConsoleColor_Default)
156 SetConsoleTextColor (aStream, aColor, toIntense);
157 *aStream << theString;
158 SetConsoleTextColor (aStream, Message_ConsoleColor_Default, false);
162 *aStream << theString;
164 (*aStream) << std::endl;
167 //=======================================================================
168 //function : SetConsoleTextColor
170 //=======================================================================
171 void Message_PrinterOStream::SetConsoleTextColor (Standard_OStream* theOStream,
172 Message_ConsoleColor theTextColor,
173 bool theIsIntenseText)
176 // there is no difference between STD_OUTPUT_HANDLE/STD_ERROR_HANDLE for std::cout/std::cerr
178 if (HANDLE anStdOut = GetStdHandle (STD_OUTPUT_HANDLE))
181 if (theIsIntenseText)
183 aFlags |= FOREGROUND_INTENSITY;
185 switch (theTextColor)
187 case Message_ConsoleColor_Default:
188 case Message_ConsoleColor_White:
189 aFlags |= FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
191 case Message_ConsoleColor_Black:
193 case Message_ConsoleColor_Red:
194 aFlags |= FOREGROUND_RED;
196 case Message_ConsoleColor_Green:
197 aFlags |= FOREGROUND_GREEN;
199 case Message_ConsoleColor_Blue:
200 aFlags |= FOREGROUND_BLUE;
202 case Message_ConsoleColor_Yellow:
203 aFlags |= FOREGROUND_RED | FOREGROUND_GREEN;
205 case Message_ConsoleColor_Cyan:
206 aFlags |= FOREGROUND_GREEN | FOREGROUND_BLUE;
208 case Message_ConsoleColor_Magenta:
209 aFlags |= FOREGROUND_RED | FOREGROUND_BLUE;
212 SetConsoleTextAttribute (anStdOut, aFlags);
214 #elif defined(__EMSCRIPTEN__)
215 // Terminal capabilities are undefined on this platform.
216 // std::cout could be redirected to HTML page, into terminal or somewhere else.
219 (void )theIsIntenseText;
221 if (theOStream == NULL)
226 const char* aCode = "\e[0m";
227 switch (theTextColor)
229 case Message_ConsoleColor_Default:
230 aCode = theIsIntenseText ? "\e[0;1m" : "\e[0m";
232 case Message_ConsoleColor_Black:
233 aCode = theIsIntenseText ? "\e[30;1m" : "\e[30m";
235 case Message_ConsoleColor_Red:
236 aCode = theIsIntenseText ? "\e[31;1m" : "\e[31m";
238 case Message_ConsoleColor_Green:
239 aCode = theIsIntenseText ? "\e[32;1m" : "\e[32m";
241 case Message_ConsoleColor_Yellow:
242 aCode = theIsIntenseText ? "\e[33;1m" : "\e[33m";
244 case Message_ConsoleColor_Blue:
245 aCode = theIsIntenseText ? "\e[34;1m" : "\e[34m";
247 case Message_ConsoleColor_Magenta:
248 aCode = theIsIntenseText ? "\e[35;1m" : "\e[35m";
250 case Message_ConsoleColor_Cyan:
251 aCode = theIsIntenseText ? "\e[36;1m" : "\e[36m";
253 case Message_ConsoleColor_White:
254 aCode = theIsIntenseText ? "\e[37;1m" : "\e[37m";
257 *theOStream << aCode;