tc_ex.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #include "util/tc_ex.h"
  17. #include "util/tc_platform.h"
  18. #if TARGET_PLATFORM_LINUX
  19. #include <execinfo.h>
  20. #endif
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <cerrno>
  24. #include <iostream>
  25. namespace tars
  26. {
  27. TC_Exception::TC_Exception(const string &buffer)
  28. : _code(0), _buffer(buffer)
  29. {
  30. }
  31. TC_Exception::TC_Exception(const string &buffer, int err)
  32. {
  33. if(err != 0)
  34. {
  35. _buffer = buffer + " :" + parseError(err);
  36. }
  37. else
  38. {
  39. _buffer = buffer;
  40. }
  41. _code = err;
  42. }
  43. TC_Exception::~TC_Exception() throw()
  44. {
  45. }
  46. const char* TC_Exception::what() const throw()
  47. {
  48. return _buffer.c_str();
  49. }
  50. void TC_Exception::getBacktrace()
  51. {
  52. #if TARGET_PLATFORM_LINUX
  53. void * array[64];
  54. int nSize = backtrace(array, 64);
  55. char ** symbols = backtrace_symbols(array, nSize);
  56. for (int i = 0; i < nSize; i++)
  57. {
  58. _buffer += symbols[i];
  59. _buffer += "\n";
  60. }
  61. free(symbols);
  62. #endif
  63. }
  64. #if TARGET_PLATFORM_WINDOWS
  65. static std::string Unicode2ANSI(LPCWSTR lpszSrc)
  66. {
  67. std::string sResult;
  68. if (lpszSrc != NULL) {
  69. int nANSILen = WideCharToMultiByte(CP_ACP, 0, lpszSrc, -1, NULL, 0, NULL, NULL);
  70. char* pANSI = new char[nANSILen + 1];
  71. if (pANSI != NULL) {
  72. ZeroMemory(pANSI, nANSILen + 1);
  73. WideCharToMultiByte(CP_ACP, 0, lpszSrc, -1, pANSI, nANSILen, NULL, NULL);
  74. sResult = pANSI;
  75. delete[] pANSI;
  76. }
  77. }
  78. return sResult;
  79. }
  80. #endif
  81. string TC_Exception::parseError(int err)
  82. {
  83. string errMsg;
  84. #if TARGET_PLATFORM_LINUX || TARGET_PLATFORM_IOS
  85. errMsg = strerror(err);
  86. #else
  87. // LPTSTR lpMsgBuf;
  88. LPSTR lpMsgBuf;
  89. FormatMessageA(
  90. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  91. NULL, err, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
  92. (LPTSTR) & lpMsgBuf, 0, NULL);
  93. // errMsg = Unicode2ANSI((LPCWSTR)lpMsgBuf);
  94. if(lpMsgBuf != NULL)
  95. {
  96. errMsg = lpMsgBuf;
  97. }
  98. LocalFree(lpMsgBuf);
  99. #endif
  100. return errMsg;
  101. }
  102. int TC_Exception::getSystemCode()
  103. {
  104. #if TARGET_PLATFORM_WINDOWS
  105. return GetLastError();
  106. #else
  107. return errno;
  108. #endif
  109. }
  110. string TC_Exception::getSystemError()
  111. {
  112. return parseError(getSystemCode());
  113. }
  114. }