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.
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.
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
26 #define WIN32_LEAN_AND_MEAN
45 #include "alc/alconfig.h"
46 #include "alc/context.h"
47 #include "core/logging.h"
48 #include "opthelpers.h"
53 context_error::context_error(ALenum code
, const char *msg
, ...) : mErrorCode
{code
}
55 /* NOLINTBEGIN(*-array-to-pointer-decay) */
58 setMessage(msg
, args
);
60 /* NOLINTEND(*-array-to-pointer-decay) */
62 context_error::~context_error() = default;
65 void ALCcontext::setError(ALenum errorCode
, const char *msg
, ...)
67 auto message
= std::vector
<char>(256);
69 /* NOLINTBEGIN(*-array-to-pointer-decay) */
70 std::va_list args
, args2
;
73 int msglen
{std::vsnprintf(message
.data(), message
.size(), msg
, args
)};
74 if(msglen
>= 0 && static_cast<size_t>(msglen
) >= message
.size())
76 message
.resize(static_cast<size_t>(msglen
) + 1u);
77 msglen
= std::vsnprintf(message
.data(), message
.size(), msg
, args2
);
81 /* NOLINTEND(*-array-to-pointer-decay) */
87 msg
= "<internal error constructing message>";
88 msglen
= static_cast<int>(strlen(msg
));
91 WARN("Error generated on context %p, code 0x%04x, \"%s\"\n",
92 decltype(std::declval
<void*>()){this}, errorCode
, msg
);
96 /* DebugBreak will cause an exception if there is no debugger */
97 if(IsDebuggerPresent())
99 #elif defined(SIGTRAP)
104 if(mLastThreadError
.get() == AL_NO_ERROR
)
105 mLastThreadError
.set(errorCode
);
107 debugMessage(DebugSource::API
, DebugType::Error
, static_cast<ALuint
>(errorCode
),
108 DebugSeverity::High
, {msg
, static_cast<uint
>(msglen
)});
111 /* Special-case alGetError since it (potentially) raises a debug signal and
112 * returns a non-default value for a null context.
114 AL_API
auto AL_APIENTRY
alGetError() noexcept
-> ALenum
116 if(auto context
= GetContextRef()) LIKELY
117 return alGetErrorDirect(context
.get());
119 auto get_value
= [](const char *envname
, const char *optname
) -> ALenum
121 auto optstr
= al::getenv(envname
);
123 optstr
= ConfigValueStr({}, "game_compat", optname
);
127 auto value
= std::strtoul(optstr
->c_str(), &end
, 0);
128 if(end
&& *end
== '\0' && value
<= std::numeric_limits
<ALenum
>::max())
129 return static_cast<ALenum
>(value
);
130 ERR("Invalid default error value: \"%s\"", optstr
->c_str());
132 return AL_INVALID_OPERATION
;
134 static const ALenum deferror
{get_value("__ALSOFT_DEFAULT_ERROR", "default-error")};
136 WARN("Querying error state on null context (implicitly 0x%04x)\n", deferror
);
140 if(IsDebuggerPresent())
142 #elif defined(SIGTRAP)
149 FORCE_ALIGN ALenum AL_APIENTRY
alGetErrorDirect(ALCcontext
*context
) noexcept
151 ALenum ret
{context
->mLastThreadError
.get()};
152 if(ret
!= AL_NO_ERROR
) UNLIKELY
153 context
->mLastThreadError
.set(AL_NO_ERROR
);