Revert "License conversion from Flora to Apache 2.0"
[platform/core/uifw/dali-core.git] / dali / public-api / common / dali-common.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 // CLASS HEADER
18 #include <dali/public-api/common/dali-common.h>
19
20 // EXTERNAL INCLUDES
21 #include <stdlib.h>
22 #include <string>
23 #include <cstdio>
24 #include <execinfo.h>
25
26 #ifndef EMSCRIPTEN // cxxabi not supported
27 # include <cxxabi.h>
28 #endif
29
30 #include <cstring>
31
32 // INTERNAL INCLUDES
33 #include <dali/integration-api/debug.h>
34
35 namespace
36 {
37 const int MAX_NUM_STACK_FRAMES = 25;
38 const size_t C_SYMBOL_LENGTH = 4096;
39 }
40
41 namespace Dali
42 {
43
44 #ifndef EMSCRIPTEN
45
46 std::string Demangle(const char* symbol)
47 {
48   std::string result;
49
50   // backtrace ::=  <filename>'('['_'<mangled-symbol>'_']['+'<offset>]')'
51   // Only want <mangled-symbol>:
52   const char* openParen = strchr(symbol, '(');
53   if(openParen != NULL)
54   {
55     const char* startOfToken = openParen + 1;
56     const char* plus = strchr(startOfToken, '+');
57     const char* closeParen = strchr(startOfToken, ')');
58     const char* endOfToken = NULL;
59     if(plus != NULL)
60     {
61       endOfToken = plus;
62     }
63     else if(closeParen != NULL)
64     {
65       endOfToken = closeParen;
66     }
67     if(endOfToken != NULL)
68     {
69       size_t tokenLength = endOfToken - startOfToken;
70
71       // Allocate space for symbol
72       char *mangledSymbol = (char*)malloc(tokenLength+1u);
73       strncpy(mangledSymbol, startOfToken, tokenLength);
74       mangledSymbol[tokenLength] = '\0';
75
76       size_t size;
77       int    status;
78       char*  demangled=NULL;
79       demangled = abi::__cxa_demangle( mangledSymbol, NULL, &size, &status );
80       if( demangled != NULL )
81       {
82         result = demangled;
83         free(demangled); // demangle() allocates returned string, so free it
84       }
85       else
86       {
87         result = symbol;
88       }
89       free(mangledSymbol);
90     }
91   }
92
93   return result;
94 }
95
96 #endif // EMSCRIPTEN
97
98 #ifndef EMSCRIPTEN
99
100 DALI_EXPORT_API DaliException::DaliException(const char *location, const char* condition)
101 : mLocation(location), mCondition(condition)
102 {
103   // Note, if a memory error has occured, then the backtrace won't work - backtrace_symbols relies on
104   // allocating memory.
105
106   // Initial dlog error message is output in DALI_ASSERT_ALWAYS macro
107   // Also output on stderr
108   fprintf(stderr, "Exception: \n%s\n thrown at %s\nSee dlog for backtrace\n", mCondition.c_str(), mLocation.c_str());
109
110   DALI_LOG_ERROR_NOFN("Backtrace:\n");
111
112   void* frameArray[MAX_NUM_STACK_FRAMES];
113   int nSize = backtrace(frameArray, MAX_NUM_STACK_FRAMES);
114   char** symbols = backtrace_symbols(frameArray, nSize);
115   for(int i=1; i< nSize; i++)
116   {
117     std::string demangled_symbol = Demangle(symbols[i]);
118     DALI_LOG_ERROR_NOFN("[%02d]   %s\n", i, demangled_symbol.c_str() );
119   }
120   free(symbols);
121 }
122
123 #else
124
125 DALI_EXPORT_API DaliException::DaliException(const char *location, const char* condition)
126 : mLocation(location), mCondition(condition)
127 {
128   printf("Exception: \n%s\n thrown at %s\nSee dlog for backtrace\n", mCondition.c_str(), mLocation.c_str());
129 }
130
131 #endif // EMSCRIPTEN
132
133 DALI_EXPORT_API void DaliAssertMessage(const char* condition, const char* file, int line)
134 {
135   DALI_LOG_ERROR_NOFN( "Assertion (%s) failed in: %s:%d\n", condition, file, line );
136 }
137
138 } // Dali