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