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.
16 #include <Message_PrinterOStream.hxx>
18 #include <OSD_OpenFile.hxx>
19 #include <TCollection_AsciiString.hxx>
20 #include <TCollection_ExtendedString.hxx>
22 IMPLEMENT_STANDARD_RTTIEXT(Message_PrinterOStream,Message_Printer)
24 #if !defined(_MSC_VER)
28 //=======================================================================
29 //function : Constructor
30 //purpose : Empty constructor, defaulting to cerr
31 //=======================================================================
32 Message_PrinterOStream::Message_PrinterOStream (const Message_Gravity theTraceLevel)
33 : myStream (&std::cout),
34 myIsFile (Standard_False),
35 myUseUtf8 (Standard_False)
37 myTraceLevel = theTraceLevel;
40 //=======================================================================
41 //function : Constructor
42 //purpose : Opening a file as an std::ostream
43 // for specific file names standard streams are created
44 //=======================================================================
45 Message_PrinterOStream::Message_PrinterOStream (const Standard_CString theFileName,
46 const Standard_Boolean theToAppend,
47 const Message_Gravity theTraceLevel)
48 : myStream (&std::cout),
49 myIsFile (Standard_False)
51 myTraceLevel = theTraceLevel;
52 if (strcasecmp(theFileName, "cerr") == 0)
54 myStream = &std::cerr;
57 else if (strcasecmp(theFileName, "cout") == 0)
59 myStream = &std::cout;
63 TCollection_AsciiString aFileName (theFileName);
65 aFileName.ChangeAll ('/', '\\');
68 std::ofstream* aFile = new std::ofstream();
69 OSD_OpenStream (*aFile, aFileName.ToCString(), (theToAppend ? (std::ios_base::app | std::ios_base::out) : std::ios_base::out));
72 myStream = (Standard_OStream* )aFile;
73 myIsFile = Standard_True;
78 myStream = &std::cout;
80 std::cerr << "Error opening " << theFileName << std::endl << std::flush;
85 //=======================================================================
88 //=======================================================================
90 void Message_PrinterOStream::Close ()
92 if ( ! myStream ) return;
93 Standard_OStream* ostr = (Standard_OStream*)myStream;
99 std::ofstream* ofile = (std::ofstream* )ostr;
102 myIsFile = Standard_False;
106 //=======================================================================
109 //=======================================================================
111 void Message_PrinterOStream::Send (const Standard_CString theString,
112 const Message_Gravity theGravity,
113 const Standard_Boolean putEndl) const
115 if ( theGravity < myTraceLevel || ! myStream ) return;
116 Standard_OStream* ostr = (Standard_OStream*)myStream;
117 (*ostr) << theString;
118 if ( putEndl ) (*ostr) << std::endl;
121 //=======================================================================
124 //=======================================================================
126 void Message_PrinterOStream::Send (const TCollection_AsciiString &theString,
127 const Message_Gravity theGravity,
128 const Standard_Boolean putEndl) const
130 Send ( theString.ToCString(), theGravity, putEndl );
133 //=======================================================================
136 //=======================================================================
138 void Message_PrinterOStream::Send (const TCollection_ExtendedString &theString,
139 const Message_Gravity theGravity,
140 const Standard_Boolean putEndl) const
142 TCollection_AsciiString aStr (theString, myUseUtf8 ? Standard_Character(0) : '?');
143 Send (aStr.ToCString(), theGravity, putEndl);