PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/OpenFOAM/db/error/error.H

https://github.com/exerciseGroup/OpenFOAM-2.0.x
C Header | 358 lines | 142 code | 84 blank | 132 comment | 0 complexity | 6923401266f9b8013439642e403df243 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
  1. /*---------------------------------------------------------------------------*\
  2. ========= |
  3. \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
  4. \\ / O peration |
  5. \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
  6. \\/ M anipulation |
  7. -------------------------------------------------------------------------------
  8. License
  9. This file is part of OpenFOAM.
  10. OpenFOAM is free software: you can redistribute it and/or modify it
  11. under the terms of the GNU General Public License as published by
  12. the Free Software Foundation, either version 3 of the License, or
  13. (at your option) any later version.
  14. OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
  15. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  17. for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
  20. Class
  21. Foam::error
  22. Description
  23. Class to handle errors and exceptions in a simple, consistent stream-based
  24. manner.
  25. The error class is globaly instantiated with a title string. Errors,
  26. messages and other data are piped to the messageStream class in the
  27. standard manner. Manipulators are supplied for exit and abort which may
  28. terminate the program or throw an exception depending of if the exception
  29. handling has beed switched on (off by default).
  30. Usage
  31. \code
  32. error << "message1" << "message2" << FoamDataType << exit(errNo);
  33. error << "message1" << "message2" << FoamDataType << abort();
  34. \endcode
  35. SourceFiles
  36. error.C
  37. \*---------------------------------------------------------------------------*/
  38. #ifndef error_H
  39. #define error_H
  40. #include "messageStream.H"
  41. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
  42. namespace Foam
  43. {
  44. // Forward declaration of friend functions and operators
  45. class error;
  46. Ostream& operator<<(Ostream&, const error&);
  47. /*---------------------------------------------------------------------------*\
  48. Class error Declaration
  49. \*---------------------------------------------------------------------------*/
  50. class error
  51. :
  52. public std::exception,
  53. public messageStream
  54. {
  55. protected:
  56. // Protected data
  57. string functionName_;
  58. string sourceFileName_;
  59. label sourceFileLineNumber_;
  60. bool abort_;
  61. bool throwExceptions_;
  62. OStringStream* messageStreamPtr_;
  63. public:
  64. // Constructors
  65. //- Construct from title string
  66. error(const string& title);
  67. //- Construct from dictionary
  68. error(const dictionary&);
  69. //- Construct as copy
  70. error(const error&);
  71. //- Destructor
  72. virtual ~error() throw();
  73. // Member functions
  74. string message() const;
  75. const string& functionName() const
  76. {
  77. return functionName_;
  78. }
  79. const string& sourceFileName() const
  80. {
  81. return sourceFileName_;
  82. }
  83. label sourceFileLineNumber() const
  84. {
  85. return sourceFileLineNumber_;
  86. }
  87. void throwExceptions()
  88. {
  89. throwExceptions_ = true;
  90. }
  91. void dontThrowExceptions()
  92. {
  93. throwExceptions_ = false;
  94. }
  95. //- Convert to OSstream
  96. // Prints basic message and returns OSstream for further info.
  97. OSstream& operator()
  98. (
  99. const char* functionName,
  100. const char* sourceFileName,
  101. const int sourceFileLineNumber = 0
  102. );
  103. //- Convert to OSstream
  104. // Prints basic message and returns OSstream for further info.
  105. OSstream& operator()
  106. (
  107. const string& functionName,
  108. const char* sourceFileName,
  109. const int sourceFileLineNumber = 0
  110. );
  111. //- Convert to OSstream
  112. // Prints basic message and returns OSstream for further info.
  113. operator OSstream&();
  114. //- Explicitly convert to OSstream for << operations
  115. OSstream& operator()()
  116. {
  117. return operator OSstream&();
  118. }
  119. //- Create and return a dictionary
  120. operator dictionary() const;
  121. //- Helper function to print a stack
  122. static void printStack(Ostream&);
  123. //- Exit : can be called for any error to exit program.
  124. // Prints stack before exiting.
  125. void exit(const int errNo = 1);
  126. //- Abort : used to stop code for fatal errors.
  127. // Prints stack before exiting.
  128. void abort();
  129. // Ostream operator
  130. friend Ostream& operator<<(Ostream&, const error&);
  131. };
  132. // Forward declaration of friend functions and operators
  133. class IOerror;
  134. Ostream& operator<<(Ostream&, const IOerror&);
  135. /*---------------------------------------------------------------------------*\
  136. Class IOerror Declaration
  137. \*---------------------------------------------------------------------------*/
  138. //- Report an I/O error
  139. class IOerror
  140. :
  141. public error
  142. {
  143. // Private data
  144. string ioFileName_;
  145. label ioStartLineNumber_;
  146. label ioEndLineNumber_;
  147. public:
  148. // Constructors
  149. //- Construct from title string
  150. IOerror(const string& title);
  151. //- Construct from dictionary
  152. IOerror(const dictionary&);
  153. //- Destructor
  154. virtual ~IOerror() throw();
  155. // Member functions
  156. const string& ioFileName() const
  157. {
  158. return ioFileName_;
  159. }
  160. label ioStartLineNumber() const
  161. {
  162. return ioStartLineNumber_;
  163. }
  164. label ioEndLineNumber() const
  165. {
  166. return ioEndLineNumber_;
  167. }
  168. //- Convert to OSstream
  169. // Prints basic message and returns OSstream for further info.
  170. OSstream& operator()
  171. (
  172. const char* functionName,
  173. const char* sourceFileName,
  174. const int sourceFileLineNumber,
  175. const string& ioFileName,
  176. const label ioStartLineNumber = -1,
  177. const label ioEndLineNumber = -1
  178. );
  179. //- Convert to OSstream
  180. // Prints basic message and returns OSstream for further info.
  181. OSstream& operator()
  182. (
  183. const char* functionName,
  184. const char* sourceFileName,
  185. const int sourceFileLineNumber,
  186. const IOstream&
  187. );
  188. //- Convert to OSstream
  189. // Prints basic message and returns OSstream for further info.
  190. OSstream& operator()
  191. (
  192. const char* functionName,
  193. const char* sourceFileName,
  194. const int sourceFileLineNumber,
  195. const dictionary&
  196. );
  197. //- Print basic message and exit. Uses cerr if streams not constructed
  198. // yet (at startup). Use in startup parsing instead of FatalError.
  199. static void SafeFatalIOError
  200. (
  201. const char* functionName,
  202. const char* sourceFileName,
  203. const int sourceFileLineNumber,
  204. const IOstream&,
  205. const string& msg
  206. );
  207. //- Create and return a dictionary
  208. operator dictionary() const;
  209. //- Exit : can be called for any error to exit program
  210. void exit(const int errNo = 1);
  211. //- Abort : used to stop code for fatal errors
  212. void abort();
  213. // Ostream operator
  214. friend Ostream& operator<<(Ostream&, const IOerror&);
  215. };
  216. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
  217. // Global error declarations: defined in error.C
  218. extern error FatalError;
  219. extern IOerror FatalIOError;
  220. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
  221. } // End namespace Foam
  222. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
  223. // Convenience macros to add the file name and line number to the function name
  224. /**
  225. * \def FatalErrorIn(functionName)
  226. * Report an error message using Foam::FatalError for functionName in
  227. * file __FILE__ at line __LINE__
  228. */
  229. #define FatalErrorIn(fn) \
  230. ::Foam::FatalError((fn), __FILE__, __LINE__)
  231. /**
  232. * \def FatalIOErrorIn(functionName, ios)
  233. * Report an error message using Foam::FatalIOError for functionName in
  234. * file __FILE__ at line __LINE__
  235. * for a particular IOstream
  236. */
  237. #define FatalIOErrorIn(fn, ios) \
  238. ::Foam::FatalIOError((fn), __FILE__, __LINE__, (ios))
  239. /**
  240. * \def SafeFatalIOErrorIn(functionName, ios, msg)
  241. * Report an error message using Foam::FatalIOError (or cerr if FatalIOError
  242. * not yet constructed) for functionName in
  243. * file __FILE__ at line __LINE__
  244. * for a particular IOstream
  245. */
  246. #define SafeFatalIOErrorIn(fn, ios, msg) \
  247. ::Foam::IOerror::SafeFatalIOError((fn), __FILE__, __LINE__, (ios), (msg))
  248. /**
  249. * \def notImplemented(functionName)
  250. * Issue a FatalErrorIn for the functionName.
  251. * This is used for functions that are not currently implemented.
  252. * The functionName is printed and then abort is called.
  253. *
  254. * \note
  255. * This macro can be particularly useful when methods must be defined to
  256. * complete the interface of a derived class even if they should never be
  257. * called for this derived class.
  258. */
  259. #define notImplemented(fn) \
  260. FatalErrorIn(fn) << "Not implemented" << ::Foam::abort(FatalError);
  261. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
  262. #include "errorManip.H"
  263. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
  264. #endif
  265. // ************************************************************************* //