1 // Created on: 2001-01-06
2 // Created by: OCC Team
3 // Copyright (c) 2001-2012 OPEN CASCADE SAS
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
21 #include <Message_PrinterOStream.ixx>
23 #include <Message_Gravity.hxx>
24 #include <TCollection_AsciiString.hxx>
25 #include <TCollection_ExtendedString.hxx>
26 #include <Standard_Mutex.hxx>
27 #include <Standard_Stream.hxx>
29 //=======================================================================
30 //function : Constructor
31 //purpose : Empty constructor, defaulting to cerr
32 //=======================================================================
34 Message_PrinterOStream::Message_PrinterOStream (const Message_Gravity theTraceLevel)
35 : myTraceLevel(theTraceLevel), myStream(&cout),
36 myIsFile(Standard_False), myUseUtf8(Standard_False)
40 //=======================================================================
41 //function : Constructor
42 //purpose : Opening a file as an ostream
43 // for specific file names standard streams are created
44 //=======================================================================
45 Message_PrinterOStream::Message_PrinterOStream (const Standard_CString theFileName,
46 const Standard_Boolean doAppend,
47 const Message_Gravity theTraceLevel)
48 : myTraceLevel(theTraceLevel), myStream(&cout), myIsFile(Standard_False)
50 if ( strcasecmp(theFileName, "cout") == 0 )
52 else if ( strcasecmp(theFileName, "cerr") == 0 )
56 TCollection_AsciiString aFileName (theFileName);
58 aFileName.ChangeAll ('/', '\\');
61 ofstream *ofile = new ofstream (aFileName.ToCString(),
62 #ifdef USE_STL_STREAMS
63 (doAppend ? (std::ios_base::app | std::ios_base::out) : std::ios_base::out ) );
65 (doAppend ? ios::app : ios::out ) );
68 myStream = (Standard_OStream*)ofile;
69 myIsFile = Standard_True;
73 cerr << "Error opening " << theFileName << endl << flush;
78 //=======================================================================
81 //=======================================================================
83 void Message_PrinterOStream::Close ()
85 if ( ! myStream ) return;
86 Standard_OStream* ostr = (Standard_OStream*)myStream;
92 ofstream* ofile = (ofstream*)ostr;
95 myIsFile = Standard_False;
99 //=======================================================================
102 //=======================================================================
104 void Message_PrinterOStream::Send (const Standard_CString theString,
105 const Message_Gravity theGravity,
106 const Standard_Boolean putEndl) const
108 if ( theGravity < myTraceLevel || ! myStream ) return;
109 Standard_OStream* ostr = (Standard_OStream*)myStream;
110 (*ostr) << theString;
111 if ( putEndl ) (*ostr) << endl;
114 //=======================================================================
117 //=======================================================================
119 void Message_PrinterOStream::Send (const TCollection_AsciiString &theString,
120 const Message_Gravity theGravity,
121 const Standard_Boolean putEndl) const
123 Send ( theString.ToCString(), theGravity, putEndl );
126 //=======================================================================
129 //=======================================================================
131 void Message_PrinterOStream::Send (const TCollection_ExtendedString &theString,
132 const Message_Gravity theGravity,
133 const Standard_Boolean putEndl) const
135 // Note: the string might need to be converted to Ascii
137 char* astr = new char[theString.LengthOfCString()+1];
138 theString.ToUTF8CString (astr);
139 Send ( astr, theGravity, putEndl );
144 TCollection_AsciiString aStr ( theString, '?' );
145 Send ( aStr.ToCString(), theGravity, putEndl );