0024696: Lower performance of the new Edge/Edge intersection algorithm
[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
16#include <Message_PrinterOStream.ixx>
17
18#include <Message_Gravity.hxx>
19#include <TCollection_AsciiString.hxx>
20#include <TCollection_ExtendedString.hxx>
21#include <Standard_Mutex.hxx>
22#include <Standard_Stream.hxx>
23
24//=======================================================================
25//function : Constructor
26//purpose : Empty constructor, defaulting to cerr
27//=======================================================================
28
29Message_PrinterOStream::Message_PrinterOStream (const Message_Gravity theTraceLevel)
30: myTraceLevel(theTraceLevel), myStream(&cout),
31 myIsFile(Standard_False), myUseUtf8(Standard_False)
32{
33}
34
35//=======================================================================
36//function : Constructor
37//purpose : Opening a file as an ostream
38// for specific file names standard streams are created
39//=======================================================================
40Message_PrinterOStream::Message_PrinterOStream (const Standard_CString theFileName,
41 const Standard_Boolean doAppend,
42 const Message_Gravity theTraceLevel)
43: myTraceLevel(theTraceLevel), myStream(&cout), myIsFile(Standard_False)
44{
45 if ( strcasecmp(theFileName, "cout") == 0 )
46 myStream = &cerr;
47 else if ( strcasecmp(theFileName, "cerr") == 0 )
48 myStream = &cout;
49 else
50 {
51 TCollection_AsciiString aFileName (theFileName);
52#ifdef WNT
53 aFileName.ChangeAll ('/', '\\');
54#endif
55
56 ofstream *ofile = new ofstream (aFileName.ToCString(),
57#ifdef USE_STL_STREAMS
58 (doAppend ? (std::ios_base::app | std::ios_base::out) : std::ios_base::out ) );
59#else
60 (doAppend ? ios::app : ios::out ) );
61#endif
62 if ( ofile ) {
63 myStream = (Standard_OStream*)ofile;
64 myIsFile = Standard_True;
65 }
66 else {
67 myStream = &cout;
68 cerr << "Error opening " << theFileName << endl << flush;
69 }
70 }
71}
72
73//=======================================================================
74//function : Close
75//purpose :
76//=======================================================================
77
78void Message_PrinterOStream::Close ()
79{
80 if ( ! myStream ) return;
81 Standard_OStream* ostr = (Standard_OStream*)myStream;
82 myStream = 0;
83
84 ostr->flush();
85 if ( myIsFile )
86 {
87 ofstream* ofile = (ofstream*)ostr;
88 ofile->close();
89 delete ofile;
90 myIsFile = Standard_False;
91 }
92}
93
94//=======================================================================
95//function : Send
96//purpose :
97//=======================================================================
98
99void Message_PrinterOStream::Send (const Standard_CString theString,
100 const Message_Gravity theGravity,
101 const Standard_Boolean putEndl) const
102{
103 if ( theGravity < myTraceLevel || ! myStream ) return;
104 Standard_OStream* ostr = (Standard_OStream*)myStream;
105 (*ostr) << theString;
106 if ( putEndl ) (*ostr) << endl;
107}
108
109//=======================================================================
110//function : Send
111//purpose :
112//=======================================================================
113
114void Message_PrinterOStream::Send (const TCollection_AsciiString &theString,
115 const Message_Gravity theGravity,
116 const Standard_Boolean putEndl) const
117{
118 Send ( theString.ToCString(), theGravity, putEndl );
119}
120
121//=======================================================================
122//function : Send
123//purpose :
124//=======================================================================
125
126void Message_PrinterOStream::Send (const TCollection_ExtendedString &theString,
127 const Message_Gravity theGravity,
128 const Standard_Boolean putEndl) const
129{
130 // Note: the string might need to be converted to Ascii
131 if ( myUseUtf8 ) {
132 char* astr = new char[theString.LengthOfCString()+1];
133 theString.ToUTF8CString (astr);
134 Send ( astr, theGravity, putEndl );
06ddeafb
PD
135 delete [] astr;
136 astr = 0;
7fd59977 137 }
138 else {
139 TCollection_AsciiString aStr ( theString, '?' );
140 Send ( aStr.ToCString(), theGravity, putEndl );
141 }
142}