Integration of OCCT 6.5.0 from SVN
[occt.git] / src / Message / Message_PrinterOStream.cxx
CommitLineData
7fd59977 1// File: Message_PrinterOStream.cxx
2// Created: Sat Jan 6 20:01:38 2001
3// Author: OCC Team
4// Copyright: Open CASCADE S.A. 2005
5
6#include <Message_PrinterOStream.ixx>
7
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>
13
14//=======================================================================
15//function : Constructor
16//purpose : Empty constructor, defaulting to cerr
17//=======================================================================
18
19Message_PrinterOStream::Message_PrinterOStream (const Message_Gravity theTraceLevel)
20: myTraceLevel(theTraceLevel), myStream(&cout),
21 myIsFile(Standard_False), myUseUtf8(Standard_False)
22{
23}
24
25//=======================================================================
26//function : Constructor
27//purpose : Opening a file as an ostream
28// for specific file names standard streams are created
29//=======================================================================
30Message_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)
34{
35 if ( strcasecmp(theFileName, "cout") == 0 )
36 myStream = &cerr;
37 else if ( strcasecmp(theFileName, "cerr") == 0 )
38 myStream = &cout;
39 else
40 {
41 TCollection_AsciiString aFileName (theFileName);
42#ifdef WNT
43 aFileName.ChangeAll ('/', '\\');
44#endif
45
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 ) );
49#else
50 (doAppend ? ios::app : ios::out ) );
51#endif
52 if ( ofile ) {
53 myStream = (Standard_OStream*)ofile;
54 myIsFile = Standard_True;
55 }
56 else {
57 myStream = &cout;
58 cerr << "Error opening " << theFileName << endl << flush;
59 }
60 }
61}
62
63//=======================================================================
64//function : Close
65//purpose :
66//=======================================================================
67
68void Message_PrinterOStream::Close ()
69{
70 if ( ! myStream ) return;
71 Standard_OStream* ostr = (Standard_OStream*)myStream;
72 myStream = 0;
73
74 ostr->flush();
75 if ( myIsFile )
76 {
77 ofstream* ofile = (ofstream*)ostr;
78 ofile->close();
79 delete ofile;
80 myIsFile = Standard_False;
81 }
82}
83
84//=======================================================================
85//function : Send
86//purpose :
87//=======================================================================
88
89void Message_PrinterOStream::Send (const Standard_CString theString,
90 const Message_Gravity theGravity,
91 const Standard_Boolean putEndl) const
92{
93 if ( theGravity < myTraceLevel || ! myStream ) return;
94 Standard_OStream* ostr = (Standard_OStream*)myStream;
95 (*ostr) << theString;
96 if ( putEndl ) (*ostr) << endl;
97}
98
99//=======================================================================
100//function : Send
101//purpose :
102//=======================================================================
103
104void Message_PrinterOStream::Send (const TCollection_AsciiString &theString,
105 const Message_Gravity theGravity,
106 const Standard_Boolean putEndl) const
107{
108 Send ( theString.ToCString(), theGravity, putEndl );
109}
110
111//=======================================================================
112//function : Send
113//purpose :
114//=======================================================================
115
116void Message_PrinterOStream::Send (const TCollection_ExtendedString &theString,
117 const Message_Gravity theGravity,
118 const Standard_Boolean putEndl) const
119{
120 // Note: the string might need to be converted to Ascii
121 if ( myUseUtf8 ) {
122 char* astr = new char[theString.LengthOfCString()+1];
123 theString.ToUTF8CString (astr);
124 Send ( astr, theGravity, putEndl );
125 delete astr;
126 }
127 else {
128 TCollection_AsciiString aStr ( theString, '?' );
129 Send ( aStr.ToCString(), theGravity, putEndl );
130 }
131}