error.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * OpenAL cross platform audio library
  3. * Copyright (C) 1999-2000 by authors.
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this library; if not, write to the
  16. * Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. * Or go to http://www.gnu.org/copyleft/lgpl.html
  19. */
  20. #include "config.h"
  21. #ifdef _WIN32
  22. #define WIN32_LEAN_AND_MEAN
  23. #include <windows.h>
  24. #endif
  25. #include <csignal>
  26. #include <cstdarg>
  27. #include <cstdio>
  28. #include <cstdlib>
  29. #include <cstring>
  30. #include <optional>
  31. #include <string>
  32. #include <utility>
  33. #include "AL/al.h"
  34. #include "AL/alc.h"
  35. #include "al/debug.h"
  36. #include "alc/alconfig.h"
  37. #include "alc/context.h"
  38. #include "alnumeric.h"
  39. #include "core/except.h"
  40. #include "core/logging.h"
  41. #include "opthelpers.h"
  42. #include "strutils.h"
  43. void ALCcontext::setErrorImpl(ALenum errorCode, const fmt::string_view fmt, fmt::format_args args)
  44. {
  45. const auto msg = fmt::vformat(fmt, std::move(args));
  46. WARN("Error generated on context {}, code {:#04x}, \"{}\"",
  47. decltype(std::declval<void*>()){this}, as_unsigned(errorCode), msg);
  48. if(TrapALError)
  49. {
  50. #ifdef _WIN32
  51. /* DebugBreak will cause an exception if there is no debugger */
  52. if(IsDebuggerPresent())
  53. DebugBreak();
  54. #elif defined(SIGTRAP)
  55. raise(SIGTRAP);
  56. #endif
  57. }
  58. if(mLastThreadError.get() == AL_NO_ERROR)
  59. mLastThreadError.set(errorCode);
  60. debugMessage(DebugSource::API, DebugType::Error, static_cast<ALuint>(errorCode),
  61. DebugSeverity::High, msg);
  62. }
  63. void ALCcontext::throw_error_impl(ALenum errorCode, const fmt::string_view fmt,
  64. fmt::format_args args)
  65. {
  66. setErrorImpl(errorCode, fmt, std::move(args));
  67. throw al::base_exception{};
  68. }
  69. /* Special-case alGetError since it (potentially) raises a debug signal and
  70. * returns a non-default value for a null context.
  71. */
  72. AL_API auto AL_APIENTRY alGetError() noexcept -> ALenum
  73. {
  74. if(auto context = GetContextRef()) LIKELY
  75. return alGetErrorDirect(context.get());
  76. auto get_value = [](const char *envname, const char *optname) -> ALenum
  77. {
  78. auto optstr = al::getenv(envname);
  79. if(!optstr)
  80. optstr = ConfigValueStr({}, "game_compat", optname);
  81. if(optstr)
  82. {
  83. try {
  84. auto idx = 0_uz;
  85. auto value = std::stoi(*optstr, &idx, 0);
  86. if(idx >= optstr->size() || std::isspace(optstr->at(idx)))
  87. return static_cast<ALenum>(value);
  88. } catch(...) {
  89. }
  90. ERR("Invalid default error value: \"{}\"", *optstr);
  91. }
  92. return AL_INVALID_OPERATION;
  93. };
  94. static const ALenum deferror{get_value("__ALSOFT_DEFAULT_ERROR", "default-error")};
  95. WARN("Querying error state on null context (implicitly {:#04x})", as_unsigned(deferror));
  96. if(TrapALError)
  97. {
  98. #ifdef _WIN32
  99. if(IsDebuggerPresent())
  100. DebugBreak();
  101. #elif defined(SIGTRAP)
  102. raise(SIGTRAP);
  103. #endif
  104. }
  105. return deferror;
  106. }
  107. FORCE_ALIGN ALenum AL_APIENTRY alGetErrorDirect(ALCcontext *context) noexcept
  108. {
  109. ALenum ret{context->mLastThreadError.get()};
  110. if(ret != AL_NO_ERROR) UNLIKELY
  111. context->mLastThreadError.set(AL_NO_ERROR);
  112. return ret;
  113. }