client-cpp  0.6.1
KaaException.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2014 CyberVision, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef KAAEXCEPTION_HPP_
18 #define KAAEXCEPTION_HPP_
19 
20 #include <boost/format.hpp>
21 #include <exception>
22 #include <sstream>
23 #include <execinfo.h>
24 
25 namespace kaa {
26 
27 class KaaException : public std::exception {
28 public:
29  KaaException() : message_("") {}
30 
31  KaaException(boost::format f) {
32  std::stringstream ss;
33  ss << "[Kaa OpenSource Project] Instruction failed! Details: \"" << f
34  << "\" Original message: " << std::exception::what();
35  void *trace[16];
36  char **messages = (char **)nullptr;
37  int i, trace_size = 0;
38  trace_size = backtrace(trace, 16);
39  messages = backtrace_symbols(trace, trace_size);
40  ss << std::endl << "Backtrace: " << std::endl;
41  for (i = 0; i < trace_size; ++i) {
42  ss << "[" << i << "] " << messages[i] << std::endl;
43  }
44  message_ = ss.str();
45  }
46 
47  KaaException(const std::string &message) {
48  std::stringstream ss;
49  ss << "[Kaa OpenSource Project] Instruction failed! Details: \"" << message
50  << "\" Original message: " << std::exception::what();
51  void *trace[16];
52  char **messages = (char **)nullptr;
53  int i, trace_size = 0;
54  trace_size = backtrace(trace, 16);
55  messages = backtrace_symbols(trace, trace_size);
56  ss << std::endl << "Backtrace: " << std::endl;
57  for (i = 0; i < trace_size; ++i) {
58  ss << "[" << i << "] " << messages[i] << std::endl;
59  }
60  message_ = ss.str();
61  }
62 
63  virtual const char * what() const throw () {
64  return message_.c_str();
65  }
66 
67  virtual ~KaaException() throw () {}
68 
69 private:
70  std::string message_;
71 };
72 
73 } // namespace kaa
74 
75 
76 #endif /* KAAEXCEPTION_HPP_ */
KaaException(const std::string &message)
virtual ~KaaException()
virtual const char * what() const
KaaException(boost::format f)