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