Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_Object.cpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 // Kris
43 // 07.08.03 -- Move into Teuchos package/namespace
44 
45 #include "Teuchos_Object.hpp"
46 
47 namespace Teuchos {
48 
49 // Set TracebackMode value to default.
50 int Object::tracebackMode = -1;
51 
52 Object::Object (int tracebackModeIn)
53 {
54  tracebackMode = (tracebackModeIn != -1) ? tracebackModeIn : tracebackMode;
55 }
56 
57 Object::Object (const char* label_in, int tracebackModeIn) :
58  label_ (label_in) // this does a deep copy of the input string
59 {
60  tracebackMode = (tracebackModeIn != -1) ? tracebackModeIn : tracebackMode;
61 }
62 
63 Object::Object (const std::string& label_in, int tracebackModeIn) :
64  label_ (label_in)
65 {
66  tracebackMode = (tracebackModeIn != -1) ? tracebackModeIn : tracebackMode;
67 }
68 
69 void Object::setLabel (const char* theLabel) {
70  label_ = std::string (theLabel);
71 }
72 
73 void Object::setTracebackMode (int tracebackModeValue)
74 {
75  if (tracebackModeValue < 0) {
76  tracebackModeValue = 0;
77  }
78  Object tempObject (tracebackModeValue);
79 }
80 
82 {
83  int temp = Object::tracebackMode;
84  if (temp == -1) {
85  temp = Teuchos_DefaultTracebackMode;
86  }
87  return(temp);
88 }
89 
90 void Object::print (std::ostream& /* os */) const
91 {
92  // os << label_; // No need to print label, since std::ostream does it already
93 }
94 
95 int Object::reportError (const std::string message, int errorCode) const
96 {
97  using std::cerr;
98  using std::endl;
99 
100  // mfh 23 Nov 2014: I found the following message here:
101  //
102  // NOTE: We are extracting a C-style std::string from Message because
103  // the SGI compiler does not have a real std::string class with
104  // the << operator. Some day we should get rid of ".c_str()"
105  //
106  // All the compilers we support now have a correct implementation of
107  // std::string, so I've corrected the code below to use std::string
108  // instead.
109 
110  if (tracebackMode == 1 && errorCode < 0) {
111  // Report fatal error
112  cerr << endl << "Error in Teuchos Object with label: " << label_
113  << endl << "Teuchos Error: " << message << " Error Code: "
114  << errorCode << endl;
115  return errorCode;
116  }
117  if (tracebackMode == 2 && errorCode != 0) {
118  cerr << endl << "Error in Teuchos Object with label: " << label_
119  << endl << "Teuchos Error: " << message << " Error Code: "
120  << errorCode << endl;
121  return errorCode;
122  }
123  return errorCode;
124 }
125 
126 const char* Object::label () const
127 {
128  return label_.c_str ();
129 }
130 
131 std::ostream& operator<< (std::ostream& os, const Teuchos::Object& obj)
132 {
133  os << obj.label () << std::endl;
134  obj.print (os);
135  return os;
136 }
137 
138 } // namespace Teuchos
virtual const char * label() const
Access the object&#39;s label (LEGACY; return std::string instead).
virtual void print(std::ostream &os) const
Print the object to the given output stream.
The base Teuchos class.
Object(int tracebackModeIn=-1)
Default Constructor.
virtual int reportError(const std::string message, int errorCode) const
Report an error with this Object.
static int getTracebackMode()
Get the value of the Object error traceback report mode.
The base Teuchos object.
static void setTracebackMode(int tracebackModeValue)
Set the value of the Object error traceback report mode.