1 // Created on: 2001-01-06
2 // Created by: OCC Team
3 // Copyright (c) 2001-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
17 #include <Message_Gravity.hxx>
18 #include <Message_PrinterOStream.hxx>
19 #include <Standard_Mutex.hxx>
20 #include <Standard_Stream.hxx>
21 #include <Standard_Type.hxx>
22 #include <TCollection_AsciiString.hxx>
23 #include <TCollection_ExtendedString.hxx>
25 //=======================================================================
26 //function : Constructor
27 //purpose : Empty constructor, defaulting to cerr
28 //=======================================================================
29 Message_PrinterOStream::Message_PrinterOStream (const Message_Gravity theTraceLevel)
30 : myStream (&std::cout),
31 myIsFile (Standard_False),
32 myUseUtf8 (Standard_False)
34 myTraceLevel = theTraceLevel;
37 //=======================================================================
38 //function : Constructor
39 //purpose : Opening a file as an ostream
40 // for specific file names standard streams are created
41 //=======================================================================
42 Message_PrinterOStream::Message_PrinterOStream (const Standard_CString theFileName,
43 const Standard_Boolean theToAppend,
44 const Message_Gravity theTraceLevel)
45 : myStream (&std::cout),
46 myIsFile (Standard_False)
48 myTraceLevel = theTraceLevel;
49 if (strcasecmp(theFileName, "cout") == 0)
51 myStream = &std::cerr;
54 else if (strcasecmp(theFileName, "cerr") == 0)
56 myStream = &std::cout;
60 TCollection_AsciiString aFileName (theFileName);
62 aFileName.ChangeAll ('/', '\\');
65 std::ofstream* aFile = new std::ofstream (aFileName.ToCString(),
66 (theToAppend ? (std::ios_base::app | std::ios_base::out) : std::ios_base::out));
69 myStream = (Standard_OStream* )aFile;
70 myIsFile = Standard_True;
75 myStream = &std::cout;
77 std::cerr << "Error opening " << theFileName << std::endl << std::flush;
82 //=======================================================================
85 //=======================================================================
87 void Message_PrinterOStream::Close ()
89 if ( ! myStream ) return;
90 Standard_OStream* ostr = (Standard_OStream*)myStream;
96 std::ofstream* ofile = (std::ofstream* )ostr;
99 myIsFile = Standard_False;
103 //=======================================================================
106 //=======================================================================
108 void Message_PrinterOStream::Send (const Standard_CString theString,
109 const Message_Gravity theGravity,
110 const Standard_Boolean putEndl) const
112 if ( theGravity < myTraceLevel || ! myStream ) return;
113 Standard_OStream* ostr = (Standard_OStream*)myStream;
114 (*ostr) << theString;
115 if ( putEndl ) (*ostr) << endl;
118 //=======================================================================
121 //=======================================================================
123 void Message_PrinterOStream::Send (const TCollection_AsciiString &theString,
124 const Message_Gravity theGravity,
125 const Standard_Boolean putEndl) const
127 Send ( theString.ToCString(), theGravity, putEndl );
130 //=======================================================================
133 //=======================================================================
135 void Message_PrinterOStream::Send (const TCollection_ExtendedString &theString,
136 const Message_Gravity theGravity,
137 const Standard_Boolean putEndl) const
139 // Note: the string might need to be converted to Ascii
141 char* astr = new char[theString.LengthOfCString()+1];
142 theString.ToUTF8CString (astr);
143 Send ( astr, theGravity, putEndl );
148 TCollection_AsciiString aStr ( theString, '?' );
149 Send ( aStr.ToCString(), theGravity, putEndl );