client-cpp  0.9.0
KaaException.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2014-2016 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 
24 #ifndef NDEBUG
25 #ifdef _WIN32
26 #define WIN32_LEAN_AND_MEAN
27 #define NOMINMAX
28 #include <Windows.h>
29 #undef NOMINMAX
30 #include <DbgHelp.h>
31 #undef WIN32_LEAN_AND_MEAN
32 #else
33 #include <execinfo.h>
34 #endif
35 #endif // NDEBUG
36 
37 namespace kaa {
38 
39 class KaaException : public std::exception {
40 public:
41  KaaException() : message_("") {}
42 
43  KaaException(boost::format& f) {
44  std::stringstream ss;
45  ss << "[Kaa OpenSource Project] Instruction failed! Details: \"" << f
46  << "\" Original message: " << std::exception::what();
47  captureStack(ss);
48  message_ = ss.str();
49  }
50 
51  KaaException(const std::string& message) {
52  std::stringstream ss;
53  ss << "[Kaa OpenSource Project] Instruction failed! Details: \"" << message
54  << "\" Original message: " << std::exception::what();
55  captureStack(ss);
56  message_ = ss.str();
57  }
58 
59  KaaException(const std::exception& e) {
60  std::stringstream ss;
61  ss << "[Kaa OpenSource Project] Instruction failed! Details: \"" << e.what();
62  captureStack(ss);
63  message_ = ss.str();
64  }
65 
66  virtual const char * what() const throw() {
67  return message_.c_str();
68  }
69 
70  virtual ~KaaException() = default;
71 
72 private:
73  std::string message_;
74 
75  void captureStack(std::stringstream& ss) {
76 #ifndef NDEBUG
77  void *trace[16];
78  int i, trace_size = 0;
79 #ifdef _WIN32
80  SYMBOL_INFO * symbol;
81  HANDLE process;
82  process = GetCurrentProcess();
83  SymInitialize( process, NULL, TRUE );
84  trace_size = CaptureStackBackTrace( 0, 16, trace, NULL );
85  symbol = ( SYMBOL_INFO * )calloc( sizeof( SYMBOL_INFO ) + 256 * sizeof( char ), 1 );
86  symbol->MaxNameLen = 255;
87  symbol->SizeOfStruct = sizeof( SYMBOL_INFO );
88 #else
89  char **messages = (char **)nullptr;
90  trace_size = backtrace(trace, 16);
91  messages = backtrace_symbols(trace, trace_size);
92 #endif
93  ss << std::endl << "Backtrace: " << std::endl;
94  for (i = 0; i < trace_size; ++i) {
95 #ifdef _WIN32
96  SymFromAddr( process, ( DWORD64 )( trace[ i ] ), 0, symbol );
97  ss << "[" << trace_size - i - 1 << "] " << symbol->Name << " - " << boost::format("0x%0X")%symbol->Address << std::endl;
98 #else
99  ss << "[" << i << "] " << messages[i] << std::endl;
100 #endif
101  }
102 #ifdef _WIN32
103  free( symbol );
104 #endif
105 #else
106  static_cast<void>(ss);
107 #endif // NDEBUG
108  }
109 };
110 
111 } // namespace kaa
112 
113 
114 #endif /* KAAEXCEPTION_HPP_ */
KaaException(const std::string &message)
virtual const char * what() const
KaaException(const std::exception &e)
KaaException(boost::format &f)
virtual ~KaaException()=default