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 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 // Kris
11 // 07.08.03 -- Move into Teuchos package/namespace
12 
13 #include "Teuchos_Object.hpp"
14 
15 namespace Teuchos {
16 
17 // Set TracebackMode value to default.
18 int Object::tracebackMode = -1;
19 
20 Object::Object (int tracebackModeIn)
21 {
22  tracebackMode = (tracebackModeIn != -1) ? tracebackModeIn : tracebackMode;
23 }
24 
25 Object::Object (const char* label_in, int tracebackModeIn) :
26  label_ (label_in) // this does a deep copy of the input string
27 {
28  tracebackMode = (tracebackModeIn != -1) ? tracebackModeIn : tracebackMode;
29 }
30 
31 Object::Object (const std::string& label_in, int tracebackModeIn) :
32  label_ (label_in)
33 {
34  tracebackMode = (tracebackModeIn != -1) ? tracebackModeIn : tracebackMode;
35 }
36 
37 void Object::setLabel (const char* theLabel) {
38  label_ = std::string (theLabel);
39 }
40 
41 void Object::setTracebackMode (int tracebackModeValue)
42 {
43  if (tracebackModeValue < 0) {
44  tracebackModeValue = 0;
45  }
46  Object tempObject (tracebackModeValue);
47 }
48 
50 {
51  int temp = Object::tracebackMode;
52  if (temp == -1) {
53  temp = Teuchos_DefaultTracebackMode;
54  }
55  return(temp);
56 }
57 
58 void Object::print (std::ostream& /* os */) const
59 {
60  // os << label_; // No need to print label, since std::ostream does it already
61 }
62 
63 int Object::reportError (const std::string message, int errorCode) const
64 {
65  using std::cerr;
66  using std::endl;
67 
68  // mfh 23 Nov 2014: I found the following message here:
69  //
70  // NOTE: We are extracting a C-style std::string from Message because
71  // the SGI compiler does not have a real std::string class with
72  // the << operator. Some day we should get rid of ".c_str()"
73  //
74  // All the compilers we support now have a correct implementation of
75  // std::string, so I've corrected the code below to use std::string
76  // instead.
77 
78  if (tracebackMode == 1 && errorCode < 0) {
79  // Report fatal error
80  cerr << endl << "Error in Teuchos Object with label: " << label_
81  << endl << "Teuchos Error: " << message << " Error Code: "
82  << errorCode << endl;
83  return errorCode;
84  }
85  if (tracebackMode == 2 && errorCode != 0) {
86  cerr << endl << "Error in Teuchos Object with label: " << label_
87  << endl << "Teuchos Error: " << message << " Error Code: "
88  << errorCode << endl;
89  return errorCode;
90  }
91  return errorCode;
92 }
93 
94 const char* Object::label () const
95 {
96  return label_.c_str ();
97 }
98 
99 std::ostream& operator<< (std::ostream& os, const Teuchos::Object& obj)
100 {
101  os << obj.label () << std::endl;
102  obj.print (os);
103  return os;
104 }
105 
106 } // 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.