1 // File: Message_PrinterOStream.cxx
2 // Created: Sat Jan 6 20:01:38 2001
4 // Copyright: Open CASCADE S.A. 2005
6 #include <Message_PrinterOStream.ixx>
8 #include <Message_Gravity.hxx>
9 #include <TCollection_AsciiString.hxx>
10 #include <TCollection_ExtendedString.hxx>
11 #include <Standard_Mutex.hxx>
12 #include <Standard_Stream.hxx>
14 //=======================================================================
15 //function : Constructor
16 //purpose : Empty constructor, defaulting to cerr
17 //=======================================================================
19 Message_PrinterOStream::Message_PrinterOStream (const Message_Gravity theTraceLevel)
20 : myTraceLevel(theTraceLevel), myStream(&cout),
21 myIsFile(Standard_False), myUseUtf8(Standard_False)
25 //=======================================================================
26 //function : Constructor
27 //purpose : Opening a file as an ostream
28 // for specific file names standard streams are created
29 //=======================================================================
30 Message_PrinterOStream::Message_PrinterOStream (const Standard_CString theFileName,
31 const Standard_Boolean doAppend,
32 const Message_Gravity theTraceLevel)
33 : myTraceLevel(theTraceLevel), myStream(&cout), myIsFile(Standard_False)
35 if ( strcasecmp(theFileName, "cout") == 0 )
37 else if ( strcasecmp(theFileName, "cerr") == 0 )
41 TCollection_AsciiString aFileName (theFileName);
43 aFileName.ChangeAll ('/', '\\');
46 ofstream *ofile = new ofstream (aFileName.ToCString(),
47 #ifdef USE_STL_STREAMS
48 (doAppend ? (std::ios_base::app | std::ios_base::out) : std::ios_base::out ) );
50 (doAppend ? ios::app : ios::out ) );
53 myStream = (Standard_OStream*)ofile;
54 myIsFile = Standard_True;
58 cerr << "Error opening " << theFileName << endl << flush;
63 //=======================================================================
66 //=======================================================================
68 void Message_PrinterOStream::Close ()
70 if ( ! myStream ) return;
71 Standard_OStream* ostr = (Standard_OStream*)myStream;
77 ofstream* ofile = (ofstream*)ostr;
80 myIsFile = Standard_False;
84 //=======================================================================
87 //=======================================================================
89 void Message_PrinterOStream::Send (const Standard_CString theString,
90 const Message_Gravity theGravity,
91 const Standard_Boolean putEndl) const
93 if ( theGravity < myTraceLevel || ! myStream ) return;
94 Standard_OStream* ostr = (Standard_OStream*)myStream;
96 if ( putEndl ) (*ostr) << endl;
99 //=======================================================================
102 //=======================================================================
104 void Message_PrinterOStream::Send (const TCollection_AsciiString &theString,
105 const Message_Gravity theGravity,
106 const Standard_Boolean putEndl) const
108 Send ( theString.ToCString(), theGravity, putEndl );
111 //=======================================================================
114 //=======================================================================
116 void Message_PrinterOStream::Send (const TCollection_ExtendedString &theString,
117 const Message_Gravity theGravity,
118 const Standard_Boolean putEndl) const
120 // Note: the string might need to be converted to Ascii
122 char* astr = new char[theString.LengthOfCString()+1];
123 theString.ToUTF8CString (astr);
124 Send ( astr, theGravity, putEndl );
129 TCollection_AsciiString aStr ( theString, '?' );
130 Send ( aStr.ToCString(), theGravity, putEndl );