| 1 | // Created on: 2001-01-06 |
| 2 | // Created by: OCC Team |
| 3 | // Copyright (c) 2001-2014 OPEN CASCADE SAS |
| 4 | // |
| 5 | // This file is part of Open CASCADE Technology software library. |
| 6 | // |
| 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. |
| 12 | // |
| 13 | // Alternatively, this file may be used under the terms of Open CASCADE |
| 14 | // commercial license or contractual agreement. |
| 15 | |
| 16 | #include <Message_PrinterOStream.ixx> |
| 17 | |
| 18 | #include <Message_Gravity.hxx> |
| 19 | #include <TCollection_AsciiString.hxx> |
| 20 | #include <TCollection_ExtendedString.hxx> |
| 21 | #include <Standard_Mutex.hxx> |
| 22 | #include <Standard_Stream.hxx> |
| 23 | |
| 24 | //======================================================================= |
| 25 | //function : Constructor |
| 26 | //purpose : Empty constructor, defaulting to cerr |
| 27 | //======================================================================= |
| 28 | |
| 29 | Message_PrinterOStream::Message_PrinterOStream (const Message_Gravity theTraceLevel) |
| 30 | : myStream (&cout), |
| 31 | myIsFile (Standard_False), |
| 32 | myUseUtf8 (Standard_False) |
| 33 | { |
| 34 | myTraceLevel = theTraceLevel; |
| 35 | } |
| 36 | |
| 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 (&cout), |
| 46 | myIsFile (Standard_False) |
| 47 | { |
| 48 | myTraceLevel = theTraceLevel; |
| 49 | if ( strcasecmp(theFileName, "cout") == 0 ) |
| 50 | myStream = &cerr; |
| 51 | else if ( strcasecmp(theFileName, "cerr") == 0 ) |
| 52 | myStream = &cout; |
| 53 | else |
| 54 | { |
| 55 | TCollection_AsciiString aFileName (theFileName); |
| 56 | #ifdef _WIN32 |
| 57 | aFileName.ChangeAll ('/', '\\'); |
| 58 | #endif |
| 59 | |
| 60 | ofstream *ofile = new ofstream (aFileName.ToCString(), |
| 61 | #ifdef USE_STL_STREAMS |
| 62 | (theToAppend ? (std::ios_base::app | std::ios_base::out) : std::ios_base::out ) ); |
| 63 | #else |
| 64 | (theToAppend ? ios::app : ios::out ) ); |
| 65 | #endif |
| 66 | if ( ofile ) { |
| 67 | myStream = (Standard_OStream*)ofile; |
| 68 | myIsFile = Standard_True; |
| 69 | } |
| 70 | else { |
| 71 | myStream = &cout; |
| 72 | cerr << "Error opening " << theFileName << endl << flush; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | //======================================================================= |
| 78 | //function : Close |
| 79 | //purpose : |
| 80 | //======================================================================= |
| 81 | |
| 82 | void Message_PrinterOStream::Close () |
| 83 | { |
| 84 | if ( ! myStream ) return; |
| 85 | Standard_OStream* ostr = (Standard_OStream*)myStream; |
| 86 | myStream = 0; |
| 87 | |
| 88 | ostr->flush(); |
| 89 | if ( myIsFile ) |
| 90 | { |
| 91 | ofstream* ofile = (ofstream*)ostr; |
| 92 | ofile->close(); |
| 93 | delete ofile; |
| 94 | myIsFile = Standard_False; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | //======================================================================= |
| 99 | //function : Send |
| 100 | //purpose : |
| 101 | //======================================================================= |
| 102 | |
| 103 | void Message_PrinterOStream::Send (const Standard_CString theString, |
| 104 | const Message_Gravity theGravity, |
| 105 | const Standard_Boolean putEndl) const |
| 106 | { |
| 107 | if ( theGravity < myTraceLevel || ! myStream ) return; |
| 108 | Standard_OStream* ostr = (Standard_OStream*)myStream; |
| 109 | (*ostr) << theString; |
| 110 | if ( putEndl ) (*ostr) << endl; |
| 111 | } |
| 112 | |
| 113 | //======================================================================= |
| 114 | //function : Send |
| 115 | //purpose : |
| 116 | //======================================================================= |
| 117 | |
| 118 | void Message_PrinterOStream::Send (const TCollection_AsciiString &theString, |
| 119 | const Message_Gravity theGravity, |
| 120 | const Standard_Boolean putEndl) const |
| 121 | { |
| 122 | Send ( theString.ToCString(), theGravity, putEndl ); |
| 123 | } |
| 124 | |
| 125 | //======================================================================= |
| 126 | //function : Send |
| 127 | //purpose : |
| 128 | //======================================================================= |
| 129 | |
| 130 | void Message_PrinterOStream::Send (const TCollection_ExtendedString &theString, |
| 131 | const Message_Gravity theGravity, |
| 132 | const Standard_Boolean putEndl) const |
| 133 | { |
| 134 | // Note: the string might need to be converted to Ascii |
| 135 | if ( myUseUtf8 ) { |
| 136 | char* astr = new char[theString.LengthOfCString()+1]; |
| 137 | theString.ToUTF8CString (astr); |
| 138 | Send ( astr, theGravity, putEndl ); |
| 139 | delete [] astr; |
| 140 | astr = 0; |
| 141 | } |
| 142 | else { |
| 143 | TCollection_AsciiString aStr ( theString, '?' ); |
| 144 | Send ( aStr.ToCString(), theGravity, putEndl ); |
| 145 | } |
| 146 | } |