Branch data Line data Source code
1 : : /* *****************************************************************
2 : : MESQUITE -- The Mesh Quality Improvement Toolkit
3 : :
4 : : Copyright 2004 Sandia Corporation and Argonne National
5 : : Laboratory. Under the terms of Contract DE-AC04-94AL85000
6 : : with Sandia Corporation, the U.S. Government retains certain
7 : : rights in this software.
8 : :
9 : : This library is free software; you can redistribute it and/or
10 : : modify it under the terms of the GNU Lesser General Public
11 : : License as published by the Free Software Foundation; either
12 : : version 2.1 of the License, or (at your option) any later version.
13 : :
14 : : This library is distributed in the hope that it will be useful,
15 : : but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 : : Lesser General Public License for more details.
18 : :
19 : : You should have received a copy of the GNU Lesser General Public License
20 : : (lgpl.txt) along with this library; if not, write to the Free Software
21 : : Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 : :
23 : : ***************************************************************** */
24 : : /*!
25 : : \file MsqError.cpp
26 : : \brief Used to hold the error state and return it to the application.
27 : : \author Jason Kraftcheck
28 : : \date 2004-09-17
29 : : */
30 : :
31 : : #include "MsqError.hpp"
32 : : #include "Mesquite.hpp"
33 : :
34 : : #include <ostream>
35 : : #include <stdio.h>
36 : : #include <stdarg.h>
37 : : #include <assert.h>
38 : :
39 : : #include <cstring>
40 : : using std::strncpy;
41 : :
42 : : namespace MBMesquite
43 : : {
44 : :
45 : 0 : const char* MsqError::error_message() const
46 : : {
47 : : static const char* const error_messages[] = { "No Error",
48 : : "<unknown>",
49 : : "Out of memory",
50 : : "Invalid argument",
51 : : "Data not initialized",
52 : : "Invalid state",
53 : : "File access error",
54 : : "File format error",
55 : : "Syntax error",
56 : : "I/O error",
57 : : "Invalid mesh",
58 : : "No storage mode for PatchData",
59 : : "Not implemented",
60 : : "Internal error",
61 : : "Interrupted",
62 : : "Duplicate tag name",
63 : : "Tag not found",
64 : : "Unsupported element type",
65 : : "Parallel Error - error occurred on at least one processor",
66 : : "barruer violated when processing barrier Target Metric",
67 : : "Invalid Error Code" };
68 : :
69 : : /* If this is ever false, it should be caught by a unit test.
70 : : Do an assert here so the unit test fails.
71 : : This asserts that all error codes have a string in the above list. */
72 [ # # ]: 0 : assert( sizeof( error_messages ) == sizeof( char* ) * ( LAST_ERROR_CODE + 1 ) );
73 : :
74 [ # # ]: 0 : if( !errorMessage.empty() ) return errorMessage.c_str();
75 : :
76 [ # # ][ # # ]: 0 : if( errorCode >= 0 && errorCode < LAST_ERROR_CODE ) return error_messages[errorCode];
77 : :
78 : 0 : return error_messages[LAST_ERROR_CODE];
79 : : }
80 : :
81 [ - + ]: 52457898 : MsqError::~MsqError() {}
82 : :
83 : 1252 : bool MsqError::Setter::set( const std::string& msg, ErrorCode num )
84 : : {
85 [ + - ][ + - ]: 1252 : return mErr.set_error( num, msg.c_str() ) && mErr.push( functionName, fileName, lineNumber );
86 : : }
87 : :
88 : 40 : bool MsqError::Setter::set( const char* msg, ErrorCode num )
89 : : {
90 [ + - ][ + - ]: 40 : return mErr.set_error( num, msg ) && mErr.push( functionName, fileName, lineNumber );
91 : : }
92 : :
93 : 10 : bool MsqError::Setter::set( ErrorCode num )
94 : : {
95 [ + - ][ + - ]: 10 : return mErr.set_error( num ) && mErr.push( functionName, fileName, lineNumber );
96 : : }
97 : :
98 : 312 : bool MsqError::Setter::set( ErrorCode num, const char* format, ... )
99 : : {
100 : : char buffer[1024];
101 : :
102 : : #if defined( HAVE_VSNPRINTF )
103 : : va_list args;
104 : : va_start( args, format );
105 : : vsnprintf( buffer, sizeof( buffer ), format, args );
106 : : va_end( args );
107 : : #elif defined( HAVE__VSNPRINTF )
108 : : va_list args;
109 : : va_start( args, format );
110 : : _vsnprintf( buffer, sizeof( buffer ), format, args );
111 : : va_end( args );
112 : : #elif defined( HAVE_VSPRINTF )
113 : : va_list args;
114 : : va_start( args, format );
115 : : vsprintf( buffer, format, args );
116 : : va_end( args );
117 : : #else
118 : 312 : strncpy( buffer, format, sizeof( buffer ) );
119 : 312 : buffer[sizeof( buffer ) - 1] = '\0';
120 : : #endif
121 : :
122 [ + - ][ + - ]: 312 : return mErr.set_error( num, buffer ) && mErr.push( functionName, fileName, lineNumber );
[ + - ][ + - ]
123 : : }
124 : :
125 : 6654 : bool MsqError::push( const char* function, const char* file, int line )
126 : : {
127 [ + - ]: 6654 : stackTrace.push_back( Trace( function, file, line ) );
128 : 6654 : return true;
129 : : }
130 : :
131 : 101940 : bool MsqError::set_error( ErrorCode num, const char* msg )
132 : : {
133 : 101940 : errorCode = num;
134 : 101940 : stackTrace.clear();
135 : :
136 [ + + ]: 101940 : if( msg )
137 : 1604 : errorMessage = msg;
138 : : else
139 : : // MS VC6 doesn't have string::clear()!
140 : 100336 : errorMessage.resize( 0 );
141 : :
142 : 101940 : return num != NO_ERROR;
143 : : }
144 : :
145 : 1799 : void MsqError::clear()
146 : : {
147 : 1799 : errorCode = NO_ERROR;
148 : : // MS VC6 doesn't have string::clear()!
149 : 1799 : errorMessage.resize( 0 );
150 : 1799 : stackTrace.clear();
151 : 1799 : }
152 : :
153 : 0 : std::ostream& operator<<( std::ostream& str, const MsqError::Trace& tr )
154 : : {
155 : 0 : return ( str << tr.function << " at " << tr.file << ":" << tr.line );
156 : : }
157 : :
158 : 0 : std::ostream& operator<<( std::ostream& str, const MsqError& err )
159 : : {
160 [ # # ][ # # ]: 0 : str << "MESQUITE ERROR " << (int)err.error_code() << " : " << err.error_message() << std::endl;
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
161 : :
162 [ # # ]: 0 : MsqError::StackTrace::const_iterator iter = err.stack().begin();
163 [ # # ]: 0 : const MsqError::StackTrace::const_iterator end = err.stack().end();
164 [ # # ][ # # ]: 0 : if( iter != end )
165 : : {
166 [ # # ][ # # ]: 0 : str << " at " << *iter << std::endl;
[ # # ][ # # ]
167 [ # # ]: 0 : ++iter;
168 : : }
169 [ # # ][ # # ]: 0 : for( ; iter != end; ++iter )
[ # # ]
170 [ # # ][ # # ]: 0 : str << " in " << *iter << std::endl;
[ # # ][ # # ]
171 : :
172 : 0 : return str;
173 : : }
174 : :
175 : 94 : MsqPrintError::~MsqPrintError()
176 : : {
177 [ - + ]: 47 : if( error() ) outputStream << *this << std::endl;
178 [ - + ]: 47 : }
179 : :
180 : : } // namespace MBMesquite
|