dodo  0.0.1
A C++ library to create containerized Linux services
exception.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the dodo library (https://github.com/jmspit/dodo).
3  * Copyright (c) 2019 Jan-Marten Spit.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, version 3.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /**
19  * @file exception.cpp
20  * Implement exceptions.
21  */
22 
23 #include <common/exception.hpp>
24 #include <iostream>
25 
26 #if CMAKE_CXX_COMPILER_ID == GNU
27  #include <cxxabi.h>
28 #endif
29 
30 namespace dodo::common {
31 
32  std::string DebugObject::debugString() const {
33  std::stringstream ss;
34  ss << debugHeader();
35  ss << debugDetail();
36  return ss.str();
37  }
38 
39  std::string DebugObject::debugHeader() const {
40  std::stringstream ss;
41 
42  #if CMAKE_CXX_COMPILER_ID == GNU
43  int status;
44  char * demangled = abi::__cxa_demangle(typeid(*this).name(),0,0,&status);
45  ss << demangled << " ";
46  free(demangled);
47  #else
48  ss << typeid(*this).name() << " ";
49  #endif
50 
51  ss << "address " << std::hex << (void*)this << " ";
52  return ss.str();
53  }
54 
55  Exception::Exception( const std::string &file, unsigned int line, const std::string &what ) :
56  std::runtime_error(what), file_(file), line_(line) {
57  std::stringstream ss;
58  ss << file_ << ":" << line_ << " " << what;
59  msg_ = ss.str();
60  }
61 
62  Exception::Exception( const std::string &file, unsigned int line, const std::string &what, const DebugObject* thing ) :
63  std::runtime_error(what), file_(file), line_(line) {
64  std::stringstream ss;
65  ss << file_ << ":" << line_ << " " << what;
66  ss << std::endl << " " << thing->debugString();
67  msg_ = ss.str();
68  }
69 
70  Exception::~Exception() {
71  }
72 
73  const char* Exception::what() const noexcept {
74  return msg_.c_str();
75  }
76 
77  SystemException::SystemException( const std::string &file,
78  unsigned int line,
79  const std::string &what,
80  const SystemError &error )
81  : Exception( file, line, what + " : " + error.asString() ) {
82  error_ = error;
83  }
84 
85  SystemException::SystemException( const std::string &file,
86  unsigned int line,
87  const std::string &what,
88  const SystemError &error,
89  const DebugObject* thing )
90  : Exception( file,
91  line,
92  common::Puts() << what << " : " << error.asString() << common::Puts::endl() << thing->debugString() ) {
93  error_ = error;
94  }
95 
96 }
dodo::common::Exception::line_
unsigned int line_
The source line number.
Definition: exception.hpp:129
dodo::common::SystemException::error_
dodo::common::SystemError error_
The exception system error.
Definition: exception.hpp:167
dodo::common::DebugObject
Interface to objects that support dumping their state to a string.
Definition: exception.hpp:39
dodo::common::Exception::Exception
Exception(const std::string &file, unsigned int line, const std::string &what)
Construct an Exception.
Definition: exception.cpp:55
dodo::common::Exception::what
virtual const char * what() const noexcept
Return the exception message.
Definition: exception.cpp:73
dodo::common::DebugObject::debugHeader
std::string debugHeader() const
Generates a debug header (address of this object and a demangled class name.
Definition: exception.cpp:39
dodo::common::Exception::file_
std::string file_
The source file.
Definition: exception.hpp:124
dodo::common::SystemException::SystemException
SystemException(const std::string &file, unsigned int line, const std::string &what, const dodo::common::SystemError &error)
Constructor.
Definition: exception.cpp:77
dodo::common
Common and utility interfaces.
Definition: application.hpp:29
dodo::common::DebugObject::debugString
std::string debugString() const
Return the object dump to string.
Definition: exception.cpp:32
dodo::common::DebugObject::debugDetail
virtual std::string debugDetail() const
Descendant classes can override to dump details specific to the class.
Definition: exception.hpp:66
exception.hpp
dodo::common::Puts
Helper class to write strings in stream format, eg.
Definition: puts.hpp:46
dodo::common::SystemError
Linux system error primitive to provide a consistent interface to Linux error codes.
Definition: systemerror.hpp:53
dodo::common::Exception::msg_
std::string msg_
The exception message.
Definition: exception.hpp:131
dodo::common::Exception
An Exception is thrown in exceptional circumstances, and its occurrence should generally imply that t...
Definition: exception.hpp:83