0026823: Configuration - use EGL on another platform without GLX
[occt.git] / src / Message / Message_PrinterOStream.cxx
CommitLineData
b311480e 1// Created on: 2001-01-06
2// Created by: OCC Team
973c2be1 3// Copyright (c) 2001-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 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
973c2be1 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.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
7fd59977 15
7fd59977 16
17#include <Message_Gravity.hxx>
42cf5bc1 18#include <Message_PrinterOStream.hxx>
7fd59977 19#include <Standard_Mutex.hxx>
20#include <Standard_Stream.hxx>
42cf5bc1 21#include <Standard_Type.hxx>
22#include <TCollection_AsciiString.hxx>
23#include <TCollection_ExtendedString.hxx>
7fd59977 24
d8d01f6e 25#ifndef _WIN32
26 #include <strings.h>
27#endif
28
7fd59977 29//=======================================================================
30//function : Constructor
31//purpose : Empty constructor, defaulting to cerr
32//=======================================================================
785a9540 33Message_PrinterOStream::Message_PrinterOStream (const Message_Gravity theTraceLevel)
d41f6af3 34: myStream (&std::cout),
785a9540 35 myIsFile (Standard_False),
36 myUseUtf8 (Standard_False)
7fd59977 37{
785a9540 38 myTraceLevel = theTraceLevel;
7fd59977 39}
40
41//=======================================================================
42//function : Constructor
43//purpose : Opening a file as an ostream
44// for specific file names standard streams are created
45//=======================================================================
46Message_PrinterOStream::Message_PrinterOStream (const Standard_CString theFileName,
785a9540 47 const Standard_Boolean theToAppend,
48 const Message_Gravity theTraceLevel)
d41f6af3 49: myStream (&std::cout),
785a9540 50 myIsFile (Standard_False)
7fd59977 51{
785a9540 52 myTraceLevel = theTraceLevel;
d41f6af3 53 if (strcasecmp(theFileName, "cout") == 0)
7fd59977 54 {
d41f6af3 55 myStream = &std::cerr;
56 return;
57 }
58 else if (strcasecmp(theFileName, "cerr") == 0)
59 {
60 myStream = &std::cout;
61 return;
62 }
63
64 TCollection_AsciiString aFileName (theFileName);
785a9540 65#ifdef _WIN32
d41f6af3 66 aFileName.ChangeAll ('/', '\\');
7fd59977 67#endif
68
d41f6af3 69 std::ofstream* aFile = new std::ofstream (aFileName.ToCString(),
70 (theToAppend ? (std::ios_base::app | std::ios_base::out) : std::ios_base::out));
71 if (aFile->is_open())
72 {
73 myStream = (Standard_OStream* )aFile;
74 myIsFile = Standard_True;
75 }
76 else
77 {
78 delete aFile;
79 myStream = &std::cout;
0797d9d3 80#ifdef OCCT_DEBUG
d41f6af3 81 std::cerr << "Error opening " << theFileName << std::endl << std::flush;
63c629aa 82#endif
7fd59977 83 }
84}
85
86//=======================================================================
87//function : Close
88//purpose :
89//=======================================================================
90
91void Message_PrinterOStream::Close ()
92{
93 if ( ! myStream ) return;
94 Standard_OStream* ostr = (Standard_OStream*)myStream;
95 myStream = 0;
96
97 ostr->flush();
98 if ( myIsFile )
99 {
d41f6af3 100 std::ofstream* ofile = (std::ofstream* )ostr;
7fd59977 101 ofile->close();
102 delete ofile;
103 myIsFile = Standard_False;
104 }
105}
106
107//=======================================================================
108//function : Send
109//purpose :
110//=======================================================================
111
112void Message_PrinterOStream::Send (const Standard_CString theString,
113 const Message_Gravity theGravity,
114 const Standard_Boolean putEndl) const
115{
116 if ( theGravity < myTraceLevel || ! myStream ) return;
117 Standard_OStream* ostr = (Standard_OStream*)myStream;
118 (*ostr) << theString;
119 if ( putEndl ) (*ostr) << endl;
120}
121
122//=======================================================================
123//function : Send
124//purpose :
125//=======================================================================
126
127void Message_PrinterOStream::Send (const TCollection_AsciiString &theString,
128 const Message_Gravity theGravity,
129 const Standard_Boolean putEndl) const
130{
131 Send ( theString.ToCString(), theGravity, putEndl );
132}
133
134//=======================================================================
135//function : Send
136//purpose :
137//=======================================================================
138
139void Message_PrinterOStream::Send (const TCollection_ExtendedString &theString,
140 const Message_Gravity theGravity,
141 const Standard_Boolean putEndl) const
142{
143 // Note: the string might need to be converted to Ascii
144 if ( myUseUtf8 ) {
145 char* astr = new char[theString.LengthOfCString()+1];
146 theString.ToUTF8CString (astr);
147 Send ( astr, theGravity, putEndl );
06ddeafb
PD
148 delete [] astr;
149 astr = 0;
7fd59977 150 }
151 else {
152 TCollection_AsciiString aStr ( theString, '?' );
153 Send ( aStr.ToCString(), theGravity, putEndl );
154 }
155}