0031501: Foundation Classes, Message_Printer - remove theToPutEndl argument
[occt.git] / src / Message / Message_PrinterOStream.cxx
1 // Created on: 2001-01-06
2 // Created by: OCC Team
3 // Copyright (c) 2001-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #ifdef _WIN32
17   #include <windows.h>
18 #endif
19
20 #include <Message_PrinterOStream.hxx>
21
22 #include <OSD_OpenFile.hxx>
23 #include <TCollection_AsciiString.hxx>
24 #include <TCollection_ExtendedString.hxx>
25
26 IMPLEMENT_STANDARD_RTTIEXT(Message_PrinterOStream,Message_Printer)
27
28 #if !defined(_MSC_VER)
29   #include <strings.h>
30 #endif
31
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)
40 {
41   myTraceLevel = theTraceLevel;
42 }
43
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)
55 {
56   myTraceLevel = theTraceLevel;
57   if (strcasecmp(theFileName, "cerr") == 0)
58   {
59     myStream = &std::cerr;
60     return;
61   }
62   else if (strcasecmp(theFileName, "cout") == 0)
63   {
64     myStream = &std::cout;
65     return;
66   }
67
68   TCollection_AsciiString aFileName (theFileName);
69 #ifdef _WIN32
70   aFileName.ChangeAll ('/', '\\');
71 #endif
72
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));
75   if (aFile->is_open())
76   {
77     myStream = (Standard_OStream* )aFile;
78     myIsFile = Standard_True;
79     myToColorize = Standard_False;
80   }
81   else
82   {
83     delete aFile;
84     myStream = &std::cout;
85 #ifdef OCCT_DEBUG
86     std::cerr << "Error opening " << theFileName << std::endl << std::flush;
87 #endif
88   }
89 }
90
91 //=======================================================================
92 //function : Close
93 //purpose  : 
94 //=======================================================================
95
96 void Message_PrinterOStream::Close ()
97 {
98   if ( ! myStream ) return;
99   Standard_OStream* ostr = (Standard_OStream*)myStream;
100   myStream = 0;
101
102   ostr->flush();
103   if ( myIsFile )
104   {
105     std::ofstream* ofile = (std::ofstream* )ostr;
106     ofile->close();
107     delete ofile;
108     myIsFile = Standard_False;
109   }
110 }
111
112 //=======================================================================
113 //function : send
114 //purpose  :
115 //=======================================================================
116 void Message_PrinterOStream::send (const TCollection_AsciiString& theString,
117                                    const Message_Gravity theGravity) const
118 {
119   if (theGravity < myTraceLevel
120    || myStream == NULL)
121   {
122     return;
123   }
124
125   Message_ConsoleColor aColor = Message_ConsoleColor_Default;
126   bool toIntense = false;
127   if (myToColorize && !myIsFile)
128   {
129     switch(theGravity)
130     {
131       case Message_Trace:
132         aColor = Message_ConsoleColor_Yellow;
133         break;
134       case Message_Info:
135         aColor = Message_ConsoleColor_Green;
136         toIntense = true;
137         break;
138       case Message_Warning:
139         aColor = Message_ConsoleColor_Yellow;
140         toIntense = true;
141         break;
142       case Message_Alarm:
143         aColor = Message_ConsoleColor_Red;
144         toIntense = true;
145         break;
146       case Message_Fail:
147         aColor = Message_ConsoleColor_Red;
148         toIntense = true;
149         break;
150     }
151   }
152
153   Standard_OStream* aStream = (Standard_OStream*)myStream;
154   if (toIntense || aColor != Message_ConsoleColor_Default)
155   {
156     SetConsoleTextColor (aStream, aColor, toIntense);
157     *aStream << theString;
158     SetConsoleTextColor (aStream, Message_ConsoleColor_Default, false);
159   }
160   else
161   {
162     *aStream << theString;
163   }
164   (*aStream) << std::endl;
165 }
166
167 //=======================================================================
168 //function : SetConsoleTextColor
169 //purpose  :
170 //=======================================================================
171 void Message_PrinterOStream::SetConsoleTextColor (Standard_OStream* theOStream,
172                                                   Message_ConsoleColor theTextColor,
173                                                   bool theIsIntenseText)
174 {
175 #ifdef _WIN32
176   // there is no difference between STD_OUTPUT_HANDLE/STD_ERROR_HANDLE for std::cout/std::cerr
177   (void )theOStream;
178   if (HANDLE anStdOut = GetStdHandle (STD_OUTPUT_HANDLE))
179   {
180     WORD aFlags = 0;
181     if (theIsIntenseText)
182     {
183       aFlags |= FOREGROUND_INTENSITY;
184     }
185     switch (theTextColor)
186     {
187       case Message_ConsoleColor_Default:
188       case Message_ConsoleColor_White:
189         aFlags |= FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
190         break;
191       case Message_ConsoleColor_Black:
192         break;
193       case Message_ConsoleColor_Red:
194         aFlags |= FOREGROUND_RED;
195         break;
196       case Message_ConsoleColor_Green:
197         aFlags |= FOREGROUND_GREEN;
198         break;
199       case Message_ConsoleColor_Blue:
200         aFlags |= FOREGROUND_BLUE;
201         break;
202       case Message_ConsoleColor_Yellow:
203         aFlags |= FOREGROUND_RED | FOREGROUND_GREEN;
204         break;
205       case Message_ConsoleColor_Cyan:
206         aFlags |= FOREGROUND_GREEN | FOREGROUND_BLUE;
207         break;
208       case Message_ConsoleColor_Magenta:
209         aFlags |= FOREGROUND_RED | FOREGROUND_BLUE;
210         break;
211     }
212     SetConsoleTextAttribute (anStdOut, aFlags);
213   }
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.
217   (void )theOStream;
218   (void )theTextColor;
219   (void )theIsIntenseText;
220 #else
221   if (theOStream == NULL)
222   {
223     return;
224   }
225
226   const char* aCode = "\e[0m";
227   switch (theTextColor)
228   {
229     case Message_ConsoleColor_Default:
230       aCode = theIsIntenseText ? "\e[0;1m" : "\e[0m";
231       break;
232     case Message_ConsoleColor_Black:
233       aCode = theIsIntenseText ? "\e[30;1m" : "\e[30m";
234       break;
235     case Message_ConsoleColor_Red:
236       aCode = theIsIntenseText ? "\e[31;1m" : "\e[31m";
237       break;
238     case Message_ConsoleColor_Green:
239       aCode = theIsIntenseText ? "\e[32;1m" : "\e[32m";
240       break;
241     case Message_ConsoleColor_Yellow:
242       aCode = theIsIntenseText ? "\e[33;1m" : "\e[33m";
243       break;
244     case Message_ConsoleColor_Blue:
245       aCode = theIsIntenseText ? "\e[34;1m" : "\e[34m";
246       break;
247     case Message_ConsoleColor_Magenta:
248       aCode = theIsIntenseText ? "\e[35;1m" : "\e[35m";
249       break;
250     case Message_ConsoleColor_Cyan:
251       aCode = theIsIntenseText ? "\e[36;1m" : "\e[36m";
252       break;
253     case Message_ConsoleColor_White:
254       aCode = theIsIntenseText ? "\e[37;1m" : "\e[37m";
255       break;
256   }
257   *theOStream << aCode;
258 #endif
259 }