0030895: Coding Rules - specify std namespace explicitly for std::cout and streams
[occt.git] / src / Message / Message_PrinterOStream.cxx
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.hxx>
17
18 #include <OSD_OpenFile.hxx>
19 #include <TCollection_AsciiString.hxx>
20 #include <TCollection_ExtendedString.hxx>
21
22 IMPLEMENT_STANDARD_RTTIEXT(Message_PrinterOStream,Message_Printer)
23
24 #if !defined(_MSC_VER)
25   #include <strings.h>
26 #endif
27
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)
36 {
37   myTraceLevel = theTraceLevel;
38 }
39
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)
50 {
51   myTraceLevel = theTraceLevel;
52   if (strcasecmp(theFileName, "cerr") == 0)
53   {
54     myStream = &std::cerr;
55     return;
56   }
57   else if (strcasecmp(theFileName, "cout") == 0)
58   {
59     myStream = &std::cout;
60     return;
61   }
62
63   TCollection_AsciiString aFileName (theFileName);
64 #ifdef _WIN32
65   aFileName.ChangeAll ('/', '\\');
66 #endif
67
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));
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;
79 #ifdef OCCT_DEBUG
80     std::cerr << "Error opening " << theFileName << std::endl << std::flush;
81 #endif
82   }
83 }
84
85 //=======================================================================
86 //function : Close
87 //purpose  : 
88 //=======================================================================
89
90 void 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   {
99     std::ofstream* ofile = (std::ofstream* )ostr;
100     ofile->close();
101     delete ofile;
102     myIsFile = Standard_False;
103   }
104 }
105
106 //=======================================================================
107 //function : Send
108 //purpose  : 
109 //=======================================================================
110
111 void 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) << std::endl;
119 }
120
121 //=======================================================================
122 //function : Send
123 //purpose  : 
124 //=======================================================================
125
126 void 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
138 void Message_PrinterOStream::Send (const TCollection_ExtendedString &theString,
139                                    const Message_Gravity theGravity,
140                                    const Standard_Boolean putEndl) const
141 {
142   TCollection_AsciiString aStr (theString, myUseUtf8 ? Standard_Character(0) : '?');
143   Send (aStr.ToCString(), theGravity, putEndl);
144 }