1#ifndef _DRAMA2_EXCEPT_H
2#define _DRAMA2_EXCEPT_H
37#include "drama2_err.h"
50#include "drama/fmt/format.h"
63#define DramaTHROW(status_,message_) throw drama::Exception(__func__, __FILE__,__LINE__,(message_), (status_))
88#define DramaTHROW_S(status_,format_,...) drama::ExceptionThrowSafe(__func__, __FILE__,__LINE__,(status_), (format_), __VA_ARGS__)
102#define DramaTHROW_F(status_,format_,...) drama::ExceptionThrowSafeF(__func__, __FILE__,__LINE__,(status_), (format_), __VA_ARGS__)
156 class Exception :
public std::exception {
163 std::string _sourceFile;
164 std::string _sourceFunc;
165 std::string _origMessage;
167 std::ostringstream _message;
168 std::ostringstream _stackTrace;
169 static const unsigned _stackTraceMax = 25;
170 std::string _whatString;
172 static bool _firstReportDone;
175 bool _alwaysStacktrace =
false;
182 void GenerateStackTrace(
const int nSize,
void *array[]);
195 Exception() : _lineNum(0), _sourceFile(
"none"),
196 _sourceFunc(
"none"), _origMessage(
"none"),
219 const std::string &
file,
221 const std::string &
message =
"",
243 const std::string
func,
244 const std::string &
file,
247 const char *
format, ...) :
266 _lineNum(
source._lineNum),
267 _sourceFile(
source._sourceFile),
269 _origMessage(
source._origMessage),
270 _dramaStatus(
source._dramaStatus),
271 _whatString(
source._whatString),
272 _alwaysStacktrace(
source._alwaysStacktrace) {
273 _message.str(
source._message.str());
281 _lineNum(std::move(
source._lineNum)),
282 _sourceFile(std::move(
source._sourceFile)),
283 _sourceFunc(std::move(
source._sourceFunc)),
284 _origMessage(std::move(
source._origMessage)),
285 _dramaStatus(std::move(
source._dramaStatus)),
286 _whatString(std::move(
source._whatString)),
287 _alwaysStacktrace(
source._alwaysStacktrace) {
294 _message.str(
source._message.str());
295 _stackTrace.str(
source._stackTrace.str());
309 this->_sourceFile =
rhs._sourceFile;
310 this->_sourceFunc =
rhs._sourceFunc;
311 this->_lineNum =
rhs._lineNum;
312 this->_origMessage =
rhs._origMessage;
313 this->_dramaStatus =
rhs._dramaStatus;
314 this->_whatString =
rhs._whatString;
316 this->_message.str(
rhs._message.str());
317 this->_stackTrace.str(
rhs._stackTrace.str());
318 this->_alwaysStacktrace =
rhs._alwaysStacktrace;
331 this->_sourceFile = std::move(
rhs._sourceFile);
332 this->_sourceFunc = std::move(
rhs._sourceFunc);
333 this->_lineNum = std::move(
rhs._lineNum);
334 this->_origMessage = std::move(
rhs._origMessage);
335 this->_dramaStatus = std::move(
rhs._dramaStatus);
336 this->_whatString = std::move(
rhs._whatString);
341 this->_message.str(
rhs._message.str());
342 this->_stackTrace.str(
rhs._stackTrace.str());
344 this->_alwaysStacktrace =
rhs._alwaysStacktrace;
381 virtual const std::string &
fileName()
const {
389 virtual const std::string &
function()
const {
452 return _stackTrace.str();
486 virtual void ErsOut()
const;
504 return _whatString.c_str();
519 template<
typename...
Args>
520 void FmtWriteF(
const fmt::format_string<Args...> fmt,
522 auto formated = fmt::vformat(fmt.get(), fmt::make_format_args(
args...));
534#ifndef RUNNING_DOXYGEN
535 D2_FMT_DEPRECATED(
"Replace FmtWrite() by FmtWriteF(). See MessageUserF() page for details.")
556 template<
typename T,
typename...
Types>
557#ifndef RUNNING_DOXYGEN
558 D2_FMT_DEPRECATED(
"Replace FmtWrite() by FmtWriteF(). See MessageUserF() page for details.")
580 if (_stackTrace.str().size() != 0)
583 std::cerr <<
"----------------------------------------\n"
584 <<
"drama::Exception::FmtWrite():Extra arguments provided to drama::ExceptionThrowSafe, a stack trace follows:\n"
585 <<
" This problem is likely triggered by a call to DramaTHROW_S() (a macro)\n"
586 <<
" Most likely case - look in the stack trace for a routine calling ExceptionThrowSafe()\n"
587 <<
" And then look in the source for a call to DramaTHROW_S()\n\n"
589 <<
"----------------------------------------" << std::endl;
592 "Extra arguments provided to drama::ExceptionThrowSafe (probably via a call to DramaTHROW_S())");
628 template<
typename...
Types>
629#ifndef RUNNING_DOXYGEN
630 D2_FMT_DEPRECATED (
"replace DramaTHROW_S() by DramaTHROW_F(). See MessageUserF() page for details.")
633 const std::string &
file,
669 template<
typename...
Args>
671 const std::string &
file,
674 const fmt::format_string<
Args...> fmt,
679 e << fmt::vformat(fmt.get(), fmt::make_format_args(
args...));
void FmtWriteF(const fmt::format_string< Args... > fmt, Args &&... args)
Safe formatted write to an exception message string with fmt::format string formatting.
Definition exception.hh:547
D2DEPRECATED Exception(bool makeUnique, const std::string func, const std::string &file, const int lineNum, const StatusType status, const char *format,...)
Create an exception using C printf() style string formatting.
Definition exception.hh:268
virtual std::string getStackTrace() const
Return the stack trace created when the exception was created.
Definition exception.hh:478
virtual int lineNumber() const
Returns the source file line number at which the exception occurred.
Definition exception.hh:400
virtual int statusAsSysExitCode() const
Return a system exit code based on the DRAMA Status.
Definition exception.hh:446
virtual void ErsRep() const
Report details of this exception using ERS.
virtual void ErsOut() const
Output a DRAMA ERS report about this exception.
Exception(const std::string func, const std::string &file, const int lineNum, const std::string &message="", const StatusType status=DRAMA2__CPP_ERROR, const bool alwaysStacktrace=false)
Create an Exception.
virtual const std::string & fileName() const
Returns the name of the C++ source file in which the exception occurred.
Definition exception.hh:408
virtual const std::string & getOrigMessage() const
Returns the exception original message.
Definition exception.hh:393
const char * what() const noexcept override
Get string identifying exception.
Definition exception.hh:530
virtual void Print(FILE *file=stderr, bool include_stacktrace=false) const
Print exception details to a file.
Exception(const Exception &source)
Copy constructor.
Definition exception.hh:292
Exception & operator=(const Exception &rhs)
Assignment operator.
Definition exception.hh:331
void FmtWrite(const char *format, T value, Types... args)
Safe formatted write to an exception message string.
Definition exception.hh:587
void FmtWrite(const char *str)
Safe formatted write to an exception message string.
virtual const std::string & function() const
Returns the name of the function in which the exception occurred.
Definition exception.hh:416
void AddErs()
Add the any errors from the current ERS context to the exception.
virtual std::string dramaStatusStr() const
Return the DRAMA Status code converted to a DRAMA.
virtual std::string toString(bool details=false) const
Return the exception string.
Exception & operator<<(const T &t)
Add extra information to an exception message.
Definition exception.hh:467
Exception(Exception &&source) noexcept
Move constructor.
Definition exception.hh:307
Exception()
Create an Exception, details empty.
Definition exception.hh:222
virtual StatusType dramaStatus() const
Return the DRAMA Status code associated with the exception.
Definition exception.hh:426
virtual ~Exception()
Virtual Destructor - since we will be inherited.
Definition exception.hh:624
Exception & operator=(Exception &&rhs) noexcept
Move assignment operator.
Definition exception.hh:353
An Exception class for exceptions thrown by DRAMA V2 classes.
Definition exception.hh:183
std::ostream & operator<<(std::ostream &strm, const drama::Exception &e)
drama::Exception stream output operator
Definition exception.hh:726
#define DramaTHROW(status_, message_)
Throw a Drama exception.
Definition exception.hh:90
void CreateRunDramaTask()
Create and run a DRAMA task, with standard exception handling.
Definition task.hh:1339
void ExceptionThrowSafe(const std::string func, const std::string &file, const int lineNum, StatusType status, const char *format, Types... args)
Create and throw an exception, variable argument list, Safe formatting of arguments.
Definition exception.hh:659
void ExceptionThrowSafeF(const std::string func, const std::string &file, const int lineNum, StatusType status, const fmt::format_string< Args... > fmt, Args &&... args)
Create and throw an exception, - format using fmt::format.
Definition exception.hh:697
The drama namespace contains all the classes, types etc of the DRAMA 2 implementation.
Definition drama.hh:99
DRAMA 2 include file - Utility macros, types etc.