0024166: Unable to create file with "Save" menu of voxeldemo Qt sample
[occt.git] / src / Message / Message_PrinterOStream.cxx
CommitLineData
b311480e 1// Created on: 2001-01-06
2// Created by: OCC Team
3// Copyright (c) 2001-2012 OPEN CASCADE SAS
4//
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.
9//
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.
12//
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.
19
7fd59977 20
21#include <Message_PrinterOStream.ixx>
22
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>
28
29//=======================================================================
30//function : Constructor
31//purpose : Empty constructor, defaulting to cerr
32//=======================================================================
33
34Message_PrinterOStream::Message_PrinterOStream (const Message_Gravity theTraceLevel)
35: myTraceLevel(theTraceLevel), myStream(&cout),
36 myIsFile(Standard_False), myUseUtf8(Standard_False)
37{
38}
39
40//=======================================================================
41//function : Constructor
42//purpose : Opening a file as an ostream
43// for specific file names standard streams are created
44//=======================================================================
45Message_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)
49{
50 if ( strcasecmp(theFileName, "cout") == 0 )
51 myStream = &cerr;
52 else if ( strcasecmp(theFileName, "cerr") == 0 )
53 myStream = &cout;
54 else
55 {
56 TCollection_AsciiString aFileName (theFileName);
57#ifdef WNT
58 aFileName.ChangeAll ('/', '\\');
59#endif
60
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 ) );
64#else
65 (doAppend ? ios::app : ios::out ) );
66#endif
67 if ( ofile ) {
68 myStream = (Standard_OStream*)ofile;
69 myIsFile = Standard_True;
70 }
71 else {
72 myStream = &cout;
73 cerr << "Error opening " << theFileName << endl << flush;
74 }
75 }
76}
77
78//=======================================================================
79//function : Close
80//purpose :
81//=======================================================================
82
83void Message_PrinterOStream::Close ()
84{
85 if ( ! myStream ) return;
86 Standard_OStream* ostr = (Standard_OStream*)myStream;
87 myStream = 0;
88
89 ostr->flush();
90 if ( myIsFile )
91 {
92 ofstream* ofile = (ofstream*)ostr;
93 ofile->close();
94 delete ofile;
95 myIsFile = Standard_False;
96 }
97}
98
99//=======================================================================
100//function : Send
101//purpose :
102//=======================================================================
103
104void Message_PrinterOStream::Send (const Standard_CString theString,
105 const Message_Gravity theGravity,
106 const Standard_Boolean putEndl) const
107{
108 if ( theGravity < myTraceLevel || ! myStream ) return;
109 Standard_OStream* ostr = (Standard_OStream*)myStream;
110 (*ostr) << theString;
111 if ( putEndl ) (*ostr) << endl;
112}
113
114//=======================================================================
115//function : Send
116//purpose :
117//=======================================================================
118
119void Message_PrinterOStream::Send (const TCollection_AsciiString &theString,
120 const Message_Gravity theGravity,
121 const Standard_Boolean putEndl) const
122{
123 Send ( theString.ToCString(), theGravity, putEndl );
124}
125
126//=======================================================================
127//function : Send
128//purpose :
129//=======================================================================
130
131void Message_PrinterOStream::Send (const TCollection_ExtendedString &theString,
132 const Message_Gravity theGravity,
133 const Standard_Boolean putEndl) const
134{
135 // Note: the string might need to be converted to Ascii
136 if ( myUseUtf8 ) {
137 char* astr = new char[theString.LengthOfCString()+1];
138 theString.ToUTF8CString (astr);
139 Send ( astr, theGravity, putEndl );
06ddeafb
PD
140 delete [] astr;
141 astr = 0;
7fd59977 142 }
143 else {
144 TCollection_AsciiString aStr ( theString, '?' );
145 Send ( aStr.ToCString(), theGravity, putEndl );
146 }
147}