0028923: Foundation Classes - Message_Messenger::Send() implementation is not thread...
[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
42cf5bc1 16#include <Message_PrinterOStream.hxx>
983fd6c0 17
18#include <OSD_OpenFile.hxx>
42cf5bc1 19#include <TCollection_AsciiString.hxx>
20#include <TCollection_ExtendedString.hxx>
7fd59977 21
92efcf78 22IMPLEMENT_STANDARD_RTTIEXT(Message_PrinterOStream,Message_Printer)
23
7c65581d 24#if !defined(_MSC_VER)
d8d01f6e 25 #include <strings.h>
26#endif
27
7fd59977 28//=======================================================================
29//function : Constructor
30//purpose : Empty constructor, defaulting to cerr
31//=======================================================================
785a9540 32Message_PrinterOStream::Message_PrinterOStream (const Message_Gravity theTraceLevel)
d41f6af3 33: myStream (&std::cout),
785a9540 34 myIsFile (Standard_False),
35 myUseUtf8 (Standard_False)
7fd59977 36{
785a9540 37 myTraceLevel = theTraceLevel;
7fd59977 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,
785a9540 46 const Standard_Boolean theToAppend,
47 const Message_Gravity theTraceLevel)
d41f6af3 48: myStream (&std::cout),
785a9540 49 myIsFile (Standard_False)
7fd59977 50{
785a9540 51 myTraceLevel = theTraceLevel;
d41f6af3 52 if (strcasecmp(theFileName, "cout") == 0)
7fd59977 53 {
d41f6af3 54 myStream = &std::cerr;
55 return;
56 }
57 else if (strcasecmp(theFileName, "cerr") == 0)
58 {
59 myStream = &std::cout;
60 return;
61 }
62
63 TCollection_AsciiString aFileName (theFileName);
785a9540 64#ifdef _WIN32
d41f6af3 65 aFileName.ChangeAll ('/', '\\');
7fd59977 66#endif
67
983fd6c0 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));
d41f6af3 70 if (aFile->is_open())
71 {
72 myStream = (Standard_OStream* )aFile;
73 myIsFile = Standard_True;
74 }
75 else
76 {
77 delete aFile;
78 myStream = &std::cout;
0797d9d3 79#ifdef OCCT_DEBUG
d41f6af3 80 std::cerr << "Error opening " << theFileName << std::endl << std::flush;
63c629aa 81#endif
7fd59977 82 }
83}
84
85//=======================================================================
86//function : Close
87//purpose :
88//=======================================================================
89
90void Message_PrinterOStream::Close ()
91{
92 if ( ! myStream ) return;
93 Standard_OStream* ostr = (Standard_OStream*)myStream;
94 myStream = 0;
95
96 ostr->flush();
97 if ( myIsFile )
98 {
d41f6af3 99 std::ofstream* ofile = (std::ofstream* )ostr;
7fd59977 100 ofile->close();
101 delete ofile;
102 myIsFile = Standard_False;
103 }
104}
105
106//=======================================================================
107//function : Send
108//purpose :
109//=======================================================================
110
111void Message_PrinterOStream::Send (const Standard_CString theString,
112 const Message_Gravity theGravity,
113 const Standard_Boolean putEndl) const
114{
115 if ( theGravity < myTraceLevel || ! myStream ) return;
116 Standard_OStream* ostr = (Standard_OStream*)myStream;
117 (*ostr) << theString;
118 if ( putEndl ) (*ostr) << endl;
119}
120
121//=======================================================================
122//function : Send
123//purpose :
124//=======================================================================
125
126void Message_PrinterOStream::Send (const TCollection_AsciiString &theString,
127 const Message_Gravity theGravity,
128 const Standard_Boolean putEndl) const
129{
130 Send ( theString.ToCString(), theGravity, putEndl );
131}
132
133//=======================================================================
134//function : Send
135//purpose :
136//=======================================================================
137
138void Message_PrinterOStream::Send (const TCollection_ExtendedString &theString,
139 const Message_Gravity theGravity,
140 const Standard_Boolean putEndl) const
141{
983fd6c0 142 TCollection_AsciiString aStr (theString, myUseUtf8 ? Standard_Character(0) : '?');
143 Send (aStr.ToCString(), theGravity, putEndl);
7fd59977 144}