Error.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "Log.h"
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <stdarg.h>
  27. #if defined __GNUG__ && defined(LINUX)
  28. #include <cxxabi.h>
  29. #include <execinfo.h>
  30. #endif
  31. #include "Config.h"
  32. #pragma once
  33. namespace crown
  34. {
  35. namespace error
  36. {
  37. inline void log_backtrace()
  38. {
  39. #if defined __GNUG__ && defined(LINUX)
  40. void* array[50];
  41. int size = backtrace(array, 50);
  42. char** messages = backtrace_symbols(array, size);
  43. // skip first stack frame (points here)
  44. for (int i = 1; i < size && messages != NULL; ++i)
  45. {
  46. char *mangled_name = 0, *offset_begin = 0, *offset_end = 0;
  47. // find parantheses and +address offset surrounding mangled name
  48. for (char *p = messages[i]; *p; ++p)
  49. {
  50. if (*p == '(')
  51. {
  52. mangled_name = p;
  53. }
  54. else if (*p == '+')
  55. {
  56. offset_begin = p;
  57. }
  58. else if (*p == ')')
  59. {
  60. offset_end = p;
  61. break;
  62. }
  63. }
  64. // if the line could be processed, attempt to demangle the symbol
  65. if (mangled_name && offset_begin && offset_end && mangled_name < offset_begin)
  66. {
  67. *mangled_name++ = '\0';
  68. *offset_begin++ = '\0';
  69. *offset_end++ = '\0';
  70. int status;
  71. char* real_name = abi::__cxa_demangle(mangled_name, 0, 0, &status);
  72. Log::e("\t[%d] %s: (%s)+%s %s", i, messages[i], (status == 0 ? real_name : mangled_name), offset_begin, offset_end);
  73. free(real_name);
  74. }
  75. // otherwise, print the whole line
  76. else
  77. {
  78. Log::e("\t[%d] %s", i, messages[i]);
  79. }
  80. }
  81. free(messages);
  82. #endif
  83. }
  84. /// Aborts the program execution logging an error message and the stacktrace if
  85. /// the platform supports it.
  86. inline void abort(const char* file, int line, const char* message, ...)
  87. {
  88. va_list ap;
  89. va_start(ap, message);
  90. Log::e1(message, ap);
  91. va_end(ap);
  92. Log::e("\tIn: %s:%d\n", file, line);
  93. Log::e("Backtrace:");
  94. //fflush(0);
  95. log_backtrace();
  96. exit(EXIT_FAILURE);
  97. }
  98. } // namespace error
  99. } // namespace crown